-
Notifications
You must be signed in to change notification settings - Fork 2
/
gravityBeadSort.c
38 lines (31 loc) · 973 Bytes
/
gravityBeadSort.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
#include "noncomparisonDistribution.h"
// O(1) All beads together, doing a single operation
// O(n**0.5) Realistic physical model
// O(n) Dropping the row of beads
// O(S) S is the sum of all beads
void beadSort(long int *array, int length){
int j,k;
long int sum, *i, max;
char *beads;
for(i = array + 1, max = *array; i < array + length; i++)
if(*i > max)
max = *i;
beads = calloc(1, max * length);
for(k ^= k; k < length; k++) // Set the beads
for(j ^= j; j < *(array + k); j++)
*(beads + k * max + j) = 1;
for(j ^= j; j < max; j++){ // Count how many beads has each position
for(sum = k ^= k; k < length; k++){
sum += *(beads + k * max + j);
*(beads + k * max + j) = 0;
}
for(k = length - sum; k < length; k++) // Set bottom sum beads
*(beads + k * max + j) = 1;
}
for(k ^= k; k < length; k++){
for(j ^= j; j < max && *(beads + k * max + j); j++)
continue;
*(array + k) = j;
}
free(beads);
}