快排
动画:http://www.tyut.edu.cn/kecheng1/site01/suanfayanshi/quick_sort.asp
#include <stdio.h> #define swap(x,y) { int tmp; tmp = x; x = y; y = tmp;} void quick_sort(int *a, int start, int end) //sort a[start] to a[end] { if(start < end) { int i = start, j = end; int tmp = a[start]; while(i < j) { while( i < j && a[j] >= tmp) j--; if(i < j) { a[i++] = a[j]; } while(i < j && a[i] < tmp) i++; if(i < j) { a[j--] = a[i]; } } a[i] = tmp; quick_sort(a, start, i - 1); quick_sort(a, i + 1, end); } } void print_array(int *array, int num) { int i; for(i = 0; i < num; i++) { printf("%d ", array[i]); } printf ("\n"); } int main() { int array[10] = {2, 5, 8, 3, 1, 7, 9, 6, 0, 4}; quick_sort(array, 0, 9); print_array(array, 10); return 0; }