#include <stdio.h>
#define TRUE 1
#define FALSE 0
typedef unsigned char BOOL;
typedef unsigned int UINT;
void quick_sort(UINT*, UINT, UINT);
void print_arr(UINT*, UINT);
int main(int argc, char* argv[])
{
int arr[] = {1, 0, 2, 3, 5, 4, 6, 8, 7, 9};
printf("Before sort: ");
print_arr(arr, sizeof(arr)/sizeof(UINT));
quick_sort(arr, 0, sizeof(arr)/sizeof(UINT)-1);
printf("After sort: ");
print_arr(arr, sizeof(arr)/sizeof(UINT));
return 0;
}
void quick_sort(UINT* arr, UINT low, UINT high)
{
UINT i = low;
UINT j = high;
UINT radix_val = arr[low];
if(i >= j)
return;
while (i < j)
{
while(i<j && arr[j]>=radix_val)
{
j--;
}
if(i < j)
{
arr[i++] = arr[j];
}
while(i<j && arr[j]<=radix_val)
{
i++;
}
if(i < j)
{
arr[j--] = arr[i];
}
}
arr[i] = radix_val;
quick_sort(arr, low, i-1);
quick_sort(arr, i+1, high);
}
void print_arr(UINT* arr, UINT arr_len)
{
UINT i;
for(i=0; i<arr_len; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
快速排序
最新推荐文章于 2021-07-24 23:27:25 发布