#include <iostream>
using namespace std;
//交换ab地址的值
void swap(int &a, int &b)
{
if (a == b)
return;
a = a + b;
b = a - b;
a = a - b;
}
//输出
void print(int *p, int length)
{
for (int i = 0; i < length; i++)
{
cout << p[i] << endl;
}
}
/*
快速排序
*/
int getPartion(int *array, int low, int high)
{
int key = array[low];
while (low < high)
{
while (low < high && key <= array[high]) //如果array[high]大于键值,那么本就应该在键值右边
high--; //因此将high下标向前移动,直至找到比键值小的值,此时交换这两个值
swap(array[low], array[high]);
while (low < high && key >= array[low])
low++;
swap(array[low], array[high]);
}
return low;//返回key值的下标
}
void QuitSort(int *buf, int low, int high)
{
if (low < high)
{
int key = getPartion(buf, low, high);
QuitSort(buf, low, key - 1);
QuitSort(buf, key + 1, high);
}
}
int main(int argc, char *args[])
{
int x;
cin >> x;
int buf[10];
for (int ii = 0; ii<x; ii++)
{
cin >> buf[ii];
}
cout << "排序前:" << endl;
print(buf, x);
QuitSort(buf, 0, x - 1);
cout << "\n\n\n排序后:" << endl;
print(buf, x);
}
快速排序
最新推荐文章于 2024-09-18 18:05:43 发布