快速排序(quicksort)代码展示
#inlcude <#iostream>
using namespace std;
int sort(int a[], int low, int high);
void quickSort(int a[], int low, int high);
int main()
{
int a[] = {1,3,2,7,9,10,2,15};
int length = sizeof(a) / sizeof(a[0]);
quickSort(a, 0, length - 1);
for (auto x : a) {
printf("%d\n", x);
}
return 0;
}
int sort(int a[], int low, int high) {
int temp = a[low];
int i=low, j= high;
while(i<j){
while (a[j] >= temp && i < j) {
j–;
}
a[i] = a[j];
while (a[i] <= temp && i < j) {
i++;
}
a[j] = a[i];
}
a[j] = temp;
return j;
}
void quickSort(int a[], int low ,int high) {
if (low > high)
return;
int j = sort(a, low ,high);
quickSort(a, low, j-1);
quickSort(a, j+1, high);
}
C++ 快速排序(quicksort)代码展示
最新推荐文章于 2023-12-30 16:38:57 发布