2 * sgen-qsort.c: Quicksort.
4 * Copyright (C) 2013 Xamarin Inc
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #include "sgen/sgen-gc.h"
16 (((unsigned char*)array) + ((i) * element_size))
18 do memcpy ((i), (j), element_size); while (0)
21 size_t __i = (i), __j = (j); \
23 SET (swap_tmp, ELEM (__i)); \
24 SET (ELEM (__i), ELEM (__j)); \
25 SET (ELEM (__j), swap_tmp); \
32 const size_t element_size,
33 int (*compare) (const void *, const void *),
36 unsigned char *const pivot_tmp,
37 unsigned char *const swap_tmp)
39 ssize_t left, right, mid, pivot;
43 mid = begin + (end - begin) / 2;
45 /* Choose median of 3 as pivot and pre-sort to avoid O(n^2) case.
53 if (compare (ELEM (mid), ELEM (left)) < 0)
55 if (compare (ELEM (right), ELEM (left)) < 0)
57 if (compare (ELEM (right), ELEM (mid)) < 0)
60 SET (pivot_tmp, ELEM (pivot));
64 while (left <= right && compare (ELEM (left), pivot_tmp) <= 0)
66 while (left <= right && compare (ELEM (right), pivot_tmp) > 0)
76 SET (ELEM (pivot), ELEM (right));
77 SET (ELEM (right), pivot_tmp);
80 /* Recursively sort shorter partition, loop on longer partition. */
81 if (right - begin < end - left) {
108 const size_t element_size,
109 int (*compare) (const void *, const void *))
112 unsigned char pivot_tmp [element_size];
113 unsigned char swap_tmp [element_size];
115 unsigned char *pivot_tmp = (unsigned char *)alloca (element_size);
116 unsigned char *swap_tmp = (unsigned char *)alloca (element_size);