-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102-counting_sort.c
87 lines (76 loc) · 1.74 KB
/
102-counting_sort.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "sort.h"
/**
* counting_sort - sorts an array of int using counting sort
* @array: array of elements to be sorted
* @size: size of the array to be sorted
*/
void counting_sort(int *array, size_t size);
void counting_sort(int *array, size_t size)
{
int max_num = 0;
size_t i;
int *count, j;
int *sorted_arr;
/*create array count to store counts of each unique*/
/* number in array*/
/*determine the size of count array*/
if (size < 2)
{
return;
}
for (i = 0; i < size; i++)
{
if (array[i] > max_num)
{
max_num = array[i];
}
}
/*dynamically create count array of size max_num*/
/*max + 1 cause sequence can start from 0 not 1 to max_num*/
count = malloc((max_num + 1) * sizeof(int));
/*handle situation where malloc fails*/
if (count == NULL)
{
printf("Not sufficient Memory malloc failed");
return;
}
/*initialize all elements of count array to 0*/
for (j = 0; j <= max_num; j++)
{
count[i] = 0;
}
/*count the number of elements in array and store in count array*/
for (i = 0; i < size; i++)
{
count[array[i]] += 1;
}
/*transform count array to hold the starting position where*/
/*each element will be inserted by doing cummulative sum*/
for (j = 1 ; j <= max_num; j++)
{
count[j] += count[j - 1];
}
/*printing the count array*/
print_array(count, max_num + 1);
/*creating a sorted array */
sorted_arr = malloc(size * sizeof(int));
if (sorted_arr == NULL)
{
printf("Not sufficient Memory malloc failed");
free(count);
return;
}
for (i = 0; i < size; i++)
{
sorted_arr[count[array[i]] - 1] = array[i];
count[array[i]] -= 1;
}
/*copy sorted_arr back to original array*/
for (i = 0; i < size; i++)
{
array[i] = sorted_arr[i];
}
/*free memory*/
free(sorted_arr);
free(count);
}