//希尔排序
int Sort::shellSort(int *data, int len){
if(data == NULL || len <= 0){
return ERROR;
}
for(int dk = len/2; dk >= 1; dk = dk/2){// step
for(int i = dk; i < len; i ++){
int pirot = data[i];
int j = 0;
for( j = i+1; j > 0 && pirot < data[j]; j-=dk){
data[j+dk] = data[j];
}
data[j] = pirot;
}
}
return OK;
}
/**
对数组data从小到大排序,冒泡排序
**/
int Sort::bubbleSort(int * data, int len){
if( len <0){
return ERROR;
}
bool flag = true;
for( int i = 1; i< len; i++){
flag = true;
for( int j = len-1; j >=i; j--){
if( *(data+j) < *(data+j-1)){
int tmp = *(data+j);
*(data+j) = *(data+j-1);
*(data+j-1) = tmp;
flag = false;
}
}
if( flag){
break;
}
}
return OK;
}
int Sort::bubbleSortArray(int data[], int len){
bool flag = true;
for( int i = 1; i< len; i++){
flag = true;
for( int j = len-1; j >=i; j--){
if(data[j] < data[j-1]){
int tmp = data[j];
data[j] = data[j-1];
data[j-1] = tmp;
flag = false;
}
}
if( flag){
break;
}
}
return OK;
}
/*选择排序:从小到大排序
*/
int Sort::selectSort(int *data, int len){
for( int i = 0; i < len; i++){
int min = *(data+i);
int id = i;
for(int j = i+1; j < len; j++){
if( *(data+j) < min){
min = *(data+j);
id = j;
}
}
//交换data[i]和data[id]
if( i!=id){
int tmp= *(data+i);
*(data+i)= *(data+id);
*(data+id)= tmp;
}
}
return OK;
}
int Sort::selectSortArray(int data[], int len){
for( int i = 0; i < len; i++){
int min = *(data+i);
int id = i;
for(int j = i+1; j < len; j++){
if( *(data+j) < min){
min = *(data+j);
id = j;
}
}
//交换data[i]和data[id]
if( i!=id){
int tmp= *(data+i);
*(data+i)= *(data+id);
*(data+id)= tmp;
}
}
return OK;
}
int quickSortPart(int *data, int startIndex, int endIndex){
if( startIndex >= endIndex){
return ERROR;
}
int pirot = *(data + startIndex);
int start = startIndex, end = endIndex;
while(start < end){
while( (*(data + end) >= pirot) && (end > start)){ // 找一个比转轴小的
end --;
}
if(end != startIndex){
*(data + start) = *(data + end);
start ++;
}
while((*(data + start) <= pirot) && (start < end) ){ // 找一个比转轴大的
start ++;
}
if(start != endIndex){
*(data + end) = *(data + start);
end --;
}
}
data[start] = pirot;
quickSortPart(data, startIndex, start-1);
quickSortPart(data, start+1, endIndex);
return OK;
}
int Sort::quickSort(int *data, int len){
quickSortPart(data, 0, len -1);
return OK;
}
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
//堆排序
int minHeapFix(int * data, int len){
for(int i = (len -1) / 2; i >= 0; i--){
if((2*i+1) < len){
if(*(data + 2*i +1) < *(data+i)){
if( ((2*i+2) < len) && (*(data +2*i + 1) > *(data + 2*i +2))){
swap(*(data+i), *(data + 2*i +2));
}
else{
swap(*(data+i),*(data +2*i +1));
}
}
else{
if(((2*i+2) <len) && (*(data +i) > *(data + 2*i +2))){
swap(*(data+i), *(data + 2*i + 2));
}
}
}
}
return OK;
}
int Sort::heapSort(int *data, int len){
for(int i = len; i > 0; i--){
minHeapFix(data, i);
swap(*data, *(data + i -1));
}
return OK;
}
/***
merge data[low,..,mid] with data[mid + 1,...,high] ,归并排序
***/
int merge(int *data, int low, int mid, int high, int *mData){
int front = low, last = mid + 1;
int i = low;
while(front <= mid && last <= high){
if( data[front] <= data[last]){
mData[i] = data[front];
front ++;
}
else{
mData[i] = data[last];
last ++;
}
i ++;
}
while(front <= mid){
mData[i] = data[front];
front ++;
i ++;
}
while(last <= high){
mData[i] = data[last];
last ++;
i ++;
}
for(int i = low; i <= high; i ++){
data[i] = mData[i];
}
return OK;
}
int mergeSortP(int *data, int low, int high, int *mData, int len){
if( data == NULL || low < 0 || high < low){
return ERROR;
}
if(low < high){
int mid = (high -low)/2 + low;
mergeSortP(data, low, mid, mData,len);
mergeSortP(data, mid + 1, high, mData, len);
merge(data,low,mid, high, mData);
}
return OK;
}
int Sort::mergeSort(int *data, int len){
if(data == NULL || len <= 0){
return ERROR;
}
int *mData = new int[len];
mergeSortP(data, 0, len - 1, mData, len);
delete[] mData;
return OK;
}