1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include<stdio.h> #include<malloc.h>
int split(int*A, int low, int high); void exchange(int *A, int i, int j); void quicksort(int *A, int low, int high);
int main() {
int N,i; int *A; A = (int*)malloc(N * sizeof(int)); printf("Please input the number of the array:"); scanf("%d", &N); printf("Please input the array:"); for(i=0; i<N; i++) scanf("%d",&A[i]);
quicksort(A, 0, N-1); for(i=0; i<N; i++) printf("%5d",A[i]); return 0; }
int split(int*A, int low, int high) { int i = low; int key = A[i]; int j; for (j = low+1; j <= high; ++j) { if(A[j] <= key) { i = i + 1; if(i !=j) exchange(A, i, j); } } exchange(A, low, i); int w = i; return w; }
void quicksort(int *A, int low, int high) { if(low < high) { int w = split(A, low, high); quicksort(A, low, w-1); quicksort(A, w+1, high); } }
void exchange(int *A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; }
|