



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Data Structure and Algorithm Lab Task 03
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




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
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; }
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
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)
Task (4): Call both functions created in task 1 and task 2 from the main function with the float array in task 3 as parameter to both the functions. Display the sorted array returned by each function. #include
while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j = j - 1; } arr[j + 1] = key; } }
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 : ";