更正:希尔排序是O(O^1.5)
Code:
#include <iostream>
using namespace std;
//O(n^2)
void BubbleSort(double *array, int length){
if(length<=0||array==NULL) {cout<<"error!!!!"<<endl; return;}
for(int i=0;i<length;i++)
for(int j=1;j<length-i;j++)
if(array[j-1]>array[j])
swap(array[j],array[j-1]);
}
//O(n^2)
void SelectionSort(double *array,int length){
if(length<=0||array==NULL) {cout<<"error!!!!"<<endl; return;}
for(int i=0;i<length;i++){
int max_index=0;
for(int j=0;j<length-i;j++)
if(array[j]>array[max_index]) max_index=j;
if(length-i-1!=0){
double temp=array[max_index];
array[max_index]=array[length-i-1];
array[length-i-1]=temp;
}
}
}
//O(n^2)
void InsertionSort(double *array, int length){
if(length<=0||array==NULL) {cout<<"error!!!!"<<endl; return;}
int j;
for(int i=1;i<length;i++){
double temp=array[i];
for(j=i;j>0;j--){
if(array[j-1]>temp) array[j]=array[j-1];
else break;
}
array[j]=temp;
}
}
//O(n^1.5)
void ShellSort(double *array,int length, int *increment,int in_length){
if(length<=0||array==NULL) {cout<<"error!!!!"<<endl; return;}
for(int i=0;i<in_length;i++)
for(int j=0;j<increment[i];j++)
for(int k=j+increment[i];k<length;k+=increment[i]){
//Insertsort
double temp=array[k];
int s;
for(s=k-increment[i];s>=0;s-=increment[i]){
if(array[s]>temp) array[s+increment[i]] = array[s];
else break;
}
array[s+increment[i]]=temp;
}
}
//O(n*logn)
void QuickSort(double *array,int start, int end){
if(start>=end) return;
int i,j;
i=start; j=end;
double temp=array[i];
while(i<j){
while(array[j]>=temp&&i<j) j--;
array[i]=array[j];
while(array[i]<=temp&&i<j) i++;
array[j]=array[i];
}
array[i]=temp;
QuickSort(array,start,i-1);
QuickSort(array,i+1,end);
}
//mergesort O(nlogn)
void merge(double *array,double *sortarray,int s,int m,int e){
if(m==e) return;
int i=s,j=m+1,k=s;
while(i<=m&&j<=e){
if(array[i]<=array[j]) sortarray[k++]=array[i++];
else sortarray[k++]=array[j++];
}
while(i<=m) sortarray[k++]=array[i++];
while(j<=e) sortarray[k++]=array[j++];
}
void MergeSort(double *array, int length){
if(length<=0) return;
double *sortarray=new double[length];
double *p=array,*q=sortarray;
for(int i=1;i<length;i*=2){
for(int j=0;j<length;j+=(2*i)){
int s=j;
int m=(j+i-1)>(length-1)?(length-1):(j+i-1);
int e=(m+i)>(length-1)?(length-1):(m+i);
merge(p,q,s,m,e);
}
swap(p,q);
}
delete []sortarray;
}
//mergesort
/heapsort O(nlogn)
void Adjust_heap(double *data,int length,int start){
int left=start*2+1;
int current;
int right;
if(left<length){
current=left;
right=left+1;
if(right<length&&data[current]<data[right]) current=right;
if(data[current]>data[start]){
swap(data[start],data[current]);
Adjust_heap(data,length,current);
}
}
}
void Heapsort(double *data, int length){
int i;
for(i=length/2-1;i>=0;i--){
Adjust_heap(data,length,i);
}
for (int j = length-1; j > 0; --j){
swap(data[0],data[j]);
Adjust_heap(data,j,0);
}
}
heapsort
int main(){
double testarray[10]={12,3,1,24,6,9,2,15,11,8};
//double testarray2[10];
//for(int i=0;i<10;i++) cout<<testarray[i]<<" ";
//cout<<endl<<"after sort"<<endl;
//BubbleSort(testarray,10);
//SelectionSort(testarray,10);
InsertionSort(testarray,10);
//int increment[3]={5,3,1}; ShellSort(testarray,10,increment,3);
//QuickSort(testarray,0,9);
//MergeSort(testarray,10);
//heapsort(testarray,10);
for(int i=0;i<10;i++) cout<<testarray[i]<<" ";
return 0;
}
补充:
快速排序非递归版本
void QuickSort(int *a, int left,int right)
{
if (a == NULL || left < 0 || right <= 0 || left>right)
return;
stack<int>temp;
int i, j;
//(注意保存顺序)先将初始状态的左右指针压栈
temp.push(right);//先存右指针
temp.push(left);//再存左指针
while (!temp.empty())
{
i = temp.top();//先弹出左指针
temp.pop();
j = temp.top();//再弹出右指针
temp.pop();
if (i < j)
{
int k = Pritation(a, i, j);
if (k > i)
{
temp.push(k - 1);//保存中间变量
temp.push(i); //保存中间变量
}
if (j > k)
{
temp.push(j);
temp.push(k + 1);
}
}
}
}