Download Implementation of Bubble Sort, Insertion Sort, and Array Initialization in C++ and more Assignments Data Structures and Algorithms in PDF only on Docsity! LAB # 03 Data Structure & Algorithms Lab Task (1): Implement bubble sort as a function which takes a float array and the size of the array as Parameters and returns the array sorted in descending order. #include <iostream> using namespace std; void bubbleSort(float, float ); void swap(float*, float*); void printArray(float [], float ); int main() { float arr[] = {.64, 3.4, 2.5, 1.2, 2.2, .11, 9.0}; float n; n = sizeof(arr)/sizeof(arr[0]); bubbleSort(arr, n); cout<<"Sorted array descending order.: \n"; printArray(arr, n); return 0; } //function diffination void swap(float *xp, float *yp) { float temp = *xp; *xp = *yp; *yp = temp; } void bubbleSort(float arr[], float n) { int i, j; for (i = 0; i < n-1; i++) { for (j = 0; j < n-i-1; j++) if (arr[j] < arr[j+1]) { swap(&arr[j], &arr[j+1]);} } void printArray(float arr[], float size) { int i; for (i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } LAB # 03 Data Structure & Algorithms Lab Task (2): Implement Insertion sort as a function which takes a float array and the size of the array as parameters and returns the array sorted in descending order #include <iostream> using namespace std; void descending_order(float [], float ); void printArray(float [], float ); int main() { system("color f0"); float arr[] = { 5.3, 3.2, 2.5, 3.5, 4.6,6.4 }; float n = sizeof(arr) / sizeof(arr[0]); cout<<"Sorted array descending order.: \n"; descending_order(arr, n); printArray(arr, n); return 0; } // function diffination void descending_order(float arr[], float n) { int i, j; float key; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] < key) { arr[j+1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } void printArray(float arr[], float n) { int i; for (i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } Task (3): Create a function named “InitArray” which takes an integer parameter N for the number of elements in the array and creates a float array of N elements randomly initializing the array with numbers between 0 and 1000. (Note: use rand() function) LAB # 03 Data Structure & Algorithms Lab void print_Array(int arr[], int n) { int i; for (i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } //Main funcution of Program int main() { system("color f0"); srand((unsigned) time(0)); int Array[10]; cout<<"================================================\n"; cout<<"random number between 0 to 1000.\n"; cout<<"================================================\n"; for (int i = 0; i < 10; i++) { int c; Array[i] = ( (rand() % 1000 ) + 1); cout << Array [i]<< ", "; } for(int j=0; j<10; j++) { Array[j]=( (rand() % 1000 ) + 1); int c; cout<<"\n================================================\n"; cout<<"enter the number zero to display result: "; cin>>c; cout<<"================================================\n"; if(c==0) { int n = sizeof(Array)/sizeof(Array[0]); bubbleSort(Array, n); int l = sizeof(Array)/sizeof(Array[0]); insertionSort(Array, l); cout<<"\n================================================\n"; cout<<" bubble Sort array : "; LAB # 03 Data Structure & Algorithms Lab printArray(Array, n); cout<<"**********************************************************************\n"; cout<<"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"; cout<<" inserted Sort array : "; print_Array(Array, l); cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%"; } } return 0; }