1 // See LICENSE for license details.
6 n_squared_sort (float * value
, int * index
, int len
)
10 for (i
= 0; i
< len
-1; i
++)
12 for (j
= 0; j
< len
-1; j
++)
14 if (value
[j
] > value
[j
+1])
20 value
[j
] = value
[j
+1];
24 index
[j
] = index
[j
+1];
34 extern void* fake_malloc_radix(size_t size
);
37 radix_sort_tuples (int * value
, int * index
, int len
, int radix_bits
)
41 int numBuckets
= 1 << radix_bits
;
42 int bitMask
= numBuckets
- 1;
45 int * buckets
= fake_malloc_radix ((numBuckets
+ 2) * sizeof(int));
46 int * copy1_value
= fake_malloc_radix (sizeof(int) * len
);
47 int * copy1_index
= fake_malloc_radix (sizeof(int) * len
);
48 int * copy2_value
= fake_malloc_radix (sizeof(int) * len
);
49 int * copy2_index
= fake_malloc_radix (sizeof(int) * len
);
55 for (i
= 0; i
< len
; i
++) {
56 copy1_value
[i
] = value
[i
];
57 copy1_index
[i
] = index
[i
];
68 for (i
= 0; i
< len
; i
++)
70 copy1_value
[i
] += min
;
74 for (i
= 0; max
!= 0; max
= max
/ numBuckets
, i
++)
76 for (j
= 0; j
< numBuckets
+ 2; j
++)
83 for (j
= 0; j
< len
; j
++)
85 int myBucket
= (int) (((int) copy1_value
[j
]) >> denShift
) & bitMask
;
89 for (j
= 1; j
< numBuckets
; j
++)
91 buckets
[j
] += buckets
[j
-1];
96 for (j
= 0; j
< len
; j
++)
98 int myBucket
= (int) (((int) copy1_value
[j
]) >> denShift
) & bitMask
;
99 int index
= buckets
[myBucket
]++;
100 copy2_value
[index
] = copy1_value
[j
];
101 copy2_index
[index
] = copy1_index
[j
];
105 denShift
+= radix_bits
;
107 tmp_value
= copy1_value
;
108 copy1_value
= copy2_value
;
109 copy2_value
= tmp_value
;
111 tmp_index
= copy1_index
;
112 copy1_index
= copy2_index
;
113 copy2_index
= tmp_index
;
116 max
= copy1_value
[0];
117 for (i
= 0; i
< len
; i
++) {
118 if (max
< copy1_value
[i
]) {
119 max
= copy1_value
[i
];
123 for (i
= 0; i
< len
; i
++)
125 copy1_value
[i
] -= min
;
128 for (i
= 0; i
< len
; i
++)
130 value
[i
] = copy1_value
[i
];
131 index
[i
] = copy1_index
[i
];
138 insertion_sort (float * value
, int * index
, int len
)
142 for (i
= 1; i
< len
; i
++)
149 cur_index
= index
[i
];
152 while (empty
> 0 && current
< value
[empty
-1])
154 value
[empty
] = value
[empty
-1];
155 index
[empty
] = index
[empty
-1];
159 value
[empty
] = current
;
160 index
[empty
] = cur_index
;
168 partition (float * array
, int * index
, int low
, int high
)
170 int left
, right
, mid
;
175 mid
= (low
+ high
) / 2;
179 /* choose pivot as median of 3: low, high, and mid */
180 if ((array
[low
] - array
[mid
]) * (array
[high
] - array
[low
]) >= 0)
182 else if ((array
[mid
] - array
[low
]) * (array
[high
] - array
[mid
]) >= 0)
187 /* store value,index at the pivot */
191 /* swap pivot with the first entry in the list */
192 array
[pivot
] = array
[low
];
195 index
[pivot
] = array
[pivot
];
198 /* the quicksort itself */
201 while (array
[left
] <= cur
&& left
< high
)
203 while (array
[right
] > cur
)
210 tmp_val
= array
[right
];
211 array
[right
] = array
[left
];
212 array
[left
] = tmp_val
;
214 tmp_idx
= index
[right
];
215 index
[right
] = index
[left
];
216 index
[left
] = tmp_idx
;
220 /* pivot was in low, but now moves into position at right */
221 array
[low
] = array
[right
];
224 index
[low
] = index
[right
];
232 quicksort_inner (float * array
, int * index
, int low
, int high
)
235 int length
= high
- low
+ 1;
239 if (length
> MAX_THRESH
) {
240 pivot
= partition (array
, index
, low
, high
);
241 quicksort_inner (array
, index
, low
, pivot
-1);
242 quicksort_inner (array
, index
, pivot
+1, high
);
249 int quicksort (float * array
, int * index
, int len
)
251 quicksort_inner (array
, index
, 0, len
-1);
252 insertion_sort (array
, index
, len
);