本文共 648 字,大约阅读时间需要 2 分钟。
#include#define SWAP(A,B) {A ^= B; B ^= A; A ^= B;}void quickSort(int arr[],int left,int right){ int i,j,mid; if(left < right) { mid = arr[(left + right) / 2]; i = left - 1; j = right + 1; while(1) { while(arr[++i] < mid); while(arr[--j] > mid); if(i >= j) break; SWAP(arr[i],arr[j]); } quickSort(arr,left,i - 1); quickSort(arr,j + 1,right); }}int main(){ int i; int arr[] = {3,1,5,2,7,6,9,8,4}; int left = 0; int right = sizeof(arr) / sizeof(arr[0]); for(i = 0; i < right; i++) printf("%d ",arr[i]); printf("\n"); quickSort(arr,left,right); for(i = 0; i < right; i++) printf("%d ",arr[i]); printf("\n"); return 0;}
转载地址:http://kxuhj.baihongyu.com/