4种排序算法
插入排序,堆排序,归并排序,快速排序
#include<iostream>
#include<stdlib.h>
#include<memory.h>
using namespace std;
void generation(int array[],int len){
int *ptr=array;
for(int i=0;i < len;i++,ptr++){
(*ptr)=rand()%1000;
}
}
void print(int array[],int len){
int *ptr=array;
for(int i=0;i<len;i++,ptr++){
cout<<(*ptr)<<' ';
}
cout<<endl;
}
void InsertSort(int array[],int len){
for(int i=0;i<len-1;i++){
int j;
int max=array[i];
int max_index=i;
for(j=i+1;j<len;j++){
if(max<array[j]){
max=array[j];
max_index=j;
}
}
if(max_index!=i){
for(int k=max_index;k>i;k--){
array[k]=array[k-1];
}
array[i]=max;
}
}
}
//-----------------------MinHeapSort---------------------
void MinHeapFixUp(int heap[],int i){
int j,temp;
temp=heap[i];
j=(i-1)/2;//father node
while(j>=0&&i!=0){
if(heap[j]<=temp) break;
heap[i]=heap[j];//put the bigger one down
i=j;
j=(i-1)/2;
}
heap[i]=temp;
}
/*
void MinHeapAddNumber(int heap[],int n,int data){
heap[n]=data;
MinHeapFixUp(heap,n);
}
*/
void MinHeapFixDown(int heap[],int i,int n){
int j,temp;
temp=heap[i];
j=2*i+1;//son node
while(j<n){
if(j+1<n&&heap[j+1]<heap[j]) j++;//smaller son
if(heap[j]>=temp) break;
heap[i]=heap[j]; //put the smaller one up
i=j;
j=2*i+1;
}
heap[i]=temp;
}
void MakeMinHeap(int heap[],int n){
int i;
for(i=0;i<=(n-1)/2;i++)
MinHeapFixDown(heap,i,n);
for(;i<n;i++)
MinHeapFixUp(heap,i);
//print(heap,n);
}
inline void swap(int &a,int &b){
a=a+b;
b=a-b;
a=a-b;
}
void MinHeapSort(int heap[],int n){
MakeMinHeap(heap,n);
for(int i=n-1;i>0;i--){
swap(heap[0],heap[i]);
MinHeapFixDown(heap,0,i);
}
}
//---------------------MinHeapSort end------------------
//------------------------MergeSort----------------------
void mergearray(int a[],int first,int mid,int last,int temp[]){
int i=first;
int &m=mid;
int j=mid;
int &n=last;
int k=0;
while(i<m && j<n){
if(a[i]>=a[j]) temp[k++]=a[i++];
else temp[k++]=a[j++];
}
while(i<m) temp[k++]=a[i++];
while(j<n) temp[k++]=a[j++];
for(i=0;i<k;i++)
a[i+first]=temp[i];
}
void mergesort(int a[],int first,int last,int temp[]){
int mid=(first+last)/2;
if(first!=mid ){
mergesort(a,first,mid,temp);
mergesort(a,mid,last,temp);
mergearray(a,first,mid,last,temp);
//cout<<first<<' '<<mid<<' '<<last<<endl;
//cout<<"L" ;print(a+first,mid-first);
//cout<<"R" ;print(a+mid,last-mid);
//cout<<"res" ;print(a+first,last-first);
}
}
bool MergeSort(int a[],int n){
int *t=new int[n];
if(t==0) return false;
mergesort(a,0,n,t);
delete t;
return true;
}
//--------------------MergeSort end-------------------
//--------------------QuickSort------------------
int AdjustArray(int a[],int start,int end){
int i=start,j=end;
int x=a[i];
while(i<j){
while(i<j && a[j]<x) j--;
if(i<j) {
a[i]=a[j];
i++;
}
while(i<j && a[i]>x) i++;
if(i<j){
a[j]=a[i];
j--;
}
}
a[i]=x;
return i;
}
void _quicksort(int a[],int start,int end){
if(start<end){
int i=AdjustArray(a,start,end);
_quicksort(a,start,i-1);
_quicksort(a,i+1,end);
}
}
#define QuickSort(a,len) _quicksort(a,0,(len)-1)
//--------------------QuickSort end------------------
int array[10],temp[10];
int main()
{
int len=sizeof(array)/sizeof(int);
generation(array,len);
memcpy(temp,array,len*sizeof(int));
print(temp,len);
cout<<"InsertSort"<<endl;
InsertSort(array,len);
print(array,len);
cout<<"MinHeapSort"<<endl;
memcpy(array,temp,len*sizeof(int));
MinHeapSort(array,len);
print(array,len);
cout<<"MergeSort"<<endl;
memcpy(array,temp,len*sizeof(int));
MergeSort(array,len);
print(array,len);
cout<<"QuickSort"<<endl;
memcpy(array,temp,len*sizeof(int));
QuickSort(array,len);
print(array,10);
return 0;
}