#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