#include <iostream>
using namespace std;
void Qsort(int a[], int low, int high);
int main()
{
int a[] = { 6, 1, 2, 7, 9, 3, 4, 5, 10, 8 };
cout << sizeof(a) << '\t' << sizeof(a[0]) << '\t' << sizeof(a) / sizeof(a[0]) << endl; //sizeof(a) 获得的是数组占用的内存大小,并非元素个数
Qsort(a, 0, sizeof(a) / sizeof(a[0]) - 1);
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
cout << a[i] << " ";
}
return 0;
}
void Qsort(int a[], int low, int high)
{
if (low >= high)
return;
int start = low, last = high, key = a[low];
while (start < last)
{
while (start < last && key <= a[last])
{
last--;
}
a[start] = a[last];
while (start < last && key >= a[start])
{
start++;
}
a[last] = a[start];
}
a[start] = key; //在这里索引换成 last 一样效果
Qsort(a, low, start - 1);
Qsort(a, last + 1, high);
}
快速排序代码
最新推荐文章于 2022-10-17 22:44:26 发布