-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path105-radix_sort.c
73 lines (58 loc) · 1.29 KB
/
105-radix_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
#include "sort.h"
int get_max_value(int *array, int size);
/**
* radix_sort - sorts an array of integers
* in ascending order using the Radix sort algorithm
*
* @array: array of int.
* @size: size of the array.
*
* Return: max value in the array.
*/
void radix_sort(int *array, size_t size)
{
int *result;
int i;
int digit_place = 1;
int largest_number;
if (array == NULL || size < 2)
return;
result = malloc(size * sizeof(int));
if (!result)
return;
largest_number = get_max_value(array, size);
while ((largest_number / digit_place) > 0)
{
int count[10] = {0};
for (i = 0; i < (int)size; i++)
count[(array[i] / digit_place) % 10]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = size - 1; i >= 0; i--)
{
result[count[(array[i] / digit_place) % 10] - 1] = array[i];
count[(array[i] / digit_place) % 10]--;
}
for (i = 0; i < (int)size; i++)
array[i] = result[i];
print_array(array, size);
digit_place *= 10;
}
free(result);
}
/**
* get_max_value - finds the max value and return it.
* @array: array of int.
* @size: size of the array.
* Return: max value in the array.
*/
int get_max_value(int *array, int size)
{
int max, i;
for (max = array[0], i = 1; i < size; i++)
{
if (array[i] > max)
max = array[i];
}
return (max);
}