初学者。如果有误请指正,欢迎联系QQ2684162190
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 10
void swap(int a[],int x,int y)
{
int t = a[x];
a[x] = a[y];
a[y] = t;
}
//函数名:display
//作用:打印数组元素
//参数:array - 打印的数组,maxlen - 数组元素个数
void display(int array[], int maxlen)
{
int i;
for(i = 0; i < maxlen; i++)
{
printf("%-3d", array[i]);
}
printf("\n");
return ;
}
//将结构数组分为两个部分
int partition(int array[], int low, int high)
{
int piovtkey = array[low]; //记录关键字
while(low < high)
{
while(low < high&&array[high] >= piovtkey)
high--;
array[low] = array[high];
while(low < high&&array[low] <= piovtkey)
low++;
array[high] = array[low];
}
array[low] = piovtkey;
return low;
}
void Qsort(int array[],int low,int high)
{
int piovtloc;
if(low < high)
{
piovtloc = partition(array,low,high);
Qsort(array,low,piovtloc-1);
Qsort(array,piovtloc+1,high);
}
}
void Quicksort(int array[])
{
Qsort(array,1,ARRAY_SIZE-1);
}
int partition1(int a[],int low,int high)
{
int i = 0,j = low;
while(j < high)
{
j++;
if(a[j] <= a[high]&&i+1!=j)
{
swap(a,i+1,j);
i++;
}
else if(a[j] <= a[high] && i+1 == j)
i++;
}
return i;
}
void Qsort1(int array[],int low,int high)
{
int piovtloc;
if(low < high)
{
piovtloc = partition(array,low,high);
Qsort(array,low,piovtloc-1);
Qsort(array,piovtloc+1,high);
}
}
// 主函数
int main()
{
int n;
int array[ARRAY_SIZE] = {0,8,2,6,4,10,1,3,9,5};
int maxlen = ARRAY_SIZE;
printf("排序前的数组\n");
display(array, 10);
// Quicksort(array); // 快速排序
printf("排序后的数组\n");
Qsort1(array,0,9);
display(array, 10);
return 0;
}