|
| 1 | +#include<iostream> |
| 2 | +#include<string> |
| 3 | +#include<ctime> |
| 4 | +#include<iomanip> |
| 5 | +#include<random> |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +void input(int [], int); |
| 9 | +int binarySearch(int [], int, int, int); |
| 10 | +void insertionSort(int [], int); |
| 11 | +void bubbleSort(int [], int); |
| 12 | +void swap(int*, int*); |
| 13 | +void selectionSort(int [], int); |
| 14 | +void time_it(double, double, string); |
| 15 | +void modified_bubbleSort(int [], int); |
| 16 | +void mergeSort(int [], int, int); |
| 17 | +void merge(int [], int, int, int); |
| 18 | +void cpu_clock_cycles(double, double); |
| 19 | + |
| 20 | +int binarySearch(int a[], int low, int high, int x) { |
| 21 | + if (high <= low) |
| 22 | + return (x > a[low])? (low + 1): low; |
| 23 | + int mid = (low + high)/2; |
| 24 | + if(x == a[mid]) |
| 25 | + return mid+1; |
| 26 | + if(x > a[mid]) |
| 27 | + return binarySearch(a, mid+1, high, x); |
| 28 | + return binarySearch(a, low, mid-1, x); |
| 29 | +} |
| 30 | + |
| 31 | +void insertionSort(int a[], int n) { |
| 32 | + int loc, i, j, selected; |
| 33 | + for(i=1; i<n; i++) { |
| 34 | + selected = a[i]; |
| 35 | + j = i - 1; |
| 36 | + loc = binarySearch(a, 0, j, selected); |
| 37 | + while(j >= loc){ |
| 38 | + a[j+1] = a[j]; |
| 39 | + j--; |
| 40 | + } |
| 41 | + a[j+1] = selected; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +void bubbleSort(int b[], int n) { |
| 46 | + int i, j; |
| 47 | + for(i=n-1;i>=0;i--) { |
| 48 | + for(j=0; j<i; j++) { |
| 49 | + if(b[j+1] < b[j]) { |
| 50 | + swap(b+j,b+j+1); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void swap(int*a, int*b) { |
| 57 | + int temp = *a; |
| 58 | + *a = *b; |
| 59 | + *b = temp; |
| 60 | +} |
| 61 | + |
| 62 | +void selectionSort(int c[], int n) { |
| 63 | + int min, i, j; |
| 64 | + for(i=0; i<n-1; i++) { |
| 65 | + for(j=i+1; j<n; j++) { |
| 66 | + if(c[j] < c[i]){ |
| 67 | + swap(c+i,c+j); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void time_it(double strt, double lst, string str) { |
| 74 | + double time_taken = double(lst - strt)/ double(CLOCKS_PER_SEC); |
| 75 | + cout <<"Time taken by "<< str <<" Sort is: "<< fixed << time_taken << setprecision(3); |
| 76 | + cout <<" sec(s)\n"; |
| 77 | +} |
| 78 | + |
| 79 | +void cpu_clock_cycles(double _2, double _1) { |
| 80 | + cout <<"CPU cycles taken: " << _1 - _2 << endl; |
| 81 | +} |
| 82 | + |
| 83 | +void input(int x[], int n) { |
| 84 | + int i; |
| 85 | + random_device random_device; |
| 86 | + mt19937 random_engine(random_device()); |
| 87 | + for(i=0; i<n; i++) { |
| 88 | + uniform_int_distribution<int> distribution_1_1000(1, 1000); |
| 89 | + x[i] = distribution_1_1000(random_engine); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +void modified_bubbleSort(int b[], int n) { |
| 94 | + int i, j; |
| 95 | + bool swapped; |
| 96 | + for(i=n-1;i>=0;i--) { |
| 97 | + swapped = false; |
| 98 | + for(j=0; j<i; j++) { |
| 99 | + if(b[j+1] < b[j]) { |
| 100 | + swap(b+j,b+j+1); |
| 101 | + swapped = true; |
| 102 | + } |
| 103 | + } |
| 104 | + if(!swapped) break; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void mergeSort(int a[], int lo, int hi) { |
| 109 | + int mid; |
| 110 | + if(lo < hi) { |
| 111 | + mid = (lo + hi)/2; |
| 112 | + mergeSort(a, lo, mid); |
| 113 | + mergeSort(a, mid+1, hi); |
| 114 | + merge(a, lo, hi, mid); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +void merge(int a[], int l, int h, int m) { |
| 119 | + int i, j, k, temp[h-l+1]; |
| 120 | + i = l; |
| 121 | + j = m + 1; |
| 122 | + k = 0; |
| 123 | + while(i <= m && j <= h) { |
| 124 | + if(a[i] < a[j]) |
| 125 | + temp[k++] = a[i++]; |
| 126 | + else |
| 127 | + temp[k++] = a[j++]; |
| 128 | + } |
| 129 | + |
| 130 | + while(i <= m) |
| 131 | + temp[k++] = a[i++]; |
| 132 | + |
| 133 | + while(j <= h) |
| 134 | + temp[k++] = a[j++]; |
| 135 | + |
| 136 | + for(i=l, j=0; i<=h; i++, j++) |
| 137 | + a[i] = temp[j]; |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +int main() { |
| 142 | + clock_t start, end; |
| 143 | + int size, elem, i; |
| 144 | + cout <<"Enter the size of the array\n"; |
| 145 | + cin >> size; |
| 146 | + //Dynamic array allocation during runtime to minimize space complexity |
| 147 | + int*a = new int[size]; |
| 148 | + int*b = new int[size]; |
| 149 | + int*c = new int[size]; |
| 150 | + int*d = new int[size]; |
| 151 | + int*e = new int[size]; |
| 152 | + cout <<"Filling random array elements\n"; |
| 153 | + |
| 154 | + //Array gets automatically filled with random numbers |
| 155 | + input(a, size); |
| 156 | + for(i=0; i < size; i++) |
| 157 | + e[i] = d[i] = c[i] = b[i] = a[i]; |
| 158 | + cout <<"Done\n"; |
| 159 | + |
| 160 | + cout <<"\nSorting the array using Merge Sort...\n"; |
| 161 | + start = clock(); |
| 162 | + mergeSort(e, 0, size-1); |
| 163 | + end = clock(); |
| 164 | + delete[] e; |
| 165 | + time_it(start, end, "Merge"); |
| 166 | + cpu_clock_cycles(start, end); |
| 167 | + |
| 168 | + |
| 169 | + cout <<"\nSorting the array using Binary Insertion Sort...\n"; |
| 170 | + start = clock(); |
| 171 | + insertionSort(a, size); |
| 172 | + end = clock(); |
| 173 | + delete[] a; |
| 174 | + //Time function calculates execution time for sorting |
| 175 | + time_it(start, end, "Binary Insertion"); |
| 176 | + cpu_clock_cycles(start, end); |
| 177 | + |
| 178 | + cout <<"\nSorting the array using Selection Sort...\n"; |
| 179 | + start = clock(); |
| 180 | + selectionSort(c, size); |
| 181 | + end = clock(); |
| 182 | + //Array memory is cleared after the array is no longer required |
| 183 | + delete[] c; |
| 184 | + time_it(start, end, "Selection"); |
| 185 | + cpu_clock_cycles(start, end); |
| 186 | + |
| 187 | + |
| 188 | + cout <<"\nSorting the array using Bubble Sort...\n"; |
| 189 | + start = clock(); |
| 190 | + bubbleSort(b, size); |
| 191 | + end = clock(); |
| 192 | + delete[] b; |
| 193 | + time_it(start, end, "Bubble"); |
| 194 | + cpu_clock_cycles(start, end); |
| 195 | + |
| 196 | + cout <<"\nSorting the array using Modified Bubble Sort...\n"; |
| 197 | + start = clock(); |
| 198 | + modified_bubbleSort(d, size); |
| 199 | + end = clock(); |
| 200 | + delete[] d; |
| 201 | + time_it(start, end, "Modified Bubble"); |
| 202 | + cpu_clock_cycles(start, end); |
| 203 | + |
| 204 | + return 0; |
| 205 | +} |
0 commit comments