快速C++分类方法
最编程
2024-07-31 17:58:46
...
余祥宣, 崔国华, 邹海明. 计算机算法基础.3版[M]. 华中科技大学出版社, 2006.
P88 算法4.13
#include<iostream>
#include<limits>
using namespace std;
void InterChange(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int Partition(int A[], int m, int p)
{
int i, v;
v = A[m];
i = m;
while (true)
{
do
{
i=i+1;
} while (A[i] < v);
do
{
p=p-1;
} while (A[p] > v);
if (i < p)
{
InterChange(A[i], A[p]);
}else
{
break;
}
}
A[m] = A[p];
A[p] = v;
return p;
}
void QuickSort(int A[], int p, int q)
{
if (p < q) {
int j = q + 1;
j=Partition(A, p, j);
QuickSort(A, p, j-1);
QuickSort(A, j + 1, q);
}
}
void ShowList(int A[],int length)
{
for(int i=0;i<length;i++)
cout << i << "=>" << A[i] << "\t";
cout << endl;
}
void Show(int A[],int length)
{
cout << "\nBefore\n";
ShowList(A, length);
QuickSort(A, 0, length-1);
cout << "After\n";
ShowList(A, length);
cout << endl;
}
int main()
{
int a[] = { 6,5,4,9,3,2,1,INT_MAX };
int b[] = { 99,56,12,31,78,12,45,INT_MAX };
Show(a, 7);
Show(b, 7);
system("pause");
return 0;
}
运行结果
上一篇: 数学建模2-美国人口增长模型的确定
下一篇: 《生命的未来》读书笔记