找一個基準數,而後將大於和小於基準數的值互換。:fa-pied-piper-alt:ui
#include<stdio.h> void quick_sort(int arr[],int l,int r){ if (l>=r) return; for (int i = 0; i < 10; i++) { printf("%d ",arr[i] ); } printf("\n"); int tmp = arr[l]; int i = l; int j = r; int t; while(i != j){ while(arr[j] >= tmp && i < j ) j--; while(arr[i] <= tmp && i < j) i++; if(i<j){ t = arr[i]; arr[i] = arr[j]; arr[j] = t; } } arr[l] = arr[i]; arr[i] = tmp; quick_sort(arr,l,i-1); //左 quick_sort(arr,i+1,r); } int main(int argc, char const *argv[]) { int arr[] = {6,1,2,7,9,11,4,5,10,8}; quick_sort(arr,0,9); printf("\n=========\n"); for (int i = 0; i < 10; i++) { printf("%d ",arr[i] ); } printf("\n"); return 0; }