XINU
qsort.c
Go to the documentation of this file.
1 /* qsort.c - qsort, qs1, qsexc, qstexc */
2 
3 static int (*qscmp) (char *, char *);
4 static int qses;
5 static void qs1(char *, char *);
6 static void qsexc(char *, char *);
7 static void qstexc(char *, char *, char *);
8 
9 /*------------------------------------------------------------------------
10  * qsort - Use quicksort to sort an array
11  *------------------------------------------------------------------------
12  */
13 void qsort(
14  char *a, /* Array to sort */
15  unsigned n, /* Length of the array */
16  int es, /* Pivot value */
17  int (*fc)(char *, char *) /* Comparison function */
18  )
19 {
20  qscmp = fc;
21  qses = es;
22  qs1(a, a + n * es);
23 }
24 
25 /*------------------------------------------------------------------------
26  * qs1 - internal quicksort function
27  *------------------------------------------------------------------------
28  */
29 static void qs1(
30  char *a,
31  char *l
32  )
33 {
34  register char *i, *j;
35  register int es;
36  char *lp, *hp;
37  int c;
38  unsigned n;
39 
40  es = qses;
41 
42  start:
43  if ((n = l - a) <= es)
44  {
45  return;
46  }
47  n = es * (n / (2 * es));
48  hp = lp = a + n;
49  i = a;
50  j = l - es;
51  for (;;)
52  {
53  if (i < lp)
54  {
55  if ((c = (*qscmp) (i, lp)) == 0)
56  {
57  qsexc(i, lp -= es);
58  continue;
59  }
60  if (c < 0)
61  {
62  i += es;
63  continue;
64  }
65  }
66 
67  loop:
68  if (j > hp)
69  {
70  if ((c = (*qscmp) (hp, j)) == 0)
71  {
72  qsexc(hp += es, j);
73  goto loop;
74  }
75  if (c > 0)
76  {
77  if (i == lp)
78  {
79  qstexc(i, hp += es, j);
80  i = lp += es;
81  goto loop;
82  }
83  qsexc(i, j);
84  j -= es;
85  i += es;
86  continue;
87  }
88  j -= es;
89  goto loop;
90  }
91 
92  if (i == lp)
93  {
94  if (lp - a >= l - hp)
95  {
96  qs1(hp + es, l);
97  l = lp;
98  }
99  else
100  {
101  qs1(a, lp);
102  a = hp + es;
103  }
104  goto start;
105  }
106 
107  qstexc(j, lp -= es, i);
108  j = hp -= es;
109  }
110 }
111 
112 /*------------------------------------------------------------------------
113  * qsexc - internal quicksort function
114  *------------------------------------------------------------------------
115  */
116 static void qsexc(
117  char *i,
118  char *j
119  )
120 {
121  register char *ri, *rj, c;
122  int n;
123 
124  n = qses;
125  ri = i;
126  rj = j;
127  do
128  {
129  c = *ri;
130  *ri++ = *rj;
131  *rj++ = c;
132  }
133  while (--n);
134 }
135 
136 /*------------------------------------------------------------------------
137  * qstexc - internal quicksort function
138  *------------------------------------------------------------------------
139  */
140 static void qstexc(
141  char *i,
142  char *j,
143  char *k
144  )
145 {
146  register char *ri, *rj, *rk;
147  int c;
148  int n;
149 
150  n = qses;
151  ri = i;
152  rj = j;
153  rk = k;
154  do
155  {
156  c = *ri;
157  *ri++ = *rk;
158  *rk++ = *rj;
159  *rj++ = c;
160  }
161  while (--n);
162 }
static void qs1(char *, char *)
Definition: qsort.c:29
static void qsexc(char *, char *)
Definition: qsort.c:116
static int qses
Definition: qsort.c:4
static int(* qscmp)(char *, char *)
Definition: qsort.c:3
uint32 start
void qsort(char *a, unsigned n, int es, int(*fc)(char *, char *))
Definition: qsort.c:13
static void qstexc(char *, char *, char *)
Definition: qsort.c:140