#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void quickSort(int s[], int l, int r)//把要排序的数组和最左边的下标还有最右边的下标传进来.
{
if (l < r)
{
int i = l;
int j = r;
int x = s[l];
while (i < j)
{
while (i < j && s[j] >= x)
{
j--;
}
if (i < j)
{
s[i] = s[j];
i++;
}
while (i < j && s[i] <= x)
{
i++;
}
if (i < j)
{
s[j] = s[i];
j--;
}
}
s[i] = x;
quickSort(s, l, i - 1);
quickSort(s, i + 1, r);
}
}
int main(void)
{
int i = 0;
int array[] = { 12, 5, 33, 6, 10, 99, 55, 2, 11, 100, 88, 77, 66 };
int len = sizeof(array) / sizeof(int);
for (i = 0; i < len; i++)
{
printf("%d\t", array[i]);
}
quickSort(array, 0, len - 1);
printf("\n");
for (i = 0; i < len; i++)
{
printf("%d\t", array[i]);
}
printf("\n");
system("pause");
return 0;
}
快排
最新推荐文章于 2023-07-12 13:07:43 发布