1.五大排序算法之 快速排序
2. #include
#include
#include
using namespace std;
void fastSort(int intarr[], int start,int end)
{
if (start >= end)
{
return;
}
int starti = start;
int endi = end;
int key = intarr[starti];
while (starti < endi)
{
while (starti < endi && intarr[endi]>=key)//必须是等于,要不然容易死循环
{
endi–;
}
intarr[starti] = intarr[endi];
while (starti < endi && intarr[starti]<=key)//必须是等于,要不然容易死循环
{
starti++;
}
intarr[endi] = intarr[starti];
}
intarr[starti]= key;
fastSort(intarr, start, starti - 1);
fastSort(intarr, starti + 1, end);
}
int main()
{
const int num = 20;
int intarr[num];
srand((unsigned)time(NULL));
for (int i = 0; i < num; i++)
{
int val = rand() % 100;
intarr[i] = val;
cout << val << " ";
}
cout << endl;
fastSort(intarr, 0, num - 1);
for (auto x : intarr)
{
cout << x << " ";
}
cout << endl;
system(“pause”);
return 0;
}
Lambda特性
#include
#include
#include
快速排序和C++11新特性Lambda
最新推荐文章于 2022-09-11 22:26:45 发布