#include<iostream>
using namespace std;
int partition(int *p, int len)
{
int i = -1;
int key = *(p + len);
for (int j = 0; j <=len; j++)
{
if (*(p + j) <= key)
{
i = i + 1;
int temp = *(p + i);
*(p + i) = *(p + j);
*(p + j) = temp;
}
}
return i;
}
void quicksort(int *p, int len)
{
if (len > 0)
{
int q = partition(p, len);
if (q > 0)
{
quicksort(p, q - 1);
}
quicksort(p + q + 1, len - q-1);
}
}
int main()
{
int size;
cout << "input the length of the array you want to sort" << endl;
cin >> size;
int *p = new int[size];
cout << "input the numbers" << endl;
for (int i = 0; i <= size-1; i++)
{
cin >> *(p + i);
}
quicksort(p, size-1);
for (int i = 0; i <= size-1; i++)
{
cout << *(p + i) << endl;
}
system("pause");
delete[]p;
p = NULL;
return 0;
}
快速排序
最新推荐文章于 2021-11-19 16:57:43 发布