/*
快速排序,
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
void Qsort(int a[], int low, int high)
{
if (low >= high)
{
return;
}
int first = low;
int last = high;
int key = a[first];/*用字表的第一个记录作为枢轴*/
while (first < last)
{
while (first < last && a[last] >= key)
{
--last;
}
a[first] = a[last];/*将比第一个小的移到低端*/
while (first < last && a[first] <= key)
{
++first;
}
a[last] = a[first];
/*将比第一个大的移到高端*/
}
a[first] = key; /*枢轴记录到位*/
Qsort(a, low, first - 1);
Qsort(a, first + 1, high);
}
//http://developer.51cto.com/art/201403/430986.htm
//例子很清晰,容易理解,交换部分不同,可能会更快???
int main()
{
int narry[100], addr[100];
int sum = 1, t;
cout << "Input number:" << endl; //回车换行输入数组的方法
cin >> t;
while (t != -1)
{
narry[sum] = t;
addr[sum - 1] = t; //记录第几个位置用
sum++;
cin >> t;
}
sum -= 1;
Qsort(narry, 1, sum);
for (int i = 1; i <= sum; i++)
cout << narry[i] << '\t';
cout << endl;
int k;
cout << "Please input place you want:" << endl;
cin >> k;
int aa = 1;
int kk = sum;
for (;;)
{
if (aa == k)
break;
if (narry[kk] != narry[kk - 1])
{
aa += 1;
}kk--;
}
cout << "The NO." << k << "number is:" << narry[kk] << endl;
cout << "And it's place is:";
for (int i = 0; i < sum; i++)
{
if (addr[i] == narry[kk])
cout << i << '\t';
}
system("pause");
return 0;
}
C/C++ | 18-4 快排 Qsort
最新推荐文章于 2025-03-01 15:39:24 发布