-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path105-radix_sort.c
executable file
·86 lines (70 loc) · 1.88 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
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "sort.h"
int get_max(int *array, int size);
void radix_counting_sort(int *array, size_t size, int sig, int *buffer);
void radix_sort(int *array, size_t size);
/**
* get_max - Gets the maximum value in an array of integers
* @array: Array of integers
* @size: Array size
*
* Return: Maximum integer in the array
*/
int get_max(int *array, int size)
{
int maximum, x;
for (maximum = array[0], x = 1; x < size; x++)
{
if (array[x] > maximum)
maximum = array[x];
}
return (maximum);
}
/**
* radix_counting_sort - Sort the significant digits of an array of integers
* in ascending order using the counting sort algorithm.
* @array: An array of integers.
* @size: The size of the array.
* @sig: The significant digit to sort on.
* @buffer: A buffer to store the sorted array.
*/
void radix_counting_sort(int *array, size_t size, int sig, int *buffer)
{
int count[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
size_t x;
for (x = 0; x < size; x++)
count[(array[x] / sig) % 10] += 1;
for (x = 0; x < 10; x++)
count[x] += count[x - 1];
for (x = size - 1; (int)x >= 0; x--)
{
buffer[count[(array[x] / sig) % 10] - 1] = array[x];
count[(array[x] / sig) % 10] -= 1;
}
for (x = 0; x < size; x++)
array[x] = buffer[x];
}
/**
* radix_sort - Sort an array of integers in ascending
* order using the Radix sort algorithm.
* @array: Array of integers
* @size: Array size
*
* Description: Implementing the LSD radix sort algorithm. Prints
* the array after each significant digit increase
*/
void radix_sort(int *array, size_t size)
{
int max, sig, *buffer;
if (array == NULL || size < 2)
return;
buffer = malloc(sizeof(int) * size);
if (buffer == NULL)
return;
max = get_max(array, size);
for (sig = 1; max / sig > 0; sig *= 10)
{
radix_counting_sort(array, size, sig, buffer);
print_array(array, size);
}
free(buffer);
}