几种排序算法的代码

#include <iostream>
using namespace std;

//insert sort
void insertSort(int a[],int size)
{
	int tmp;
	int i,j;
	for(i = 1 ; i < size ; i++)
	{
		tmp = a[i];
		for(j = i - 1 ; j >=0 ; j--)
		{
			if(a[j] > tmp)
				a[j + 1] = a[j];
			else
				break;
		}
		a[j + 1] = tmp;
	}
}
/*insert sort*/
void insertSort2(int *a,int size)
{
	int i,j,tmp;
	for (i = 1 ; i < size ; i++)
	{
		tmp = a[i];
		j = i - 1;
		while (j >= 0 && a[j] > tmp)
		{
			a[j + 1] = a[j];
			j--;
		}
		a[j + 1] = tmp;
	}
}
/*shell sort*/
void shellSort(int *a,int size)
{
	for (int gap = size/2 ; gap > 0 ; gap/=2)
	{
		for (int i = gap ; i < size ; i++)
		{
			int tmp = a[i];
			int j = i;
			for(;j >= gap && tmp <a[j - gap];j-=gap)
				a[j] = a[j-gap];
			a[j] = tmp;
		}
	}
}

//heap sort
void percDown(int *a,int i,int size);
void heapSort(int *a,int size)
{
	for(int i = size/2 ; i >= 0 ; i--)
		percDown(a,i,size);
	for(int j = size-1;j>0;j--)
	{
		swap(a[0],a[j]);
		percDown(a,0,j);
	}
}
inline int leftchild(int i)
{
	return 2*i + 1;
}
void percDown(int *a,int i,int size)
{
	int child;
	int tmp;
	for(tmp = a[i] ; leftchild(i) < size ; i = child)
	{
		child = leftchild(i);
		if(child != size - 1 && a[child] < a[child + 1])
			child++;
		if(tmp < a[child])
			a[i] = a[child];
		else
			break;
	}
	a[i] = tmp;
		
}

//merge sort
void merge(int *a , int begin , int mid , int end)
{
	int *tmp = new int[end - begin + 1];
	int i,begin1,begin2;
	for (i = 0 , begin1 = begin,begin2 = mid + 1 ; begin1 <= mid && begin2 <= end ; i++)
	{
		tmp[i] = a[begin1] < a[begin2] ? a[begin1++]:a[begin2++];
	}
	while(begin1 <= mid)
		tmp[i++] = a[begin1++];
	while(begin2 <= end)
		tmp[i++] = a[begin2++];
	for(int j = 0 ; j < end - begin + 1 ; j++)
		a[begin + j ] = tmp[j];
	delete[] tmp;
}
void mergeSort(int *a,int begin,int end)
{
	if(begin < end)
	{
		int mid = ( begin + end ) / 2;
		mergeSort(a , begin , mid);
		mergeSort(a , mid + 1 , end);
		merge(a , begin , mid , end);
	}
}

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

//bubble sort
void bubbleSort(int *a,int size)
{
	for(int i = 0 ; i < size ; i++)
		for(int j = i + 1 ; j < size ; j++)
			if(a[i] > a[j])
				swap(a[i],a[j]);
}

//selection sort
void selectionSort(int *a,int size)
{
	int min,i,j;
	for(i = 0 ; i < size ; i++)
	{
		min = i;
		for(j = i + 1 ; j < size ; j++)
		{
			if(a[j] < min)
				min = j;
		}
		if(min != i)
			swap(a[min],a[i]);
	}
}
int main()
{
	int a[6] = {34,8,64,51,32,61};

	//insert sort
	insertSort2(a,6);
	cout<<"insert sort:"<<endl;
	for(int i = 0 ; i < 6 ; i++)
		cout<<a[i]<<" ";
	cout<<endl<<endl;

	//shell sort
	cout<<"shell sort:"<<endl;
	shellSort(a,6);
	for(int i = 0 ; i < 6 ; i++)
		cout<<a[i]<<" ";
	cout<<endl<<endl;

	//heap sort
	cout<<"heap sort:"<<endl;
	heapSort(a,6);
	for(int i = 0 ; i < 6 ; i++)
		cout<<a[i]<<" ";
	cout<<endl<<endl;
	//merge sort
	cout<<"merge sort:"<<endl;
	int begin = 0 , end = sizeof(a)/sizeof(int) - 1;
	mergeSort(a,begin,end);
	for(int i = 0 ; i < 6 ; i++)
		cout<<a[i]<<" ";
	cout<<endl<<endl;

	//quick sort
	cout<<"quick sort:"<<endl;
	int left=0,right=sizeof(a)/sizeof(int)-1;
	quickSort(a,left,right);
	for(int i = 0 ; i < 6 ; i++)
		cout<<a[i]<<" ";
	cout<<endl<<endl;

	//bubble sort
	cout<<"bubble sort:"<<endl;
	bubbleSort(a,6);
	for(int i = 0 ; i < 6 ; i++)
		cout<<a[i]<<" ";
	cout<<endl<<endl;

	//selection sort
	cout<<"selection sort:"<<endl;
	selectionSort(a,6);
	for(int i = 0 ; i < 6 ; i++)
		cout<<a[i]<<" ";
	cout<<endl<<endl;

}

你可能感兴趣的:(几种排序算法的代码)