头文件:
class QuickSort {
public:
void sort(int *re, int left, int right);
private:
static int partition(int *part, int left, int right);
};
源文件:
#include "QuickSort.h"
void my_swap(int& a, int& b);
void QuickSort::sort(int *re, int left, int right) {
if(left > right) return;
int j = partition(re, left, right);
sort(re, left, j-1);
sort(re, j + 1, right);
}
int QuickSort::partition(int *part, int left, int right) {
int x = *(part + left);
int i = left+1;
int j = right;
while (i <= j) {
while (*(part + i) < x){
i++;
}
while (*(part + j) > x){
j--;
}
if(i < j){
my_swap(part[i++], part[j--]);
} else
i++;
}
my_swap(part[j], part[left]);
return j;
}
void my_swap(int& a, int& b){
int tem = a;
a = b;
b = tem;
}