8大基础排序

#include<iostream>
using namespace std;

//bubble sort
void BubbleSort(int *arr,int left,int right)
{
	int temp;
	for(int i=right-1;i>=left;i--)
	{
		for(int j=left;j<=i;j++)
		{
			if(arr[j]>arr[j+1])
			{
				temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
			}
		}
	}
}

//insert sort
void InsertSort(int *arr,int left,int right)
{
	int temp;
	for(int i=left+1;i<=right;i++)
	{
		temp=arr[i];
		int j;
		for(j=i-1;j>=left;j--)
		{
			if(arr[j]>temp)
				arr[j+1]=arr[j];
			else
				break;
		}
		arr[j+1]=temp;
	}
}

//merge sort
void MergeSort(int *arr,int *list,int left,int right)
{
	if(left>=right)
		return;
	int begin1=left, end1=left+(right-left)/2;
	int begin2=end1+1, end2=right;
	
	MergeSort(arr,list,begin1,end1);
	MergeSort(arr,list,begin2,end2);
	int begin=left;
	while(begin1<=end1 && begin2<=end2)
	{
		if(arr[begin1]<=arr[begin2])
			list[begin++]=arr[begin1++];
		else
			list[begin++]=arr[begin2++];
	}
	if(begin1>end1)
		while(begin2<=end2)
			list[begin++]=arr[begin2++];
	if(begin2>end2)
		while(begin1<=end1)
			list[begin++]=arr[begin1++];	
	for(int t=left;t<=right;t++)
		arr[t]=list[t];
}

//quick sort
void QuickSort(int *arr,int left,int right)
{
	if(left<right)
	{
		int temp=arr[left];
		int i=left,j=right;
		while(i<j)
		{
			while(i<j && arr[j]>=temp)	j--;
			arr[i]=arr[j];
			while(i<j && arr[i]<=temp)	i++;
			arr[j]=arr[i];
		}
		arr[i]=temp;//i==j
		QuickSort(arr,left,i-1);
		QuickSort(arr,i+1,right);
	}
}

//heap sort
void HeapHelper(int *arr,int begin,int end)
{
	int child = 2*begin;
	int temp=arr[begin];
	while(child<=end)
	{
		if(child+1<=end && arr[child+1]>arr[child])
			child++;
		if(arr[child]>temp)
			arr[begin]=arr[child];
		else
			break;
		begin=child;
		child=2*begin;
	}
	arr[begin]=temp;
}
void HeapSort(int *arr,int n)
{
	//build
	for(int i=n/2;i>=1;i--)
	{
		HeapHelper(arr,i,n);
	}
	int temp;
	for(int i=n;i>=2;i--)
	{
		temp=arr[1];
		arr[1]=arr[i];
		arr[i]=temp;
		HeapHelper(arr,1,i-1);
	}
}

//counting sort
void CountSort(int *A,int *B,int *C,int tIndex,int kk)
{
	for(int i=0;i<=tIndex;i++)
		C[A[i]]++;
	for(int i=1;i<=kk;i++)
		C[i]=C[i]+C[i-1];
	for(int i=tIndex;i>=0;i--)
	{
		//if swap these two sentence,
		//the Array A must begin from 1 instead of 0
		C[A[i]]--;
		B[C[A[i]]]=A[i];
	}
}

//shell sort
void ShellSort(int *arr,int left,int right)
{
	int len=right-left+1;
	for(int gap=len/2;gap>0;gap--)
	{
		for(int i=gap+left;i<=right;i++)
		{
			int temp=arr[i];
			int j;
			for(j=i-gap;j>=0 && arr[j]>temp;j-=gap)
				arr[j+gap]=arr[j];
			arr[j+gap]=temp;
		}
	}
}

//radix sort
//n is the number of arr,d is the longest digit of arr
void RadixSort(int *arr,int n,int d)//
{
	int *tmp=new int[n];
	int *count=new int[10];
	int radix=1;
	for(int i=1;i<=d;i++)
	{
		for(int t=0;t<10;t++)
			count[t]=0;
		for(int t=0;t<n;t++)
		{
			int k=(arr[t]/radix)%10;
			count[k]++;
		}
		for(int t=1;t<10;t++)
			count[t]+=count[t-1];
		for(int t=n-1;t>=0;t--)
		{
			int k=(arr[t]/radix)%10;
			tmp[--count[k]]=arr[t];
			
		}
		for(int t=0;t<n;t++)
			arr[t]=tmp[t];
		radix*=10;
	}
}

int main()
{
	int arr[11]={6,2,9,3,4,5,1,7,8,5,6};
	int list[11];
	MergeSort(arr,list,0,10);
	for(int i=0;i<11;i++)
		cout<<" "<<arr[i];
	cout<<endl;

	//Counting Sort
	const int N=11;
	int A[N]={6,2,9,3,4,5,1,7,8,5,6};
	int k=9;
	int *C = new int[k+1];
	for(int i=0;i<k+1;i++)	C[i]=0;
	int *B =new int[11];
	CountSort(A,B,C,N-1,k);
	for(int i=0;i<11;i++)
		cout<<" "<<B[i];
	cout<<endl;

	//radix sort
	int a[8]={375,572,169,343,541,872,235,216};
	RadixSort(a,8,3);
	for(int i=0;i<8;i++)
		cout<<" "<<a[i];
	cout<<endl;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值