Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* list, number of elements, minimum value in list, maximum value un list */
- void counting_sort_mm(int *list, int n, int min, int max)
- {
- int i, j, z;
- int range = max - min + 1;
- int *count = malloc(range * sizeof(*list));
- for(i = 0; i < range; i++) count[i] = 0;
- for(i = 0; i < n; i++) count[ list[i] - min ]++;
- for(i = min, z = 0; i <= max; i++) {
- for(j = 0; j < count[i - min]; j++) {
- list[z++] = i;
- }
- }
- free(count);
- }
- /* list, number of elements */
- void counting_sort(int *list, int n)
- {
- int i, min, max;
- min = max = list[0];
- for(i=1; i < n; i++) {
- if ( list[i] < min ) {
- min = list[i];
- } else if ( list[i] > max ) {
- max = list[i];
- }
- }
- counting_sort_mm(list, n, min, max);
- }
Advertisement
Add Comment
Please, Sign In to add comment