常见的比较排序如下:

常用的排序_第1张图片

以下就用C++一个个实现它们

#include
#include
using namespace std;


//插入排序(便于看复杂度)
template
void InsertSort1(T arr[],const int& size)
{
	for(int i=0;i=0;end--)
		{
			if(arr[end-1]>arr[end])
			{
				swap(arr[end],arr[end-1]);
			}
		}
	}
}

template
void InsertSort2(T arr[],const int& size)
{
	for(int i=0;i=0)
		{	
			if(arr[end]>tmp)
			{
				arr[end+1]=arr[end];
				end--;
			}
			else
				break;
		}
		arr[end+1]=tmp;
	}
}


//希尔排序
template
void ShellSort(T arr[],const int& size)
{
	int gap=size;
	while(gap>1)
	{
		gap=gap/3+1;
		for(int i=0;i=0)
			{
				if(arr[end]>tmp)
				{
					arr[end+gap]=arr[end];
					end-=gap;
				}
				else
					break;
			}
			arr[end+gap]=tmp;
		}
	}
}


//选择排序
template
void SelectSort(T arr[],const int& size)
{
	for(int i=0;i
void HeapSort(T array[],const int size)
{
	int Size=size;
	while(Size>1)
	{
		for(int i=(Size-2)/2;i>=0;i--)
		{
			AdjustDown(array,i,Size);
		}
		swap(array[0],array[Size-1]);
		Size-=1;
	}	
}

template
void AdjustDown(T array[],int parent,int size)
{
	int child=parent*2+1;
	while(childarray[child]))
			swap(array[child],array[child+1]);
		if(array[child]>array[parent])
		{
			swap(array[child],array[parent]);
			parent=child;
			child=parent*2+1;
		}
		else
			break;
	}
}


//冒泡排序
//依次比较相邻两个数的大小,小的在前,大的在后,比如a[0]和a[1]比,然后a[1]和a[2]....比完第一轮,最大的数已经排在最后。
//第二轮和第一轮一样,a[0]和a[1],只不过,上一轮已经比出了比较大的数,所以比上一轮少比一次
//缺陷:
//时间复杂度:
template
void BubbleSort(T arr[],const int& size)
{
	for(int i=0;iarr[j+1])
				swap(arr[j],arr[j+1]);
		}
	}
}


//自己按脑袋的思路写的
template
void Sort(T arr[],const int& size)
{
	T Tag=size-1;
	for(int i=0;iarr[Tag])
			{
				swap(arr[end],arr[Tag]);
			}
			else
				end--;
			if(arr[start]>arr[Tag])
			{
				swap(arr[start],arr[Tag]);
			}
			else
				start++;
		}
		Tag--;
	}
}

//快速排序
template
void QuickSort(T arr[],int size)
{
	int start=0;
	int end=size-2;
	_QuickSort(arr,start,end);
}

template
void _QuickSort(T arr[],int start,int end)
{
	if(start
T& PartSort(T arr[],int start,int end)
{
	//T key=arr[end+1];
 	int KeyIndex=end+1;
	while(startstart)
		{
			start++;
		}
		//右边找小的
		while(arr[end]>arr[KeyIndex]&&end>start)
		{
			end--;
		}
		if(end<=start)
			swap(arr[end],arr[KeyIndex]);
		else
			swap(arr[start],arr[end]);
	}
	//只有两个数
	if(start==end&&arr[end]>arr[KeyIndex])
	{
		swap(arr[end],arr[KeyIndex]);
	}
	return start;
}


//归并排序
template
void MergeSort(T arr[],int size)
{
	T* tmp=new T[size];
	_MergeSort(arr,tmp,0,size-1);
	delete[] tmp;
}

template
void _MergeSort(T* src,T* dest,int left,int right)
{
	//递归分割
	if(left>1);
		_MergeSort(src,dest,left,mid);
		_MergeSort(src,dest,mid+1,right);
		//合并
		Merge(src,dest,left,mid,mid+1,right);
		memcpy(src+left,dest+left,(right-left+1)*sizeof(int));
	}
}

template
void Merge(T* src,T* dest,int left1,int right1,int left2,int right2)
{
	//类似合并两个单链表
	assert(src&&dest);
	int i=left1;
	while((left1<=right1)&&(left2<=right2))
	{
		if(src[left1]<=src[left2])
		{
			dest[i++]=src[left1++];
		}
		else
		{
			dest[i++]=src[left2++];
		}
	}
	while(left1<=right1)
	{
		dest[i++]=src[left1++];
	}
	while(left2<=right2)
	{
		dest[i++]=src[left2++];
	}
}


template
void Print(T arr[],const int& size)
{
	for(int i=0;i(array,len);
	Print(array,len);*/
	/*InsertSort2(array,len);
	Print(array,len);*/
	/*ShellSort(array,len);
	Print(array,len);*/
	//SelectSort(array,len);
	//Print(array,len);
	/*BubbleSort(array,len);
	Print(array,len);*/
	//数组的引用?
	/*QuickSort(array,len);
	Print(array,len);*/
    MergeSort(array,len);
	Print(array,len);
}


int main()
{
	Test1();
	system("pause");
	return 0;
}

分析比较各排序常用的排序_第2张图片