Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void quicksort(int *A, int len);
|
||||
|
||||
int main (void) {
|
||||
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 ", a[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
quicksort(a, n);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
printf("%d ", a[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void quicksort(int *A, int len) {
|
||||
if (len < 2) return;
|
||||
|
||||
int pivot = A[len / 2];
|
||||
|
||||
int i, j;
|
||||
for (i = 0, j = len - 1; ; i++, j--) {
|
||||
while (A[i] < pivot) i++;
|
||||
while (A[j] > pivot) j--;
|
||||
|
||||
if (i >= j) break;
|
||||
|
||||
int temp = A[i];
|
||||
A[i] = A[j];
|
||||
A[j] = temp;
|
||||
}
|
||||
|
||||
quicksort(A, i);
|
||||
quicksort(A + i, len - i);
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdlib.h> // REQ: rand()
|
||||
|
||||
void swap(int *a, int *b) {
|
||||
int c = *a;
|
||||
*a = *b;
|
||||
*b = c;
|
||||
}
|
||||
|
||||
int partition(int A[], int p, int q) {
|
||||
swap(&A[p + (rand() % (q - p + 1))], &A[q]); // PIVOT = A[q]
|
||||
|
||||
int i = p - 1;
|
||||
for(int j = p; j <= q; j++) {
|
||||
if(A[j] <= A[q]) {
|
||||
swap(&A[++i], &A[j]);
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
void quicksort(int A[], int p, int q) {
|
||||
if(p < q) {
|
||||
int pivotIndx = partition(A, p, q);
|
||||
|
||||
quicksort(A, p, pivotIndx - 1);
|
||||
quicksort(A, pivotIndx + 1, q);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue