Sorts the target array by dividing it into a variable number of internally sorted piles then merge the piles
22 {
23
24 int* *piles = (
int* *)
malloc(
sizeof(
int*) * length);
25 for (int i = 0; i < length; ++i) {
26 piles[i] =
malloc(
sizeof(
int) * length);
27 }
28
29
30
31 int *pileSizes = (
int*)
calloc(length,
sizeof(
int));
32
33
34
35 piles[0][0] = array[0];
36 int pileCount = 1;
37
38 for (int i = 1; i < length; ++i) {
39
40 int flag = 1;
41
42 for (int j = 0; j < pileCount; ++j) {
43 if(piles[j][pileSizes[j]] > array[i]) {
44
45 piles[j][pileSizes[j] + 1] = array[i];
46 pileSizes[j]++;
47 flag--;
48 break;
49 }
50 }
51
52 if(flag) {
53
54 piles[pileCount][0] = array[i];
55 pileCount++;
56 }
57 }
58
59
60 int min, minLocation;
61
62 for (int i = 0; i < length; ++i) {
63
64
65
66
67 for (int j = 0; j < pileCount; ++j) {
68 if(pileSizes[j] < 0) {
69 continue;
70 }
71 min = piles[j][pileSizes[j]];
72 minLocation = j;
73 break;
74 }
75
76 for (int j = 0; j < pileCount; ++j) {
77 if(pileSizes[j] < 0) {
78 continue;
79 }
80 if(piles[j][pileSizes[j]] < min) {
81 min = piles[j][pileSizes[j]];
82 minLocation = j;
83 }
84 }
85
86 array[i] = min;
87 pileSizes[minLocation]--;
88 }
89
90
92 for (int i = 0; i < length; ++i) {
94 }
96}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition malloc_dbg.h:22