一些排序总结

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

/*
种类		时间复杂度		空间复杂度		稳定性		是否与初始序列有关
冒泡排序	    O(n^2) 			O(1)			稳定 		有 
插入排序	    O(n^2)			O(1)			稳定 		有 
选择排序	    O(n^2) 			O(1)			不稳定 		无关 
希尔排序 	O(n^1.3)(平均)		O(1)			不稳定		有 
快速排序	    O(nlogn)(平均)		O(n)			不稳定 		有 
归并排序	    O(nlogn)		        O(n)			稳定 		有 
堆排序 	    O(nlogn)		        O(1)			不稳定 		无关 
 
综合来看 堆排时间复杂度和空间复杂度最优 但是不稳定。 

*/
class Sort
{
public:
	void BubbleSort(int a[],int len);
	void SelectSort(int a[],int len);
	void InsertSort(int a[],int len);
	void ShellSort(int a[],int len);
	void QuickSort(int a[],int len);
	void MergerSort(int a[],int len);
	void Print(int a[],int len);
	void HeapSort(int a[],int len);
private:
	void swap(int a[],int i,int j);
	int Partition(int a[],int low,int high);
	void QSort(int a[],int low,int high);
	void Merger(int src[],int dest[],int low,int mid,int high);
	void MSort(int src[],int dest[],int low,int high,int max);
	void AdjustHeap(int a[],int start,int end);
};
void Sort::HeapSort(int a[],int len)
{
	for(int i=len/2-1;i>=0;i--)//从最后一个不是叶子节点开始 ,从左到右,从 下往上构建最大堆 时间复杂度是O(n) 
	{
		AdjustHeap(a,i,len); 
	}
	for(int j=len-1;j>0;j--)
	{
		swap(a,0,j);
		AdjustHeap(a,0,j);//下标没有到j,实际调整的是0到j-1 
	}
}
void Sort::AdjustHeap(int a[],int start,int end)
{
	int tmp=a[start];
	int dad=start; //最终的目的是找到dad的位置。 
	for(int son=dad*2+1;son<end;son=son*2+1)
	{
		if((son+1<end)&&(a[son+1]>a[son]))
		{
			son++;
		}
		if(a[son]>tmp)
		{
			a[dad]=a[son];
			dad=son;
		}
		else 
		{
			break;
		}
	}
	a[dad]=tmp;
	
}
void Sort::MergerSort(int a[],int len)
{
	MSort(a,a,0,len-1,len);
}
void Sort::MSort(int src[],int dest[],int low,int high,int max)
{
	if(low==high)
	{
		dest[low]=src[low];
	}
	else 
	{
		int mid=(low+high)/2;
		int *space=(int*)malloc(sizeof(int)*max);
		if(space!=NULL)
		{
			MSort(src,space,low,mid,max);
			MSort(src,space,mid+1,high,max);
			Merger(space,dest,low,mid,high);
		}
		free(space);
	}
}
void Sort::Merger(int src[],int dest[],int low,int mid,int high)
{
	int i=low;
	int j=mid+1;
	int k=low;
	while((i<=mid)&&(j<=high))
	{
		if(src[i]<=src[j])
		{
			dest[k++]=src[i++];
		}
		else
		{
			dest[k++]=src[j++];
		}
	}
	while(i<=mid)
	{
		dest[k++]=src[i++];
	}
	while(j<=high)
	{
		dest[k++]=src[j++];
	}
}
int Sort::Partition(int a[],int low,int high)
{
	int pv=a[low];
	while(low<high)
	{
		while((low<high)&&(a[high]>=pv))
		{
			high--;
		}
		swap(a,low,high);
		while((low<high)&&(a[low]<=pv))
		{
			low++;
		}
		swap(a,low,high);
	}
	return low;
}
void Sort::QSort(int a[],int low ,int high)
{
	if(low<high)
	{
		int tmp=Partition(a,low,high);
		QSort(a,low,tmp-1);
		QSort(a,tmp+1,high);
	}
}
void Sort::QuickSort(int a[],int len)
{
	QSort(a,0,len-1);
}
void Sort::Print(int a[],int len)
{
	for(int i=0;i<len;i++)
	{
		cout<<a[i]<<' ';
	}
	cout<<endl;
}
void Sort::swap(int a[],int i,int j)
{
	int tmp=a[i];
	a[i]=a[j];
	a[j]=tmp;
}
void Sort::BubbleSort(int a[],int len)
{
	int i,j;
	int tmp=1;
	for(i=0;i<len-1&&tmp;i++)
	{
		tmp=0;
		for(j=0;j<len-1;j++)
		{
			if((a[j]>a[j+1]))
			{
				swap(a,j+1,j);
				tmp=1;
			}
		}
	}
}
void Sort::SelectSort(int a[],int len)
{
	int i,j,k;
	for(i=0;i<len;i++)
	{
		k=i;
		for(j=i+1;j<len;j++)
		{
			if(a[j]<a[k])
			{
				k=j;
			}
		}
		swap(a,i,k);
	}
}
void Sort::InsertSort(int a[],int len)
{
	int i,j;
	for(i=1;i<len;i++)
	{
		int tmp=a[i];
		for(j=i-1;j>=0&&(a[j]>tmp);j--)
		{
				a[j+1]=a[j];
		}
		a[j+1]=tmp;
	}
}
void Sort::ShellSort(int a[],int len)
{
	int i,j;
	int gap=len;
	do
	{
		gap=gap/3+1;
		for(i=gap;i<len;i+=gap)
		{
			int tmp=a[i];
			for(j=i-gap;j>=0&&(a[j]>tmp);j-=gap)
			{
				a[j+gap]=a[j];	
			}
			a[j+gap]=tmp;
		}
	}
	while(gap>1);
}
int main(void)
{
	int a[]={2,4,3,1,5,6,8,0,7,9};
	int len=sizeof(a)/sizeof(*a);
	Sort test;
	test.Print(a,len);
	//test.BubbleSort(a,len);
	//test.SelectSort(a,len);
	//test.InsertSort(a,len);
	//test.ShellSort(a,len);
	//test.QuickSort(a,len);
	//test.MergerSort(a,len);
	test.HeapSort(a,len);
	test.Print(a,len);
	
	
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_33436509/article/details/82154112