#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");
}
排序算法-快速排序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
NetBeans运行error: bin/nativeexecution/Linux-x86_64/pty
Juffi
2018-08-27 19:54:17
my leetcode#1 #2 #3 #4 #6 #7 #8
Juffi
2018-08-27 19:54:16
my leetcode#13 #14
Juffi
2018-08-27 19:54:15
移植unbuntu Qt项目到 windows 下(包含socket)
Juffi
2018-08-27 19:54:15
学习C++通过使用MinGW+Eclipse(windows下)
暴烈骑士
2018-08-27 19:49:19
虚函数表
fengjingchao8
2018-08-27 19:48:48
C++中int main(int argc, char *argv[])解释
YuFF
2018-08-27 19:45:47
C++中函数的参数传递
YuFF
2018-08-27 19:45:43
直接插入排序、shell 排序、快速排序C++实现
FANG___
2018-08-27 19:42:41
二叉树的前中后序的递归、非递归遍历
FANG___
2018-08-27 19:42:38
(C++) 踩了几个坑
FANG___
2018-08-27 19:42:38
个人觉得C++BuilderX是个失败的作品
elever
2018-08-27 19:41:36
CloseHandle(),TerminateThread(),ExitThread()的区别
侠隐仙踪
2018-08-27 19:34:57
拷贝某个文件夹下的所有文件到指定文件夹
侠隐仙踪
2018-08-27 19:34:57