快速排序
程序代码:
#include <stdio.h> #include <stdlib.h> void QkSort(int *arr,int left ,int right){ if (right>left){ return; } int temp = arr[left]; int i = left; int j = right; while(i!=j){ while (arr[j]>=temp&&j>i) { j--; } while (arr[i]<=temp&&j>i) { i++; } if(j>i){ int t = arr[i]; arr[i] = arr[j]; arr[j] =t; } } arr[left] = arr[i]; arr[i] = temp; QkSort(arr, left, i - 1); QkSort(arr, i + 1, right); } int main(){ int arr[] = {1, 32, 4, 56, 77, 55, 34, 12, 56, 75}; int len = sizeof(arr) / sizeof(int); for (int i=0; i <= len - 1;i++){ printf("%d ", arr[i]); } QkSort(arr, 0, len - 1); printf("\n"); for (int i = 0; i <= len - 1;i++){ printf("%d ", arr[i]); } }