注:本题只是单趟快排
Description |
对输入的关键字进行快速排序,输出第一趟的排序结果。 说明:输入第一行为关键字数;第二行为关键字序列 输出为第一趟快速排序的结果。 |
Sample Input |
8 49 38 65 97 76 13 27 49 |
Sample Output |
27 38 13 49 76 97 65 49 |
Hint |
输出有换行 |
#include <iostream>
#include <bits/stdc++.h>
#define Maxnum 105
#define padix 10
using namespace std;
typedef struct
{
int key;
int arr[Maxnum];
} keynode;
typedef struct
{
keynode *R;
int length;
} Sqlist;
void initL(Sqlist &L)
{
L.R = new keynode[Maxnum];
}
void creatL(Sqlist &L)
{
int m;
cin >> m;
L.length = m;
for (int i = 1; i <= m; i++)
{
cin >> L.R[i].key;
}
}
void Partition(Sqlist &L, int low, int high)
{ // 单趟快速排序
L.R[0] = L.R[low];
int pivo = L.R[low].key;
while (low < high)
{
if (low < high && L.R[high].key >= pivo)
{ // 从右往前找小的交换(带等号)
high--;
}
L.R[low] = L.R[high];
if (low < high && L.R[low].key <= pivo)
{
low++;
}
L.R[high] = L.R[low];
}
L.R[low] = L.R[0]; // 结束为相等也是插入位置
}
void PrintL(Sqlist L)
{
int cnt = 0;
for (int i = 1; i <= L.length; i++)
{
cout << L.R[i].key;
if (cnt != L.length - 1)
{
cout << " ";
}
cnt++;
}
cout << "\n";
}
int main()
{
Sqlist L;
initL(L);
creatL(L);
Partition(L, 1, L.length);
PrintL(L);
return 0;
}