Rev 331 | Rev 335 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 331 | Rev 333 | ||
---|---|---|---|
Line -... | Line 1... | ||
- | 1 | /* |
|
- | 2 | * Copyright (C) 2005 Sergey Bondari |
|
- | 3 | * All rights reserved. |
|
- | 4 | * |
|
- | 5 | * Redistribution and use in source and binary forms, with or without |
|
- | 6 | * modification, are permitted provided that the following conditions |
|
- | 7 | * are met: |
|
- | 8 | * |
|
- | 9 | * - Redistributions of source code must retain the above copyright |
|
- | 10 | * notice, this list of conditions and the following disclaimer. |
|
- | 11 | * - Redistributions in binary form must reproduce the above copyright |
|
- | 12 | * notice, this list of conditions and the following disclaimer in the |
|
- | 13 | * documentation and/or other materials provided with the distribution. |
|
- | 14 | * - The name of the author may not be used to endorse or promote products |
|
- | 15 | * derived from this software without specific prior written permission. |
|
- | 16 | * |
|
- | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
- | 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
- | 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
- | 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
- | 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
- | 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
- | 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
- | 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
- | 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
- | 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
- | 27 | */ |
|
- | 28 | ||
1 | #include <mm/heap.h> |
29 | #include <mm/heap.h> |
2 | #include <memstr.h> |
30 | #include <memstr.h> |
3 | #include <sort.h> |
31 | #include <sort.h> |
4 | 32 | ||
5 | 33 | ||
6 | void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) { |
34 | void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) { |
7 | - | ||
8 | if (n > 4) { |
35 | if (n > 4) { |
9 | int i = 0, j = n - 1; |
36 | int i = 0, j = n - 1; |
10 | void * tmp = (void *) malloc(e_size); |
37 | void * tmp = (void *) malloc(e_size); |
11 | void * pivot = (void *) malloc(e_size); |
38 | void * pivot = (void *) malloc(e_size); |
12 | 39 | ||
13 | memcpy(pivot, data, e_size); |
40 | memcpy(pivot, data, e_size); |
14 | 41 | ||
15 | while (1) { |
42 | while (1) { |
16 | while ((cmp(data + i * e_size, pivot) < 0) && i < n) i++; |
43 | while ((cmp(data + i * e_size, pivot) < 0) && i < n) i++; |
17 | while ((cmp(data + j * e_size, pivot) >=0) && j > 0) j--; |
44 | while ((cmp(data + j * e_size, pivot) >=0) && j > 0) j--; |
18 | - | ||
19 | if (i<j) { |
45 | if (i<j) { |
20 | memcpy(tmp, data + i * e_size, e_size); |
46 | memcpy(tmp, data + i * e_size, e_size); |
21 | memcpy(data + i * e_size, data + j * e_size, e_size); |
47 | memcpy(data + i * e_size, data + j * e_size, e_size); |
22 | memcpy(data + j * e_size, tmp, e_size); |
48 | memcpy(data + j * e_size, tmp, e_size); |
23 | } else { |
49 | } else { |
24 | break; |
50 | break; |
25 | } |
51 | } |
26 | } |
52 | } |
27 | 53 | ||
28 | free(tmp); |
54 | free(tmp); |
29 | free(pivot); |
55 | free(pivot); |
30 | 56 | ||
31 | qsort(data, j + 1, e_size, cmp); |
57 | qsort(data, j + 1, e_size, cmp); |
32 | qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp); |
58 | qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp); |
33 | - | ||
34 | } else { |
59 | } else { |
35 | bubblesort(data, n, e_size, cmp); |
60 | bubblesort(data, n, e_size, cmp); |
36 | } |
- | |
37 | - | ||
38 | 61 | } |
|
39 | } |
62 | } |
40 | 63 | ||
41 | 64 | ||
42 | void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) { |
65 | void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) { |
43 | bool done = false; |
66 | bool done = false; |
44 | void * p; |
67 | void * p; |
45 | void * slot = (void *) malloc(e_size); |
68 | void * slot = (void *) malloc(e_size); |
46 | 69 | ||
47 | while (!done) { |
70 | while (!done) { |
48 | done = true; |
71 | done = true; |
49 | for (p = data; p < data + e_size * (n - 1); p = p + e_size) { |
72 | for (p = data; p < data + e_size * (n - 1); p = p + e_size) { |
50 | if (cmp(p, p + e_size) == 1) { |
73 | if (cmp(p, p + e_size) == 1) { |
51 | memcpy(slot, p, e_size); |
74 | memcpy(slot, p, e_size); |
52 | memcpy(p, p + e_size, e_size); |
75 | memcpy(p, p + e_size, e_size); |
53 | memcpy(p + e_size, slot, e_size); |
76 | memcpy(p + e_size, slot, e_size); |
54 | done = false; |
77 | done = false; |
55 | } |
- | |
56 | } |
78 | } |
57 | 79 | } |
|
58 | } |
80 | } |
59 | 81 | ||
60 | free(slot); |
82 | free(slot); |
61 | } |
83 | } |
62 | 84 | ||
63 | /* |
85 | /* |
64 | * Comparator returns 1 if a > b, 0 if a == b, -1 if a < b |
86 | * Comparator returns 1 if a > b, 0 if a == b, -1 if a < b |
65 | */ |
87 | */ |
66 | int int_cmp(void * a, void * b) { |
88 | int int_cmp(void * a, void * b) { |
67 | return (* (int *) a > * (int*)b) ? 1 : (*(int *)a < * (int *)b) ? -1 : 0; |
89 | return (* (int *) a > * (int*)b) ? 1 : (*(int *)a < * (int *)b) ? -1 : 0; |
68 | } |
90 | } |
69 | 91 | ||
70 | int __u8_cmp(void * a, void * b) { |
92 | int __u8_cmp(void * a, void * b) { |
71 | return (* (__u8 *) a > * (__u8 *)b) ? 1 : (*(__u8 *)a < * (__u8 *)b) ? -1 : 0; |
93 | return (* (__u8 *) a > * (__u8 *)b) ? 1 : (*(__u8 *)a < * (__u8 *)b) ? -1 : 0; |
72 | } |
94 | } |
73 | 95 | ||
74 | int __u16_cmp(void * a, void * b) { |
96 | int __u16_cmp(void * a, void * b) { |
75 | return (* (__u16 *) a > * (__u16 *)b) ? 1 : (*(__u16 *)a < * (__u16 *)b) ? -1 : 0; |
97 | return (* (__u16 *) a > * (__u16 *)b) ? 1 : (*(__u16 *)a < * (__u16 *)b) ? -1 : 0; |
76 | } |
98 | } |
77 | 99 | ||
78 | int __u32_cmp(void * a, void * b) { |
100 | int __u32_cmp(void * a, void * b) { |
79 | return (* (__u32 *) a > * (__u32 *)b) ? 1 : (*(__u32 *)a < * (__u32 *)b) ? -1 : 0; |
101 | return (* (__u32 *) a > * (__u32 *)b) ? 1 : (*(__u32 *)a < * (__u32 *)b) ? -1 : 0; |
80 | } |
102 | } |
81 | 103 |