Data Structure and Algorithm Lab Task 03, Assignments of Data Structures and Algorithms

Data Structure and Algorithm Lab Task 03

Typology: Assignments

2020/2021

Uploaded on 03/16/2021

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LAB # 03 Data Structure & Algorithms Lab
1
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;
}
pf3
pf4
pf5

Partial preview of the text

Download Data Structure and Algorithm Lab Task 03 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

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 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; }

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 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)

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 #include #include using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp;} void bubbleSort(int arr[], int n)//bubble sort function { 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(int arr[], int size) { int i; for (i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } void insertionSort(int arr[], int n)//insertion sort function { 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 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 : ";