C++ 实现快速排序问题?
程序目的如题,代码如下:#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
template <typename T>
void quicksort(T B[], int lo, int hi);
template <typename T>
int Partition(T B[], int lo, int hi);
int main() {
int a[] = {6,5,0,1,34,9,4,4,4,2,-1};
for(int i = 0; i < 11; i++)
cout << a[i] << ",";
cout << endl;
quicksort(a, 0, 11);
for(int i = 0; i < 11; i++)
cout << a[i] << ",";
cout << endl;
return 0;
}
template <typename T>
void quicksort(T B[], int lo, int hi) {
if(hi - lo < 2) return;
int mi = Partition(B, lo, hi - 1);
quicksort(B, lo, mi);
quicksort(B, mi+1, hi);
}
template <typename T>
int Partition(T B[], int lo, int hi) {
swap(B[lo], B[rand()%(hi - lo)]);
T pivot = B[lo];
while(lo < hi) {
while(pivot <= B[hi])
hi--;
B[lo] = B[hi];
while(B[lo] <= pivot)
lo++;
B[hi] = B[lo];
}
B[lo] = pivot;
return lo;
}
运行结果如下:
6,5,0,1,34,9,4,4,4,2,-1,
1,0,2,4,-1,5,-309542368,20689408,9,34,-309542080,
*** stack smashing detected ***: ./test terminated
Aborted (core dumped)
请各位看看是哪儿错了?