//排序算法-------快速排序程序实现
//2018年6月7日下午三点27分,距离当年高考数学两年的失利过去六七年了。希望成功会眷顾真正努力的人
#include <iostream>
using namespace std;
void QuickSort(double A[], int s, int t) //对A[s]到A[t]进行快速排序
{
int i = s, j = t;
if (s < t)
{
double tmp = A[s];
while (i != j)
{
while (A[j] >= tmp&&j>i)
{
--j;
}
A[i] = A[j];
while (A[i] <= tmp&&i < j)
{
++i;
}
A[j] = A[i];
}
A[i] = tmp;
QuickSort(A, s, i - 1);
QuickSort(A, s + 1, t);
}
}
void print(int n,double A[])
{
for (int k = 1; k<=n; ++k)
{
cout << A[k] << " ";
}
}
int main()
{
int n;
cout << "请输入您要排序的数字个数" << endl;
cin >> n;
double A[100] = { 0 };
cout << "请输入要排序的数字,数字之间用空格间隔" << endl;
for (int m = 1; m <= n; ++m)
{
cin >> A[m];
}
cout << "待排序的序列是这样,您看对吗"<< endl;
print(n, A); cout << endl;
QuickSort(A, 1, n);
print(n, A);
}
//2018年6月7日下午三点27分,距离当年高考数学两年的失利过去六七年了。希望成功会眷顾真正努力的人
using namespace std;
void QuickSort(double A[], int s, int t) //对A[s]到A[t]进行快速排序
{
int i = s, j = t;
if (s < t)
{
double tmp = A[s];
while (i != j)
{
while (A[j] >= tmp&&j>i)
{
--j;
}
A[i] = A[j];
while (A[i] <= tmp&&i < j)
{
++i;
}
A[j] = A[i];
}
A[i] = tmp;
QuickSort(A, s, i - 1);
QuickSort(A, s + 1, t);
}
}
void print(int n,double A[])
{
for (int k = 1; k<=n; ++k)
{
cout << A[k] << " ";
}
}
int main()
{
int n;
cout << "请输入您要排序的数字个数" << endl;
cin >> n;
double A[100] = { 0 };
cout << "请输入要排序的数字,数字之间用空格间隔" << endl;
for (int m = 1; m <= n; ++m)
{
cin >> A[m];
}
cout << "待排序的序列是这样,您看对吗"<< endl;
print(n, A); cout << endl;
QuickSort(A, 1, n);
print(n, A);
}