#include<stdlib.h>
#include<stdio.h>
using namespace std;
void swap(int* arry,int low,int high)
{
int tmp = arry[low];
arry[low] = arry[high];
arry[high] = tmp;
}
int partition(int* arry,int low,int high)
{
int pv = arry[low];
while (low<high)
{
while (low<high&&arry[high]>=pv)
{
high--;//比基准大,本来就在右边,所以high前移动
}
swap(arry,low,high);
while (low<high&&arry[low]<=pv)
{
low++;
}
swap(arry,low,high);
}
//返回枢轴的位置 。。重要
return low;
}
void QSort(int* arry,int low,int high)
{
if (low<high)
{
//选一个pv值,把数据分别放在pv值的左右两边,并把pivot位置返回出来
int pivot = partition(arry,low,high);
//对子序列1排序
QSort(arry,low,pivot-1);
//对子序列2排序
QSort(arry,pivot+1,high);
}
}
void Quick_Sort(int * arry ,int count)
{
QSort(arry,0,count-1);
}
void main()
{
int arry[] = { 12,20,45,25,35,1,2,54,52 };
int count = sizeof(arry) / sizeof(*arry);
printf("arry数组个数:%d\n", count);
for (int i = 0; i < count; i++)
{
printf("%d ", arry[i]);
}
printf("\n排序之前");
Quick_Sort(arry, count);
for (int i = 0; i < count; i++)
{
printf("%d ", arry[i]);
}
printf("\n排序之后");
system("pause");
}
#include<stdio.h>
using namespace std;
void swap(int* arry,int low,int high)
{
int tmp = arry[low];
arry[low] = arry[high];
arry[high] = tmp;
}
int partition(int* arry,int low,int high)
{
int pv = arry[low];
while (low<high)
{
while (low<high&&arry[high]>=pv)
{
high--;//比基准大,本来就在右边,所以high前移动
}
swap(arry,low,high);
while (low<high&&arry[low]<=pv)
{
low++;
}
swap(arry,low,high);
}
//返回枢轴的位置 。。重要
return low;
}
void QSort(int* arry,int low,int high)
{
if (low<high)
{
//选一个pv值,把数据分别放在pv值的左右两边,并把pivot位置返回出来
int pivot = partition(arry,low,high);
//对子序列1排序
QSort(arry,low,pivot-1);
//对子序列2排序
QSort(arry,pivot+1,high);
}
}
void Quick_Sort(int * arry ,int count)
{
QSort(arry,0,count-1);
}
void main()
{
int arry[] = { 12,20,45,25,35,1,2,54,52 };
int count = sizeof(arry) / sizeof(*arry);
printf("arry数组个数:%d\n", count);
for (int i = 0; i < count; i++)
{
printf("%d ", arry[i]);
}
printf("\n排序之前");
Quick_Sort(arry, count);
for (int i = 0; i < count; i++)
{
printf("%d ", arry[i]);
}
printf("\n排序之后");
system("pause");
}