├── LICENSE ├── README.md └── Sorting Algos.cpp /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 9 Sorting Algorithms 2 | 3 | It was a coding assignment given under the Bachelors course **Design & Analysis of Algorithms** in Spring 2015 4 | 5 | 6 | ### Algorithms Implemented: 7 | 1. Bubble Sort 8 | 2. Selection Sort 9 | 3. Insertion Sort 10 | 4. Merge Sort 11 | - Top Down 12 | - Bottom Up 13 | 5. Quick Sort 14 | - Simple Partition 15 | - Hoare Partition 16 | 6. Heap Sort 17 | 7. Count Sort 18 | 8. Radix Sort 19 | 9. Shell Sort 20 | 21 | ### Comparison 22 | Besides just running the sorting algorithms, the program also does performance comparison of two sorting algorithms based on running time. The algorithm takes the following parameters as input from the user: 23 | 24 | 1. Two algorithms for comparison 25 | 2. Total data size 26 | 3. Data range 27 | - Starting value 28 | - Ending value 29 | 4. Initial data size to start with 30 | 5. Number of times the process be repeated (to take avg.) 31 | 32 | -------------------------------------------------------------------------------- /Sorting Algos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | using namespace std::chrono; 16 | 17 | string NAMES[11] = { "Bubble Sort", "Selection Sort", "Insertion Sort", "Merge Sort (Top Down)", " Merge Sort (Bottom Up)", "Quick Sort", "Original Hoare Quick Sort", "Heap Sort", "Count Sort", "Radix Sort", "Shell Sort" }; 18 | 19 | void PlaceCursor(int x, int y) 20 | { 21 | COORD c; 22 | c.X = x; 23 | c.Y = y; 24 | 25 | HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); 26 | SetConsoleCursorPosition(h, c); 27 | } 28 | 29 | int LOG(int x, int base) { 30 | return (int)(log(x) / log(base)); 31 | } 32 | 33 | void COPY_ARRAY(int *arr, int *&temp, int n, int start = 0){ //start has been added for Heap 34 | temp = new int[n + start]; 35 | for (int i = 0; i < n; i++) 36 | temp[i + start] = arr[i]; 37 | 38 | } 39 | 40 | void PRINT(int *arr, int n, int start = 0){ 41 | 42 | cout << endl; 43 | 44 | for (int i = start; i < n + start; i++) 45 | cout << arr[i] << " "; 46 | 47 | cout << endl << endl; 48 | } 49 | 50 | void TIME_PRINT(nanoseconds TIME_SPAN, int choice, int n){ //void TIME_PRINT(nanoseconds TIME_SPAN,int choice,int n){ 51 | 52 | double time = TIME_SPAN.count(); 53 | char micro = -26; 54 | 55 | if (time < 1000) 56 | cout << "\nTime consumed by " << NAMES[choice - 1] << " with " << n << " Data size is " << time << " ns" << endl; 57 | else if (time / 1000 < 1000) 58 | cout << "\nTime consumed by " << NAMES[choice - 1] << " with " << n << " Data size is " << time / 1000 << " " << micro << "s" << endl; 59 | else if (time / 1000000 < 1000) 60 | cout << "\nTime consumed by " << NAMES[choice - 1] << " with " << n << " Data size is " << time / 1000000 << " ms" << endl; 61 | else 62 | cout << "\nTime consumed by " << NAMES[choice - 1] << " with " << n << " Data size is " << time / 1000000000 << " sec." << endl; 63 | } 64 | 65 | int POPULATE(int *&arr){ 66 | 67 | cout << "\n\t\t**************************************************\n"; 68 | cout << "\t\t* POPULATING DATA IN THE ARRAY TO RUN THE ALGOS. *\n"; 69 | cout << "\t\t**************************************************\n\n"; 70 | int a1, a2, n; 71 | cout << "\nEnter Data size: "; 72 | cin >> n; 73 | while (n < 1){ 74 | cout << "Please Enter valid Data Size (i.e. greater that 0) : "; 75 | cin >> n; 76 | } 77 | 78 | cout << "\nEnter Data Range:\n\tFrom: "; 79 | cin >> a1; 80 | cout << "\n\tTo: "; 81 | cin >> a2; 82 | while (a2 < a1){ 83 | cout << "\"To:\" limit must be greater than or equal to \"From:\" limit\n"; 84 | cout << "\n\tTo: "; 85 | cin >> a2; 86 | } 87 | arr = new int[n]; 88 | 89 | mt19937 gen(duration_cast(system_clock::now().time_since_epoch()).count()); 90 | uniform_int_distribution<> dis(a1, a2); 91 | 92 | for (int i = 0; i < n; i++) { 93 | arr[i] = dis(gen); 94 | } 95 | 96 | char filename[50], size_str[10]; 97 | _itoa_s(n, size_str, 10); 98 | 99 | filename[0] = '\0'; 100 | strcat_s(filename, "Data for Algos. (Data Size "); 101 | strcat_s(filename, size_str); 102 | strcat_s(filename, " ).txt"); 103 | 104 | ofstream fout(filename); 105 | 106 | for (int i = 0; i < n; i++){ 107 | fout << arr[i] << ", "; 108 | if (i % 15 == 14) 109 | fout << endl; 110 | } 111 | 112 | cout << "\n\n Data has been populated and placed in the file\n\n"; 113 | return n; 114 | } 115 | 116 | void POPULATE_BY_SIZE(int *&arr, int size,int a1,int a2){ 117 | 118 | arr = new int[size]; 119 | 120 | mt19937 gen(duration_cast(system_clock::now().time_since_epoch()).count()); 121 | uniform_int_distribution<> dis(a1,a2); 122 | 123 | for (int i = 0; i < size; i++) { 124 | arr[i] = dis(gen); 125 | } 126 | } 127 | 128 | void STORE_DATA_IN_FILE(int arr[], int arr_size, int count){ 129 | 130 | char filename[30], size_str[15], count_str[5]; 131 | _itoa_s(arr_size, size_str, 10); 132 | _itoa_s(count + 1, count_str, 10); 133 | 134 | filename[0] = '\0'; 135 | strcat_s(filename, size_str); 136 | strcat_s(filename, " ints Data Chunk #"); 137 | strcat_s(filename, count_str); 138 | strcat_s(filename, ".txt"); 139 | 140 | ofstream fout(filename); 141 | 142 | for (int i = 0; i < arr_size; i++){ 143 | fout << arr[i] << ", "; 144 | if (i % 15 == 14) 145 | fout << endl; 146 | } 147 | } 148 | 149 | void STORE_RESULT_IN_FILE(int arr[], int arr_size, int choice, int start, bool isComparison = false, int count = 0){ 150 | 151 | char filename[100], size_str[15], algoStr[50], count_str[5]; 152 | _itoa_s(arr_size, size_str, 10); 153 | _itoa_s(count + 1, count_str, 10); 154 | 155 | int i; 156 | for (i = 0; i < NAMES[choice - 1].length(); i++) 157 | algoStr[i] = NAMES[choice - 1].at(i); 158 | algoStr[i] = '\0'; 159 | 160 | filename[0] = '\0'; 161 | if (isComparison) 162 | strcat_s(filename, "Comparison "); 163 | strcat_s(filename, "Result of "); 164 | strcat_s(filename, algoStr); 165 | strcat_s(filename, " (with "); 166 | if (isComparison){ 167 | strcat_s(filename, " Data Chunk #"); 168 | strcat_s(filename, count_str); 169 | strcat_s(filename, ").txt"); 170 | } 171 | else{ 172 | strcat_s(filename, size_str); 173 | strcat_s(filename, " Data Size)"); 174 | strcat_s(filename, ".txt"); 175 | } 176 | 177 | ofstream fout(filename); 178 | 179 | for (int i = start; i < arr_size + start; i++){ 180 | fout << arr[i] << ", "; 181 | if (i % 15 == 14) 182 | fout << endl; 183 | } 184 | 185 | } 186 | 187 | void INSERTION_SORT(int *arr, int n){ 188 | 189 | int key, i; 190 | for (int j = 1; j < n; j++){ 191 | key = arr[j]; 192 | i = j - 1; 193 | while (i >= 0 && arr[i]>key){ 194 | arr[i + 1] = arr[i]; 195 | i--; 196 | } 197 | arr[i + 1] = key; 198 | } 199 | } 200 | 201 | void SELECTION_SORT(int *arr, int n){ 202 | 203 | int smallest; 204 | for (int j = 0; j < n - 1; j++){ 205 | smallest = j; 206 | for (int i = j + 1; i < n; i++){ 207 | if (arr[i] < arr[smallest]) 208 | smallest = i; 209 | } 210 | swap(arr[j], arr[smallest]); 211 | } 212 | } 213 | 214 | void BUBBLE_SORT(int *arr, int n){ 215 | 216 | for (int i = 0; i < n - 1; i++){ 217 | for (int j = 0; j < n - i - 1; j++){ 218 | if (arr[j]>arr[j + 1]) 219 | swap(arr[j], arr[j + 1]); 220 | } 221 | } 222 | } 223 | 224 | void MERGE(int *arr, int p, int q, int r){ 225 | 226 | int n1, n2, i, j; 227 | n1 = q - p + 1; 228 | n2 = r - q; 229 | 230 | int *A = new int[n1 + 1]; 231 | 232 | for (int b = 0, x = p; b < n1; b++, x++) 233 | A[b] = arr[x]; 234 | A[n1] = 2147483647; 235 | 236 | int *B = new int[n2 + 1]; 237 | for (int b = 0, x = q + 1; b < n2; b++, x++) 238 | B[b] = arr[x]; 239 | B[n2] = 2147483647; 240 | 241 | i = 0; j = 0; 242 | for (int k = p; k <= r; k++){ 243 | if (A[i] < B[j]) 244 | arr[k] = A[i++]; 245 | else 246 | arr[k] = B[j++]; 247 | } 248 | } 249 | 250 | void MERGE_SORT_TOP_DOWN(int *arr, int p, int r){ 251 | 252 | if (p < r){ 253 | int q = (p + r) / 2; 254 | 255 | MERGE_SORT_TOP_DOWN(arr, p, q); 256 | MERGE_SORT_TOP_DOWN(arr, q + 1, r); 257 | 258 | MERGE(arr, p, q, r); 259 | } 260 | 261 | } 262 | 263 | void MERGE_SORT_BOTTOM_UP(int *arr, int n){ 264 | 265 | int c, d, p, q, r, n1, n2, size; 266 | 267 | for (int i = 1; i < n; i *= 2){ 268 | 269 | size = i; 270 | 271 | for (int j = 0; j < n; j += 2 * i){ 272 | 273 | if (n - j < 2 * size){ 274 | r = n - 1; 275 | q = j - 1; 276 | } 277 | else{ 278 | p = j; 279 | q = p + size - 1; 280 | r = q + size; 281 | } 282 | 283 | n1 = q - p + 1; 284 | n2 = r - q; 285 | 286 | int *A = new int[n1 + 1]; 287 | 288 | for (int b = 0, x = p; b < n1; b++, x++) 289 | A[b] = arr[x]; 290 | A[n1] = 2147483647; 291 | int *B = new int[n2 + 1]; 292 | for (int b = 0, x = q + 1; b < n2; b++, x++) 293 | B[b] = arr[x]; 294 | B[n2] = 2147483647; 295 | c = 0; 296 | d = 0; 297 | for (int b = p; b <= r; b++){ 298 | if (A[c] < B[d]) 299 | arr[b] = A[c++]; 300 | else 301 | arr[b] = B[d++]; 302 | } 303 | 304 | delete A; 305 | delete B; 306 | } 307 | 308 | } 309 | } 310 | 311 | int PARTITION(int *arr, int p, int r){ 312 | int x = arr[r]; 313 | int i = p - 1; 314 | for (int j = p; j <= r - 1; j++){ 315 | if (arr[j] <= x){ 316 | i++; 317 | swap(arr[i], arr[j]); 318 | } 319 | } 320 | swap(arr[i + 1], arr[r]); 321 | return i + 1; 322 | } 323 | 324 | void QUICK_SORT(int *arr, int p, int r){ 325 | 326 | if (p < r){ 327 | int q = PARTITION(arr, p, r); 328 | QUICK_SORT(arr, p, q - 1); 329 | QUICK_SORT(arr, q + 1, r); 330 | } 331 | } 332 | 333 | int HOARE_PARTITION(int *arr, int p, int r){ 334 | 335 | int x = arr[p]; 336 | int i = p - 1; 337 | int j = r + 1; 338 | while (true){ 339 | 340 | do{ 341 | j--; 342 | } while (arr[j] > x); 343 | 344 | do{ 345 | i++; 346 | } while (arr[i] < x); 347 | if (i < j) 348 | swap(arr[i], arr[j]); 349 | else 350 | return j; 351 | } 352 | } 353 | 354 | void HOARE_QUICK_SORT(int *arr, int p, int r){ 355 | 356 | if (p < r){ 357 | int q = HOARE_PARTITION(arr, p, r); 358 | HOARE_QUICK_SORT(arr, p, q); 359 | HOARE_QUICK_SORT(arr, q + 1, r); 360 | } 361 | } 362 | 363 | void MAX_HEAPIFY(int* arr, int Heapsize, int i) 364 | { 365 | int LEFT = 2 * i; 366 | int RIGHT = 2 * i + 1; 367 | int largest; 368 | 369 | if (LEFT <= Heapsize && arr[LEFT]>arr[i]) 370 | largest = LEFT; 371 | else 372 | largest = i; 373 | 374 | if (RIGHT <= Heapsize && arr[RIGHT] > arr[largest]) 375 | largest = RIGHT; 376 | 377 | if (largest != i) 378 | { 379 | swap(arr[i], arr[largest]); 380 | MAX_HEAPIFY(arr, Heapsize, largest); 381 | } 382 | } 383 | 384 | void BUILD_MAX_HEAP(int* arr, int size) 385 | { 386 | for (int i = size / 2; i >= 1; i--) 387 | MAX_HEAPIFY(arr, size, i); 388 | } 389 | 390 | void HEAP_SORT(int* arr, int size) 391 | { 392 | int HeapSize = size; 393 | 394 | BUILD_MAX_HEAP(arr, size); 395 | for (int i = size; i >= 2; i--){ 396 | swap(arr[1], arr[i]); 397 | HeapSize--; 398 | MAX_HEAPIFY(arr, HeapSize, 1); 399 | } 400 | 401 | } 402 | 403 | void COUNT_SORT(int *arr, int n){ 404 | 405 | int min=0,max = 0, C_size, *C, *B, sum,temp; 406 | for (int i = 1; i < n; i++){ 407 | if (arr[i]arr[max]) 410 | max = i; 411 | 412 | } 413 | temp = arr[min]; 414 | temp = temp*(-1); 415 | 416 | if (temp>arr[max]) 417 | C_size = 2*temp + 1; 418 | else 419 | C_size = 2 * arr[max]+ 1; 420 | 421 | C = new int[C_size]; 422 | 423 | for (int i = 0; i < C_size; i++) 424 | C[i] = 0; 425 | 426 | for (int i = 0; i < n; i++) 427 | C[(C_size / 2) + arr[i]]++; 428 | 429 | sum = 0; 430 | 431 | for (int i = 1; i < C_size; i++) 432 | C[i] += C[i - 1]; 433 | 434 | B = new int[n]; 435 | 436 | for (int i = n - 1; i >= 0; i--){ 437 | B[C[(C_size / 2) + arr[i]] - 1] = arr[i]; 438 | C[(C_size / 2) + arr[i]]--; 439 | } 440 | 441 | for (int i = 0; i < n; i++) //Copy the sorted data into the original array 442 | arr[i] = B[i]; 443 | } 444 | 445 | void RADIX_SORT(int *arr, int n){ 446 | 447 | int max, max_digits, C_size, *C, *B, sum;; 448 | max = 0; 449 | for (int i = 1; i < n; i++){ //Finding max. element in the array to see the maximum no. of digits 450 | if (arr[i]>arr[max]) 451 | max = i; 452 | } 453 | char str[15]; 454 | _itoa_s(arr[max],str, 10); 455 | max_digits = strlen(str); 456 | 457 | for (int i = 1; i <= max_digits; i++){ 458 | int m = pow(10,i); 459 | 460 | max = 0; 461 | for (int i = 1; i < n; i++){ 462 | if (arr[i] % m / (m / 10)>arr[max] % m / (m / 10)) 463 | max = i; 464 | } 465 | 466 | C_size = arr[max] % m / (m / 10) + 1; 467 | C = new int[C_size]; 468 | 469 | for (int i = 0; i < C_size; i++) 470 | C[i] = 0; 471 | 472 | for (int i = 0; i < n; i++) 473 | C[arr[i] % m / (m / 10)]++; 474 | 475 | 476 | sum = 0; 477 | 478 | for (int i = 1; i < C_size; i++) 479 | C[i] += C[i - 1]; 480 | 481 | B = new int[n]; 482 | 483 | for (int i = n - 1; i >= 0; i--){ 484 | B[C[arr[i] % m / (m / 10)]-1] = arr[i]; 485 | C[arr[i] % m / (m / 10)]--; 486 | } 487 | 488 | for (int i = 0; i < n; i++) //Copy the sorted data into the original array 489 | arr[i] = B[i]; 490 | } 491 | 492 | } 493 | 494 | void SHELL_SORT(int *arr, int n){ 495 | 496 | int h = 1; 497 | while (h < (n / 3)) 498 | h = 3 * h + 1; 499 | while (h >= 1){ 500 | for (int i = h; i < n; i++){ 501 | for (int j = i; j >= h && (arr[j] < arr[j - h]); j -= h) 502 | swap(arr[j], arr[j - h]); 503 | } 504 | h /= 3; 505 | } 506 | } 507 | 508 | void COMPARISON(){ 509 | 510 | int c1, c2,a1,a2, count, size, curr_size, ini_size, repeatCount, *data,*arr1, *arr2; 511 | double time1, time2; 512 | 513 | high_resolution_clock::time_point STARTING_TIME, ENDING_TIME; 514 | nanoseconds TIME_SPAN; 515 | vector Times1; 516 | vector Times2; 517 | 518 | system("cls"); 519 | cout << "\nSelect Two Algos.\n\t1) Bubble Sort\n\t2) Selection Sort\n\t3) Insertion Sort\n\t4) Merge Sort (Top Down)\n\t5) Merge Sort (Bottom Up)\n\t6) Quick Sort\n\t7) Original Hoare Quick Sort\n\t8) Heap Sort\n\t9) Count Sort\n\t10) Radix Sort (only +ve integers)\n\t11) Shell Sort\n\nEnter Option #1: "; 520 | cin >> c1; 521 | cout << "\n\nEnter Option #2: "; 522 | cin >> c2; 523 | cout << "\n\nEnter Total Data Size: "; 524 | cin >> size; 525 | cout << "\nEnter Data Range:\n\tFrom: "; 526 | cin >> a1; 527 | cout << "\n\tTo: "; 528 | cin >> a2; 529 | while (a2 < a1){ 530 | cout << "\"To:\" limit must be greater than or equal to \"From:\" limit\n"; 531 | cout << "\n\tTo: "; 532 | cin >> a2; 533 | } 534 | cout << "\n\nEnter Initial Data Size to start with: "; 535 | cin >> ini_size; 536 | cout << "\n\nFor How many times you want to repeat the process (to take avg.)? "; 537 | cin >> repeatCount; 538 | 539 | system("cls"); 540 | cout << "\n\n\n\n\t\t\t\tPROCESSING..."; 541 | 542 | nanoseconds ZERO = nanoseconds::duration(0); 543 | for (int i = 0; i <= LOG((size / ini_size), 2); i++){ 544 | Times1.push_back(ZERO); 545 | Times2.push_back(ZERO); 546 | } 547 | 548 | curr_size = ini_size; 549 | count = 0; 550 | while (curr_size < size){ 551 | 552 | POPULATE_BY_SIZE(data, curr_size, a1, a2); 553 | 554 | 555 | if (c1 == 8) 556 | COPY_ARRAY(data, arr1, curr_size, 1); 557 | else 558 | COPY_ARRAY(data, arr1, curr_size); 559 | 560 | if (c2 == 8) 561 | COPY_ARRAY(data, arr2, curr_size, 1); 562 | else 563 | COPY_ARRAY(data, arr2, curr_size); 564 | 565 | STARTING_TIME = high_resolution_clock::now(); 566 | 567 | switch (c1){ 568 | 569 | case 1: 570 | BUBBLE_SORT(arr1, curr_size); 571 | break; 572 | 573 | case 2: 574 | SELECTION_SORT(arr1, curr_size); 575 | break; 576 | 577 | case 3: 578 | 579 | INSERTION_SORT(arr1, curr_size); 580 | break; 581 | 582 | case 4: 583 | MERGE_SORT_TOP_DOWN(arr1, 0, curr_size - 1); 584 | break; 585 | 586 | case 5: 587 | MERGE_SORT_BOTTOM_UP(arr1, curr_size); 588 | break; 589 | 590 | case 6: 591 | QUICK_SORT(arr1, 0, curr_size - 1); 592 | break; 593 | 594 | case 7: 595 | HOARE_QUICK_SORT(arr1, 0, curr_size - 1); 596 | break; 597 | 598 | case 8: 599 | HEAP_SORT(arr1, curr_size); 600 | break; 601 | 602 | case 9: 603 | COUNT_SORT(arr1, curr_size); 604 | break; 605 | 606 | case 10: 607 | RADIX_SORT(arr1, curr_size); 608 | break; 609 | 610 | case 11: 611 | SHELL_SORT(arr1, curr_size); 612 | break; 613 | } 614 | 615 | ENDING_TIME = high_resolution_clock::now(); 616 | TIME_SPAN = duration_cast(ENDING_TIME - STARTING_TIME); 617 | Times1.at(LOG((curr_size / ini_size), 2)).operator+=(TIME_SPAN); 618 | 619 | STARTING_TIME = high_resolution_clock::now(); 620 | 621 | switch (c2){ 622 | 623 | case 1: 624 | BUBBLE_SORT(arr2, curr_size); 625 | break; 626 | 627 | case 2: 628 | SELECTION_SORT(arr2, curr_size); 629 | break; 630 | 631 | case 3: 632 | 633 | INSERTION_SORT(arr2, curr_size); 634 | break; 635 | 636 | case 4: 637 | MERGE_SORT_TOP_DOWN(arr2, 0, curr_size - 1); 638 | break; 639 | 640 | case 5: 641 | MERGE_SORT_BOTTOM_UP(arr2, curr_size); 642 | break; 643 | 644 | case 6: 645 | QUICK_SORT(arr2, 0, curr_size - 1); 646 | break; 647 | 648 | case 7: 649 | HOARE_QUICK_SORT(arr2, 0, curr_size - 1); 650 | break; 651 | 652 | case 8: 653 | HEAP_SORT(arr2, curr_size); 654 | break; 655 | 656 | case 9: 657 | COUNT_SORT(arr2, curr_size); 658 | break; 659 | 660 | case 10: 661 | RADIX_SORT(arr2, curr_size); 662 | break; 663 | 664 | case 11: 665 | SHELL_SORT(arr2, curr_size); 666 | break; 667 | } 668 | 669 | ENDING_TIME = high_resolution_clock::now(); 670 | TIME_SPAN = duration_cast(ENDING_TIME - STARTING_TIME); 671 | Times2.at(LOG((curr_size / ini_size), 2)).operator+=(TIME_SPAN); 672 | 673 | if (LOG((curr_size / ini_size), 2)<3){ 674 | STORE_DATA_IN_FILE(data, curr_size, count); 675 | 676 | if (c1==8) 677 | STORE_RESULT_IN_FILE(arr1, curr_size, c1,1,true,count); 678 | else 679 | STORE_RESULT_IN_FILE(arr1, curr_size, c1, 0, true, count); 680 | delete arr1; 681 | 682 | if (c2 == 8) 683 | STORE_RESULT_IN_FILE(arr2, curr_size, c2, 1, true, count); 684 | else 685 | STORE_RESULT_IN_FILE(arr2, curr_size, c2, 0, true, count); 686 | delete arr2; 687 | } 688 | curr_size *= 2; 689 | 690 | if (curr_size > size && ++count < repeatCount) 691 | curr_size = ini_size; 692 | 693 | 694 | } 695 | 696 | char micro = -26; 697 | double time; 698 | system("cls"); 699 | cout << "Data Size"; 700 | PlaceCursor(16, 0); 701 | cout << NAMES[c1 - 1]; 702 | PlaceCursor(50, 0); 703 | cout<< NAMES[c2 - 1] << "\n__________________________________________________________________________\n"; 704 | for (int i = 0; i <= LOG((size / ini_size), 2); i++){ 705 | PlaceCursor(2, 2 * i + 2); 706 | cout << pow(2, i)*ini_size; 707 | 708 | time= Times1.at(i).count(); 709 | PlaceCursor(20, 2 * i + 2); 710 | if (time<1000) 711 | cout << time<<" ns"; 712 | 713 | else if (time / 1000 < 1000) 714 | cout << time/1000<< micro << "s" << endl; 715 | 716 | else if (time / 1000000 < 1000) 717 | cout << time / 1000000 << " ms" << endl; 718 | 719 | else 720 | cout << time / 1000000000 << " sec" << endl; 721 | 722 | cout << " "; 723 | 724 | time = Times2.at(i).count(); 725 | PlaceCursor(52, 2 * i + 2); 726 | if (time<1000) 727 | cout << time << " ns"; 728 | 729 | else if (time / 1000 < 1000) 730 | cout << time / 1000 << micro << "s" << endl; 731 | 732 | else if (time / 1000000 < 1000) 733 | cout << time / 1000000 << " ms" << endl; 734 | 735 | else 736 | cout << time / 1000000000 << " sec" << endl; 737 | } 738 | cout << "\n\n\n* The Data used in the whole process have been populated in the files\n"; 739 | 740 | } 741 | 742 | int main(){ 743 | 744 | int a1, a2, n, choice, *arr = nullptr, *temp = nullptr; 745 | double time; 746 | high_resolution_clock::time_point STARTING_TIME, ENDING_TIME; 747 | 748 | n = 0; 749 | n = POPULATE(arr); 750 | system("pause"); 751 | system("cls"); 752 | 753 | while (true){ 754 | 755 | cout << "\nWhat do you want to do?\t\t\t\t(0) for EXIT\n\t1) Bubble Sort\n\t2) Selection Sort\n\t3) Insertion Sort\n\t4) Merge Sort (Top Down)\n\t5) Merge Sort (Bottom Up)\n\t6) Quick Sort\n\t7) Original Hoare Quick Sort\n\t8) Heap Sort\n\t9) Count Sort\n\t10) Radix Sort (only +ve integers)\n\t11) Shell Sort\n\t12) Refresh Data\n\t13) Comparison of 2 Algos.\n\nEnter Option: "; 756 | cin >> choice; 757 | if (choice == 0) 758 | break; 759 | 760 | if (choice==8) 761 | COPY_ARRAY(arr, temp, n, 1); 762 | else if (choice>=1 && choice<=11) 763 | COPY_ARRAY(arr, temp, n); 764 | 765 | 766 | STARTING_TIME = high_resolution_clock::now(); 767 | switch (choice){ 768 | 769 | case 1: 770 | BUBBLE_SORT(temp, n); 771 | break; 772 | 773 | case 2: 774 | SELECTION_SORT(temp, n); 775 | break; 776 | 777 | case 3: 778 | INSERTION_SORT(temp, n); 779 | break; 780 | 781 | case 4: 782 | MERGE_SORT_TOP_DOWN(temp, 0, n - 1); 783 | break; 784 | 785 | case 5: 786 | MERGE_SORT_BOTTOM_UP(temp, n); 787 | break; 788 | 789 | case 6: 790 | QUICK_SORT(temp, 0, n - 1); 791 | break; 792 | 793 | case 7: 794 | HOARE_QUICK_SORT(temp, 0, n - 1); 795 | break; 796 | 797 | case 8: 798 | HEAP_SORT(temp, n); 799 | break; 800 | 801 | case 9: 802 | COUNT_SORT(temp, n); 803 | break; 804 | 805 | case 10: 806 | RADIX_SORT(temp,n); 807 | break; 808 | 809 | case 11: 810 | SHELL_SORT(temp, n); 811 | break; 812 | 813 | case 12: 814 | delete arr; 815 | n = POPULATE(arr); 816 | break; 817 | 818 | case 13: 819 | COMPARISON(); 820 | break; 821 | 822 | } 823 | 824 | if (choice>=1 && choice <=11){ 825 | ENDING_TIME = high_resolution_clock::now(); 826 | nanoseconds TIME_SPAN = duration_cast(ENDING_TIME - STARTING_TIME); 827 | 828 | TIME_PRINT(TIME_SPAN,choice,n); 829 | if (choice==8) 830 | STORE_RESULT_IN_FILE(temp,n,choice,1); 831 | else 832 | STORE_RESULT_IN_FILE(temp, n,choice,0); 833 | delete temp; 834 | 835 | } 836 | 837 | cout << endl << endl; 838 | system("pause"); 839 | system("cls"); 840 | } 841 | return 0; 842 | } 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | --------------------------------------------------------------------------------