各种排序算法的练习

要准备面试笔试了,很多基础知识需要巩固下。排序和查找是必须要掌握的基础知识,参考链接如下:

http://www.cnblogs.com/kkun/archive/2011/11/23/2260312.html


代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

void bubble_sort(int *a, int len)
{
	int i,j;
	for(i=0; i a[j])
			{
				int tmp = a[j];
				a[j] = a[i];
				a[i] = tmp;
			}
		}
	}
}
void insert_sort(int *a, int len)
{
	for(int i=1; i= 0; j--)
		{
			if(tmp < a[j])
			{
				a[j+1] = a[j];
			}
			else
				break;
		}
		a[j+1] = tmp;
	}
}
void bin_insert_sort(int *a, int len)
{
	int guard = a[0];
	for(int i=1; i= r+1; j--)
				a[j+1] = a[j];
			a[j+1] = guard;
		}
	}
}
void shell_sort(int *a, int len)
{
	int i, j , gap;
	for(gap = len / 2; gap > 0; gap /= 2)
	{
		for(i=0; i < gap; i++)
		{
			for(j = i + gap; j < len; j += gap)
			{
				if(a[j] < a[j-gap])
				{
					int tmp = a[j];
					int k = j - gap;
					while(k >= 0 && a[k] > tmp)
					{
						a[k+gap] = a[k];
						k -= gap;
					}
					a[k+gap] = tmp;
				}
			}
		}
	}
}

int partition(int *a, int st, int ed)
{
	int guard = a[st];
	int l = st;
	int r = ed;
	while(l < r)
	{
		while(l < r && a[r] > guard)
			r--;
		a[l] = a[r];
		while(l < r && a[l] <= guard)
			l++;
		a[r] = a[l];
	}
	a[l] = guard;
	return l;
}
void quick_sort(int *a,int st, int ed)
{
	if(!a)
		return ;
	int loc = 0;
	if(st < ed)
	{
		loc = partition(a, st, ed);
		quick_sort(a, st, loc - 1);
		quick_sort(a, loc + 1, ed);
	}
}

void merge(int *a, int st, int mid, int ed, int *tmp)
{
	if(!a || !tmp)
		return ;
	int i = st;
	int j = mid+1;
	int cnt = st;
	for(; i<=mid && j <= ed;)
	{
		if(a[j] > a[i])
			tmp[cnt++] = a[i++];
		else
			tmp[cnt++] = a[j++];
	}
	while(i <= mid)
		tmp[cnt++] = a[i++];
	while(j <= ed)
		tmp[cnt++] = a[j++];
	for(i = st; i<= ed; i++)
		a[i] = tmp[i];
}

void merge_sort(int *a, int st, int ed, int *tmp)
{
	if(!a)
		return;
	if(st < ed)
	{
		int mid = (st + ed)/2;
		merge_sort(a, st, mid , tmp);
		merge_sort(a, mid + 1, ed, tmp);
		merge(a, st, mid , ed, tmp);
	}
}

void select_sort(int *a, int len)
{
	if(!a )
		return;
	int i ,j;
	for(i=0; i> 1) - 1)
void maxHeapify(int *a, int index, int heapsize)
{
	if(a == NULL) return;
	int i = 2*index + 1;
	int rc = a[index];
	for(; i < heapsize; i= 2*i+1)
	{
		if(i a[i])
			break;
		a[index] = a[i];
		index = i;
	}
	a[index] = rc;
}

void maxHeapBuild(int *a, int heapsize)
{
	int index = PARENT(heapsize - 1);
	for(int i=index; i>=0 ; i--)
	{
		maxHeapify(a, i, heapsize);
	}
}

void maxheap_sort(int *a, int heapsize)
{
	maxHeapBuild(a, heapsize);	

	for(int i = heapsize; i > 0; i --)
	{
		int tmp = a[i-1];
		a[i-1] = a[0];
		a[0] = tmp;

		maxHeapify(a,0,i-1);
	}
}
void minHeapify(int *a, int index, int heapsize)
{
	if(!a )
		return;
	int i = 2*index + 1;
	int rc = a[index];
	for(; i < heapsize; i = i*2 + 1)
	{
		if(i=0; i--)
		minHeapify(a, i, heapsize);
}
void minheap_sort(int *a, int heapsize)
{
	minHeapBuild(a, heapsize);
	for(int i=heapsize; i>0; i--)
	{
		int tmp = a[i-1];
		a[i-1] = a[0];
		a[0] = tmp;

		minHeapify(a, 0, i-1);
	}
}

void bucket_sort(int *a,int len, int maxbucket)
{
	if(!a)
		return;
	if(len < 1)
		return;
	int *tmp = (int *)malloc(sizeof(int) * maxbucket);
	memset(tmp, 0, sizeof(int)*maxbucket);
	for(int i=0; i < len; i++)
		tmp[a[i]]++;
	for(int i=0; i < maxbucket; i++)
	{
		int cnt = tmp[i];
		if(cnt > 0)
		{
			while(cnt--)
				*a++ = i;
		}
	}
	free(tmp);
}


int main()
{
	time_t lt = time(NULL);
	srand(lt);
	int len = 20;
	int data[len];
	vector vdata;
	for(int i=0; i



你可能感兴趣的:(算法学习)