#include <stdio.h> void swap(int *pa, int *pb) { int t = *pa; *pa = *pb; *pb = t; } int partion(int *array, int begin, int end) { if (array == NULL || begin < 0 || end < 0) return -1; int pivot = array[end]; int small = begin -1; for(int i = begin; i < end; i++ ){ if(array[i] <= pivot) { ++small; if(small != i){ swap(&array[small], &array[i]); } } } swap(&array[++small], &array[end]); return small; } void qsort(int array[], int begin, int end) { if (end == begin) return; int index = partion(array, begin, end); if (index == -1) return; if (index > begin) qsort(array, begin, index - 1); if (end > index) qsort(array, index + 1, end); } void display(int a[], int n) { for(int i = 0; i < n; i ++) printf("%d ", a[i]); printf(" "); } int main() { int a[] = {5, 4, 3, 2,6}; int n = sizeof(a)/sizeof(a[0]); display(a, n); qsort(a, 0, n -1); display(a, n); }