#include <stdio.h>
int pivot_index(int *arr, int low, int high)
{
int pivot = arr[low];
while (low < high)
{
while (low < high && arr[high] >= pivot)
{
high--;
}
arr[low] = arr[high];
while (low < high && arr[low] <= pivot)
{
low++;
}
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
void quick_sort(int *arr, int start, int end)
{
if (start >= end)
{
return;
}
int i = pivot_index(arr, start, end);
quick_sort(arr, start, i - 1);
quick_sort(arr, i + 1, end);
}
int main()
{
int arr[6] = {1, 34, 54, 21, 45, 13};
int len = sizeof(arr) / sizeof(int);
quick_sort(arr, 0, len - 1);
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
快速排序(c语言)
最新推荐文章于 2024-08-23 00:00:02 发布