Download code

Jump to: navigation, search

Back to Quicksort_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

quicksort_test.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Quicksort_(C)?oldid=10011
14 */
15 
16 #include <stdio.h>
17 #include <time.h>
18 #include <stdlib.h>
19 
20 #include "quicksort.h"
21 
22 int compare_int(const void* left, const void* right) {
23     return *((int*)left) - *((int*)right);
24 }
25 
26 int main(int argc, char* argv[]) {
27     int size = atoi(argv[1]);
28     int* a = malloc(sizeof(int)*size);
29     int i;
30     srand(time(NULL));
31     for(i=0; i<size; i++) {
32         a[i] = rand() % size;
33     }
34     quicksort(a, size, sizeof(int), compare_int);
35     for(i=1; i<size; i++) {
36         if (!(a[i-1] <= a[i])) {
37             puts("ERROR");
38             return -1;
39         }
40     }
41     puts("SUCCESS");
42     return 0;
43 }


hijacker
hijacker
hijacker
hijacker

quicksort.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Quicksort_(C)?oldid=10011
14 */
15 
16 #include <stdlib.h>
17 #include "quicksort.h"
18 
19 #define MIN_QUICKSORT_LIST_SIZE    32
20 
21 static int compare_elements_helper(void *base, size_t element_size, int idx1, int idx2,
22                                    int(*comparer)(const void *, const void*))
23 {
24     char* base_bytes = base;
25     return comparer(&base_bytes[idx1*element_size], &base_bytes[idx2*element_size]);
26 }
27 
28 #define element_less_than(i,j)  (compare_elements_helper(base, element_size, (i), (j), comparer) < 0)
29 
30 static void exchange_elements_helper(void *base, size_t element_size, int idx1, int idx2)
31 {
32     char* base_bytes = base;
33     int i;
34     for (i=0; i<element_size; i++)
35     {
36         char temp = base_bytes[idx1*element_size + i];
37         base_bytes[idx1*element_size + i] = base_bytes[idx2*element_size + i];
38         base_bytes[idx2*element_size + i] = temp;
39     }
40 }
41 
42 #define exchange_elements(i,j)  (exchange_elements_helper(base, element_size, (i), (j)))
43 void insertion_sort(void * base, size_t num_elements, size_t element_size,
44                     int (*comparer)(const void *, const void *))
45 {
46    int i;
47    for (i=0; i < num_elements; i++)
48    {
49        int j;
50        for (j = i - 1; j >= 0; j--)
51        {
52            if (element_less_than(j, j + 1)) break;
53            exchange_elements(j, j + 1);
54        }
55    }
56 }
57 int partition(void * base, size_t num_elements, size_t element_size,
58                int (*comparer)(const void *, const void *), int pivotIndex)
59 {
60     int low = 0, high = num_elements - 1;
61     exchange_elements(num_elements - 1, pivotIndex);
62 
63     while (1) {
64         while (element_less_than(low, num_elements-1)) {
65 	    low++;
66 	}
67 	while (!element_less_than(high, num_elements-1)) {
68 	    high--;
69 	}
70         if (low > high) break;
71         exchange_elements(low, high);
72     }
73     exchange_elements(low, num_elements - 1);
74     return low;
75 }
76 void quicksort(void * base, size_t num_elements, size_t element_size,
77                int (*comparer)(const void *, const void *))
78 {
79     int pivotIndex;
80 
81     if (num_elements < MIN_QUICKSORT_LIST_SIZE) {
82         insertion_sort(base, num_elements, element_size, comparer);
83         return;
84     }
85 
86     pivotIndex = rand() % num_elements;
87     pivotIndex = partition(base, num_elements, element_size, comparer, pivotIndex);
88     quicksort(base, pivotIndex, element_size, comparer);
89     quicksort(((char*)base) + element_size*(pivotIndex+1),
90               num_elements - (pivotIndex + 1), element_size, comparer);
91 }
92 


hijacker
hijacker
hijacker
hijacker

quicksort.h

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Quicksort_(C)?oldid=10011
14 */
15 
16 #ifndef _QUICKSORT_H_
17 #define _QUICKSORT_H_
18 
19 #include <stddef.h>
20 
21 void quicksort(void * base, size_t num_elements, size_t element_size,
22                int (*comparer)(const void *, const void *));
23 
24 #endif
25 


hijacker
hijacker
hijacker
hijacker