#include #include #include #include #include using namespace std; void input(int [], int); int binarySearch(int [], int, int, int); void insertionSort(int [], int); void bubbleSort(int [], int); void swap(int*, int*); void selectionSort(int [], int); void time_it(double, double, string); void modified_bubbleSort(int [], int); void mergeSort(int [], int, int); void merge(int [], int, int, int); void cpu_clock_cycles(double, double); int binarySearch(int a[], int low, int high, int x) { if (high <= low) return (x > a[low])? (low + 1): low; int mid = (low + high)/2; if(x == a[mid]) return mid+1; if(x > a[mid]) return binarySearch(a, mid+1, high, x); return binarySearch(a, low, mid-1, x); } void insertionSort(int a[], int n) { int loc, i, j, selected; for(i=1; i= loc){ a[j+1] = a[j]; j--; } a[j+1] = selected; } } void bubbleSort(int b[], int n) { int i, j; for(i=n-1;i>=0;i--) { for(j=0; j distribution_1_1000(1, 1000); x[i] = distribution_1_1000(random_engine); } } void modified_bubbleSort(int b[], int n) { int i, j; bool swapped; for(i=n-1;i>=0;i--) { swapped = false; for(j=0; j> size; //Dynamic array allocation during runtime to minimize space complexity int*a = new int[size]; int*b = new int[size]; int*c = new int[size]; int*d = new int[size]; int*e = new int[size]; cout <<"Filling random array elements\n"; //Array gets automatically filled with random numbers input(a, size); for(i=0; i < size; i++) e[i] = d[i] = c[i] = b[i] = a[i]; cout <<"Done\n"; cout <<"\nSorting the array using Merge Sort...\n"; start = clock(); mergeSort(e, 0, size-1); end = clock(); delete[] e; time_it(start, end, "Merge"); cpu_clock_cycles(start, end); cout <<"\nSorting the array using Binary Insertion Sort...\n"; start = clock(); insertionSort(a, size); end = clock(); delete[] a; //Time function calculates execution time for sorting time_it(start, end, "Binary Insertion"); cpu_clock_cycles(start, end); cout <<"\nSorting the array using Selection Sort...\n"; start = clock(); selectionSort(c, size); end = clock(); //Array memory is cleared after the array is no longer required delete[] c; time_it(start, end, "Selection"); cpu_clock_cycles(start, end); cout <<"\nSorting the array using Bubble Sort...\n"; start = clock(); bubbleSort(b, size); end = clock(); delete[] b; time_it(start, end, "Bubble"); cpu_clock_cycles(start, end); cout <<"\nSorting the array using Modified Bubble Sort...\n"; start = clock(); modified_bubbleSort(d, size); end = clock(); delete[] d; time_it(start, end, "Modified Bubble"); cpu_clock_cycles(start, end); return 0; }