-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpatienceSort.c
47 lines (44 loc) · 1.24 KB
/
patienceSort.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
#include "insertion.h"
// Worst case performance O(n log n)
// Best case performance O(n)
// Average case performance O(n log n)
void patienceSort(long int *array, int length){
int i,j,row;
long int *count = calloc(sizeof(long int), length), *sorted = malloc(length * sizeof(long int)), **decks = malloc(length * sizeof(long int *)), min;
for(i ^= i; i < length; i++)
*(decks + i) = malloc(length * sizeof(long int));
for(i ^= i; i < length; i++){
for(j ^= j; j < length; j++){
if(*(count + j) == 0 || (*(count + j) > 0 && *(*(decks + j) + *(count + j) - 1) >= *(array + i))){
*(*(decks + j) + *(count +j)) = *(array + i);
(*(count + j))++;
break;
}
}
}
min = *(*decks + *count - 1);
row = 0;
for(i ^= i; i < length; i++){
for(j ^= j; j < length; j++){
if(*(count + j) > 0 && *(*(decks + j) + *(count + j) - 1) < min){
min = *(*(decks + j) + *(count + j) - 1);
row = j;
}
}
*(sorted + i) = min;
(*(count + row))--;
for(j ^= j; j < length; j++)
if(*(count + j) > 0){
min = *(*(decks + j) + *(count + j) - 1);
row = j;
break;
}
}
for(i ^= i; i < length; i++)
*(array + i) = *(sorted + i);
free(sorted);
for(i ^= i; i < length; i++)
free(*(decks + i));
free(decks);
free(count);
}