0% found this document useful (0 votes)
10 views4 pages

Lab Report: Bubble Sort

The document is a lab report for the Algorithm Design Sessional course, detailing various sorting algorithms including Bubble Sort, Insertion Sort, Merge Sort, Quick Sort, and Selection Sort, as well as algorithms for Coin Change, Fractional Knapsack, Travelling Salesman Problem, and 0/1 Knapsack Problem. Each algorithm is presented with its implementation in C++ and includes example usage. The report serves as a practical demonstration of algorithm design and implementation techniques.

Uploaded by

eldieblo30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Lab Report: Bubble Sort

The document is a lab report for the Algorithm Design Sessional course, detailing various sorting algorithms including Bubble Sort, Insertion Sort, Merge Sort, Quick Sort, and Selection Sort, as well as algorithms for Coin Change, Fractional Knapsack, Travelling Salesman Problem, and 0/1 Knapsack Problem. Each algorithm is presented with its implementation in C++ and includes example usage. The report serves as a practical demonstration of algorithm design and implementation techniques.

Uploaded by

eldieblo30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Report

Course Title: Algorithm Design Sessional


Course Code: CSE 0613-2104
Name: Shamim Ahmed Hasib
Student ID: 12321187
Semester: 3rd
Section :D
Bubble Sort: #include<iostream> using namespace std;
void bubbleSort(int array[], int size){
int temp;
for (int i = 0; i<size;i++)
{ for (int j = 0; j<size -i-1;j++)
{ if(array[j]>array[j+1])
{ temp = array[j]; array[j] =
array[j+1]; array[j+1] =
temp;
} } } int main()
{ int array[] = {1,5,6,4,2,3}; int size = sizeof(array)/sizeof(array[0]);
for (int i = 0; i<size;i++) {
cout << array[i] << " ";
} bubbleSort(array, size); cout
<< "\nAfter Sorting: "; for
(int i = 0; i<size;i++)
{ cout << array[i] << " "; } }

Insertion Sort:
#include<iostream>
using namespace
std; void insertionSort(int array[], int
size)
{ for(int i=1; i<size;i++)
{ int temp = array[i]; int j = i
- 1;
while(j>=0 && array[j]>temp)
{ array[j+1] = array[j]; j--; }
// cout << array[j+1] << " " << endl; array[j+1] = temp; } }
int main()
{ int array[] = {1,5,4,6,3,2,0,7,9,8}; int size =
sizeof(array)/sizeof(array[0]); insertionSort(array,size);
for(int i =0;i<size;i++)
{ cout << array[i] << " "; } }

Merge Sort:
#include <iostream> using namespacestd;
int arr[6] = {6, 5, 12, 10, 9, 1};
void merge(int l, int r, int m) { int
size_l = m - l + 1; int
size_r = r - m; int left_arr[size_l],
right_arr[size_r]; for
(int i = 0; i < size_l; i++) { left_arr[i] = arr[l + i]; }
for (int i = 0; i < size_r; i++) { right_arr[i] = arr[m + 1 + i]; }
int i = 0, j = 0, k = l;
while (i < size_l && j < size_r) { if
(left_arr[i] <= right_arr[j]) { arr[k]
= left_arr[i]; i++;
} else { arr[k] = right_arr[j]; j++;
} k++; }
while (i < size_l) { arr[k] =
left_arr[i]; i++;
k++; }
while (j < size_r) { arr[k] =
right_arr[j]; j++;
k++; } }
void mergesort(int l, int r) { if (l >= r) return; int m = l + (r - l) /
2; mergesort(l, m); mergesort(m + 1, r);
merge(l, r, m); }
void printArray(int size) {
for (int i = 0; i < size; i++) { cout << arr[i] << " "; } }

int main() { int size = 6;


mergesort(0, size - 1);
cout << "Sorted array: \n"; printArray(size);
return 0; }

Quick Sort:
#include<iostream> using namespace std; int partition(int array[], int start, int end)
{ int pivot = array[end]; int i = start -1;
for(int j=start;j<=end-1;j++)
{ if(array[j]< pivot)
{ i++; int temp = array[i]; array[i] =
array[j]; array[j] = temp; }
} i++; int temp = array[i]; array[i] = array[end];
array[end] = temp; return i;
}
void quickSort(int array[], int start, int end)
{ if(end <= start) return;
int pivot = partition(array, start ,end); quickSort(array, start, pivot -1);
quickSort(array, pivot +1, end);
}
int main()
{ int array[] = {1,5,4,6,3,2,0,7,9,8}; int size = sizeof(array)/sizeof(array[0]); quickSort(array,
0, size-1); for(int i =0;i<size;i++)
{ cout << array[i] << " "; } }

Selection Sort:
#include<iostream> using namespace std; void selectionSort(int array[], int size)
{ for (int i = 0; i<size-1;i++) { int min
= i; for (int j = i+1; j<size;j++) {
if(array[min]>array[j]) min = j;
} int temp = array[i]; array[i] = array[min];
array[min] = temp; }
} int main()
{ int array[] = {1,5,6,4,2,3}; int size = sizeof(array)/sizeof(array[0]);
for (int i = 0; i<size;i++) cout << array[i] << " "; }
selectionSort(array, size);
cout << "\nAfter Sorting: "; for (int i = 0; i<size;i++)
{ cout << array[i] << " ";
} }

Coin Change Algorithm: #include<iostream> using namespace std;

void bubbleSort(int coins[], int size)


{ for(int i=0 ; i <size-1 ; i++)
{ for(int j=0 ; j<size-i-1 ; j++)
{ if(coins[j]<coins[j+1])
{ int temp = coins[j]; coins[j] = coins[j+1]; coins[j+1] = temp; } } } }
int main() { int n = 6; int coins[6] = {2,5,3,4,2,5}; int value = 20;
bubbleSort(coins, 6); for(int i = 0; i < n; i++)
{ cout << coins[i] << " "; } cout << endl;
int j=0; while(j!=n && value!=0)
{ if(value>=coins[j])
{ value = value - coins[j]; cout
<< value << endl;
} j++; }
if(value==0) cout << "Exchange Successful\n"; else cout
<< "Exchange Unsuccessful\n";
cout << "Remaining Value: " << value;
}

Fractional Knapsack Algorithm:


#include<iostream> using namespace std;
void sorting(float ratio[],float weight[], float value[], int size)
{ for(int i=0 ; i <size-1 ; i++)
{ for(int j=0 ; j<size-i-1 ; j++)
{ if(ratio[j]<ratio[j+1])
{
swap(ratio[j],ratio[j+1]); swap(weight[j],weight[j+1]);
swap(value[j],value[j+1]); } } } }
int main() { int bagCapacity = 18; int n = 5;
float value[5] = {10, 20, 6, 36, 4}; float weight[5] = {2, 5, 3, 6, 4}; float ratio[5]; float profit
= 0;
for(int i=0; i<n; i++) { ratio[i] = value[i]/weight[i]; //cout << ratio[i] << " ";
}
sorting(ratio,weight,value, 5);
cout << "RATIO " << "WEIGHT " << "VALUE " << endl; for(int i=0; i<n; i++)
{ cout << ratio[i] << "\t" << weight[i] << "\t" << value[i] << " " << endl; }
int j=0;
while(j!=n && bagCapacity!=0)
{ if(bagCapacity>=weight[j])
{ bagCapacity = bagCapacity - weight[j]; profit = profit +
value[j]; j++; } else { profit = profit + (bagCapacity*ratio[j]);
bagCapacity = 0; break; }
} cout << "\nTotal Profit = " << profit << endl; }
Travelling Salesman Problem:
#include <iostream> #define n 4 using namespace std; int main()
{ int data[n][n]={{0,30,20,15},
{30,0,25,35},
{20,25,0,30},
{15,35,30,0}};
int initalIndex=0,current_index=0,totalCost= 0 ; int
visit[n]={1,0,0,0},countVisit=1;
cout<<current_index<<"->";
//cout<<totalCost<<"\n"; while(countVisit!=n)
{ int minValue=10000,minIndex=0; for(int i=0;i<n;i++)
{ if(data[current_index][i]<minValue && data[current_index][i]!=0 &&visit[i]==0 )
{ minValue=data[current_index][i]; minIndex=i;
}
} current_index=minIndex; countVisit=countVisit+1;
visit[minIndex]=1; totalCost=totalCost+minValue;
cout<<current_index<<"->";
} cout<<"\nTotal Cost:"<<totalCost<<endl; return 0;
}

0/1 Knapsack Problem:


#include<iostream>
#define n 3 using
namespace std; int main(){
int wt[n]={1,2,3}; int
val[n]={10,15,30}; int
bag_capacity=6;
int dp[n+1][bag_capacity+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=bag_capacity;j++){ if(i==0 ||
j==0){ dp[i][j]=0; }
else if(wt[n-1]<=j){ dp[i][j]=max(dp[i-1][j],val[n-1]+dp[i-1][j-wt[i-1]]);
} else dp[i][j]=dp[i-1][j]; }
} for(int i=0;i<=n;i++){ for(int j=0;j<=bag_capacity;j++){ cout<<dp[i][j]<<" ";
} cout<<endl; }
cout<<dp[n][bag_capacity];
}

You might also like