写这个的起因是自己很好奇,为什么大家的快排都是左边放小于等于基准数的,这个等于为什么要放在左边,所有自己加了很多注释写了个代码看了一下,其实很简单的问题,从左往右遍历的时候,每次都是和自身先进行比较的,这几天学东西有点杂,脑子有点转不动了,用这篇文章稍微警示一下自己吧
/*************************************************************************
> File Name: quick.c
> Created Time: Wed 11 Aug 2021 03:31:44 PM CST
> Author: noah
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
void quickSort(int nums[], int left, int right) {
#if 0
printf("use:%d\n", nums[left]);
for (int i = 0; i < 7; i++) {
printf("set.%d : nums.%d\t\t", i, nums[i]);
}
putchar(10);
#endif
if (left > right) return;
int temp = nums[left];
int l = left, r = right;
while (l != r) {
while (r > l && nums[r] > temp) {
r--;
}
while (r > l && nums[l] <= temp) {
l++;
}
if (r > l) {
//printf("swap:%d\t%d\n", l, r);
int ret = nums[r];
nums[r] = nums[l];
nums[l] = ret;
}
}
nums[left] = nums[l];
nums[l] = temp;
quickSort(nums, left, l-1);
quickSort(nums, l+1, right);
}
int main(int argc, char *argv[])
{
int a[10] = {1,5,11,8,0,6,7};
quickSort(a, 0, 6);
for (int i = 0; i < 7; i++) {
printf("%d\t", a[i]);
}
putchar(10);
return 0;
}