【数据结构】--2.排序算法

常见的排序算法 :冒泡排序 、选择排序、插入排序、归并排序、快速排序、堆排序

 https://www.cnblogs.com/eniac12/p/5329396.html

#include<iostream>
using namespace std;

void Swap(int A[],int i,int j)
{
	int temp=A[j];
	A[j]=A[i];
	A[i]=temp;
}

//冒泡法      平均时间复杂度 o(n^2)
void BubbloSort(int A[],int n)  
//void BubbloSort(int *A,int n)
//因为不能拷贝数组,所以我们无法以值传递的方式使用数组参数。
//因为数组会被转换成指针,所以当我们为函数传递一个数组时,
//实际上传递的是指向数组首元素的指针。
{
if(A==NULL*&&n<2)
	{
		return;
	}
	for(int i=0;i<n-1;i++)
	{
		for(int j=0;j<n-1-i;j++)
			if(A[j]>A[j+1])
				Swap(A,j,j+1);  //数组A被转换成int*并指向A[0]
	}
}

//鸡尾酒排序  平均时间复杂度 o(n^2)
void CocktailSort(int *A,int n)
{
	if(n<0)
		return ;
	int left=0;
	int right=n-1;
	while(left<right)
	{
		for(int i=left;i<right;i++)  //前半轮,排序最大数
		{	if(A[i]>A[i+1])
				Swap(A,i,i+1);
		}
		right--;
		for(int i=right;i>0;i--)    //后半轮,排序最小数
		{
			if(A[i-1]>A[i])
				Swap(A,i-1,i);
		}
		left++;
	}
}

//选择排序  不稳定排序  O(n^2)
void SelectionSort(int A[],int n)
{
if(A==NULL*&&n<2)
	{
		return;
	}
	for(int i=0;i<n-1;i++)
	{
		int min=i;
		for(int j=i+1;j<n;i++)
		{
			if(A[j]<A[min])
				min=j;
		}
		if(min!=i)
			Swap(A,min,i);
	}
}

//插入排序
void InsertSort(int *A,int len)
{
	if(A==NULL*&&n<2)
	{
		return;
	}
	for(int i=1;i<n;i++)
	{
		for(int j=i-1;j>=0&&A[j]>A[j+1];j--)
			swap(A,j,j+1);
	}
}

//归并排序
void MergeSort(int *A,int len)
{
	if(A==NULL*&&len<2)
	{
		return;
	}
	mergeSort(A,0,len-1)
	
}
//递归过程
void mergeSort(int *A,int L,int R)
{
	if(L==R)
		return;
	int mid=L+(R-L)>>1;
	mergeSort(A,L,mid);
	mergeSort(A,mid+1,R);
	merge(A,L,mid,R);
}
//左右两部分排序
void merge(int *A,int left,int midum,int right)
{
	int help[]=new int[right-left+1];
	int i=0;
	int p=left;
	int q=midum+1;
	while(p<=midum&&q<=right)
		help[i++]=A[p]<A[q]?A[p++]:A[q++];
	while(p<=midum)
		help[i++]=A[p++];
	while(q<=right)
		help[i++]=A[q++];
	for(int i=0;i<help.length();i++)
		A[left+i]=help[i++];
}

//随机快速排序  荷兰国旗问题
void QuickSoit(int *A,int len)
{
	if(A==NULL*&&len<2)
	{
		return;
	}
	quickSort(A,0,len-1);
}

void quickSort(int *A,int L,int R)
{
	if(L<R)
	{
		swap(A,(int) (Math.random() * (R - L + 1)),R);
		int Array[]=partiton(A,L,R);
		quickSort(A,0,Array[0]-1);
		quickSort(A,Array[1]+1,R);
	}
}
int partiton(int *A,int L,int R)
{
	int less=L-1;
	int more=R;
	
	while(L<more)
	{
		if(A[L]<A[R])
			swap(A,++less,L++);
		else if(A[L]>A[R])
			swap(A,--more,L);
		else
			L++;
	}
	swap(A,more,R);
	return new int []{less+1,more};
}

//堆排序
void HeapInsert(int *A,int len)
{
	if(A==NULL*&&len<2)
	{
		return;
	}
	for(int i=0;i<len;i++)
	{
		heapInsert(A,i);
	}
	int heapSize=len;
	swap(A,0,--heapSize);
	while(heapSize>0)
	{
		heapify(A,0,heapSize);
		swap(A,0,--heapSize);
	}
}
void heapInsert(int *A,int index)
{
	while(A[index]>A[(index-1)/2])
	{
		swap(A,index,(index-1)/2);
		index=(index-1)/2;
	}
}

void heapify(int *A,int index,int heapSize)
{
	int left=index*2+1;
	while(left<heapSize){
		int largest=left+1<heapSize&&A[left+1]>A[left]?left+1:left;
		largest=A[index]>A[largest]?index:largest;
		if(index==largest)
			break;
		swap(A,index,largest);
		index=largest;
		left=index*2+1;
	}
}





猜你喜欢

转载自blog.csdn.net/wxq_1993/article/details/83549791