-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathbead_sort.c
99 lines (84 loc) · 1.96 KB
/
bead_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
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @file
* @brief Sorting of array list using [bead
* sort](https://en.wikipedia.org/wiki/Bead_sort)
*/
#include <stdio.h>
#include <stdlib.h>
/**
* @addtogroup sorting Sorting algorithms
* @{
*/
/** Create easy access of elements from a 2D matrix stored in memory as a 1D
* array
*/
#define BEAD(i, j) beads[i * max + j]
/**
* Displays the array, passed to this method
* @param [in] arr array to display
* @param [in] n number of elements in the array
*/
void display(const int *arr, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
/** This is where the sorting of the array takes place
* @param [in,out] a array to be sorted
* @param [in] len Array Size
*/
void bead_sort(int *a, size_t len)
{
int i, j, max, sum;
unsigned char *beads;
for (i = 1, max = a[0]; i < len; i++)
if (a[i] > max)
max = a[i];
beads = calloc(1, max * len);
/* mark the beads */
for (i = 0; i < len; i++)
for (j = 0; j < a[i]; j++) BEAD(i, j) = 1;
for (j = 0; j < max; j++)
{
/* count how many beads are on each post */
for (sum = i = 0; i < len; i++)
{
sum += BEAD(i, j);
BEAD(i, j) = 0;
}
/* mark bottom sum beads */
for (i = len - sum; i < len; i++) BEAD(i, j) = 1;
}
for (i = 0; i < len; i++)
{
for (j = 0; j < max && BEAD(i, j); j++)
;
a[i] = j;
}
free(beads);
}
/** @} */
/** Main function */
int main(int argc, const char *argv[])
{
int n;
printf("Enter size of array:\n");
scanf("%d", &n); // E.g. 8 1 2 3
printf("Enter the elements of the array\n");
int i;
int *arr = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Original array: ");
display(arr, n);
bead_sort(arr, n);
printf("Sorted array: ");
display(arr, n);
free(arr);
return 0;
}