#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void Swap(int* a,int* b){
int tmp=*a;
*a=*b;
*b=tmp;
}
int Random(int start,int end){
srand(time(NULL));
return start+rand()%(end-start+1);
}
void BubbleSort(int* num,int len){
if(!num||len<=0) return;
bool flag=true;
for(int i=0;i<len && flag;++i){
flag=false;
for(int j=len-1;j>i;j--){
if(num[j]<num[j-1]){
Swap(&num[j],&num[j-1]);
flag=true;
}
}
}
}
void SelectSort(int* num,int len){
if(!num||len<=0) return;
int min;
for(int i=0;i<len;++i){
min=i;
for(int j=len-1;j>i;j--){
if(num[j]<num[min])
min=j;
}
if(min!=i)
Swap(&num[i],&num[min]);
}
}
void InsertSort(int* num,int len){
if(!num||len<=0) return;
for(int flag=1;flag<len;++flag){
int tmp=num[flag];
int j;
for(j=flag-1;num[j]>tmp && j>=0;j--)
num[j+1]=num[j];
num[j+1]=tmp;
}
}
//shell sort
void ShellSortCore(int* num,int len,int d){
for(int flag=d;flag<len;flag++){
int tmp=num[flag];
int j;
for(j=flag-d;num[j]>tmp && j>=0;j-=d)
num[j+d]=num[j];
num[j+d]=tmp;
}
}
void ShellSort(int* num,int len){
if(!num||len<=0) return;
int d=len/2;
while(d>0){
ShellSortCore(num,len,d);
d/=2;
}
}
//heap sort
void HeapAdjust(int* num,int len,int adjust){
int child=2*adjust+1;
while(child<len){
if(child+1<len && num[child]<num[child+1])
child++;
if(num[child]>num[adjust]){
Swap(&num[child],&num[adjust]);
adjust=child;
child=2*adjust+1;
}
else
break;
}
}
void HeapSort(int* num,int len){
if(!num||len<=0) return;
for(int i=len/2-1;i>=0;--i)
HeapAdjust(num,len,i);
for(int i=len-1;i>=0;--i){
Swap(&num[0],&num[i]);
HeapAdjust(num,i,0);
}
}
//merge sort
void Merge(int* tmp,int* res,int start,int mid,int end){
int i=mid,j=end;
while(i>=start&&j>mid){//start!!! not 0
if(tmp[i]>tmp[j])
res[end--]=tmp[i--];
else
res[end--]=tmp[j--];
}
while(i>=start)//start!!! not 0
res[end--]=tmp[i--];
while(j>mid)
res[end--]=tmp[j--];
}
void MergeSortCore(int* num,int* res,int len,int start,int end){
if(start==end){
res[start]=num[start];
return;
}
int* tmp=new int[len];
int mid=(start+end)/2;
MergeSortCore(num,tmp,len,start,mid);
MergeSortCore(num,tmp,len,mid+1,end);
Merge(tmp,res,start,mid,end);
delete[] tmp;
}
void MergeSort(int* num,int len){
if(!num||len<=0) return;
int start=0,end=len-1;
int* res=new int[len];
MergeSortCore(num,res,len,start,end);
for(int i=0;i<len;++i){
cout<<res[i]<<' ';
}
delete[] res;
}
//quick sort
int Partition(int* num,int len,int start,int end){
int index=Random(start,end);
Swap(&num[end],&num[index]);
int flag=start;//start!!! not 0
for(int i=start;i<end;++i){
if(num[i]<num[end]){
if(i>flag)
Swap(&num[i],&num[flag]);
flag++;
}
}
Swap(&num[flag],&num[end]);
return flag;
}
void QuickSortCore(int* num,int len,int start,int end){
if(start==end) return;
int flag=Partition(num,len,start,end);
if(flag<end)
QuickSortCore(num,len,flag+1,end);
if(flag>start)
QuickSortCore(num,len,start,flag-1);
}
void QuickSort(int* num,int len){
if(!num||len<=0) return;
int start=0,end=len-1;
QuickSortCore(num,len,start,end);
}
int main(){
int num[]={2,1,4,3,9,8,7,10};
QuickSort(num,sizeof(num)/sizeof(*num));
//MergeSort(num,sizeof(num)/sizeof(*num));
//HeapSort(num,sizeof(num)/sizeof(*num));
//ShellSort(num,sizeof(num)/sizeof(*num));
//InsertSort(num,sizeof(num)/sizeof(*num));
//SelectSort(num,sizeof(num)/sizeof(*num));
//BubbleSort(num,sizeof(num)/sizeof(*num));
for(int i=0;i<sizeof(num)/sizeof(*num);++i){
cout<<num[i]<<' ';
}
}