1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include <stdio.h> #include <conio.h> void main() { int arr[6], count[100], final[100]; int i, length = 6, min, max; clrscr(); // get value for array for (i = 0; i < length; i++) { printf("\nEnter the value %d : ", i + 1); scanf("%d", & arr[i]); } // get min, max min = max = arr[0]; for (i = 1; i < length; i++) { if (arr[i] < min) { min = arr[i]; } if (arr[i] > max) max = arr[i]; } // intialize count array with 0 for (i = min; i <= max; i++) { count[i] = 0; } // count each number iteration for (i = 0; i < length; i++) { count[arr[i]] += 1; } // modify count array by applying formula a[i] = a[i] + a[i-1] for (i = (min + 1); i <= max; i++) { count[i] = count[i] + count[i - 1]; } // get the sorted array out of previous calculations in 'final' array for (i = 5; i >= 0; i--) { final[count[arr[i]]] = arr[i]; count[arr[i]] -= 1; } // print sorted array for (i = 1; i <= length; i++) { printf("%d,", final[i]); } getch(); } |
C Code Samples
Merge Sort Program in C
Merge Sort Program Implementation in C [crayon-5a2bba9634f58235955623/]