Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,29 +1,33 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void quick_sort (int *a, int n) {
|
||||
int i, j, p, t;
|
||||
if (n < 2)
|
||||
return;
|
||||
int p = a[n / 2];
|
||||
int *l = a;
|
||||
int *r = a + n - 1;
|
||||
while (l <= r) {
|
||||
if (*l < p) {
|
||||
l++;
|
||||
continue;
|
||||
}
|
||||
if (*r > p) {
|
||||
r--;
|
||||
continue; // we need to check the condition (l <= r) every time we change the value of l or r
|
||||
}
|
||||
int t = *l;
|
||||
*l++ = *r;
|
||||
*r-- = t;
|
||||
p = a[n / 2];
|
||||
for (i = 0, j = n - 1;; i++, j--) {
|
||||
while (a[i] < p)
|
||||
i++;
|
||||
while (p < a[j])
|
||||
j--;
|
||||
if (i >= j)
|
||||
break;
|
||||
t = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = t;
|
||||
}
|
||||
quick_sort(a, r - a + 1);
|
||||
quick_sort(l, a + n - l);
|
||||
quick_sort(a, i);
|
||||
quick_sort(a + i, n - i);
|
||||
}
|
||||
|
||||
int main () {
|
||||
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
|
||||
int n = sizeof a / sizeof a[0];
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
|
||||
quick_sort(a, n);
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue