码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构与算法分析-排序

时间:2015-06-04 15:49:33      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

作者:xiabodan 出处:http://blog.csdn.net/xiabodan 

排序算法(Sorting Algorithm)是计算机算法的一个组成部分。也是程序=算法+数据结构中的一部分(算法)。

实验平台:raspberry 2 B + Ubuntu Mate 

插入排序

//插入排序
//stable
//O(N^2) comparisons and swaps
//Adaptive: O(n) time when nearly sorted
//Very low overhead
void insertion(elementtype A[],int n)
{
	int p = 0 ;
	int j = 0 ;
	for(p=1;p<n;p++ )
	{
		elementtype tem = A[p] ; 
		for(j=p;j>0&&A[j-1]>tem;j--)
		{
			 A[j] = A[j-1];
		}
		A[j] = tem;
	}
			
}

希尔排序

//希尔排序
//O(N^3/2)   unstable
//Adaptive: O(N.lg(N)) time when nearly sorted
//Very low overhead
void shell(elementtype A[],int n)
{
	int i,j,inc;
	elementtype tem;

	for(inc=N/2;inc>0;inc /=2)
	{
		for(i=inc;i<N;i++)
		{
			tem = A[i];
			for(j=i;j>=inc;j-=inc)
			{
				if(tem<A[j-inc])
					A[j] = A[j-inc];
				else
					break;
			}
			A[j] = tem;
		}
	}
}

冒泡排序

//冒泡排序
//O(N^2)   stable
//Adaptive: O(N) time when nearly sorted
//Very low overhead
void bubble(elementtype A[],int n)
{
	int flag = 1;
	int i,j;
	for(i=0;i<n;i++)
	{
		flag = 0;
		for(j=n-1;j>i;j--)
		{
			if(A[j]<A[j-1])
			{
				flag = 1;
				swap(A+j,A+j-1 );
			}
				
		}
		if(flag == 0) break;
	}
}


选择排序

//选择排序
//Not stable
//O(1) extra space
//Θ(n2) comparisons
//Θ(n) swaps
//Not adaptive
void selection(elementtype A[],int n)
{
	int i,j;
	int k;
	for(i=0;i<n;i++)
	{
		k = i;
		for(j=i+1;j<n;j++)
		{
			if(A[j]<A[k])
			{	
				k = j;
			}
		}

		
		swap(A+i,A+k);
	}
}


快速排序

//快速排序
//not Stable
//O(lg(n)) extra space (see discussion)
//O(n2) time, but typically O(n·lg(n)) time
//Not adaptive
#define CUT 3
elementtype median3(elementtype A[],int left ,int right)
{
	int center = (left +right) / 2;
	if(A[left]>A[center])
		swap(&A[left],&A[center]);
	if(A[left]>A[right])
		swap(&A[left],&A[right]);
	if(A[center]>A[right])
		swap(&A[center],&A[right]);

	swap(&A[center],&A[right-1]);

	return A[right-1];
}
void Qsort(elementtype A[],int left, int right)
{
	int i,j;
	elementtype pivot;

	if(left + CUT<= right)
	{
		pivot = median3(A,left,right); //select middle element as pivot
		i = left;j = right-1;
		for(;;)
		{
			while(A[++i]<pivot){}

			while(A[--j]>pivot){}
			if(i<j)
				swap(&A[i],&A[j]);
			else
				break;
		}
		swap(&A[i],&A[right-1]);

		Qsort(A,left,i-1);
		Qsort(A,i+1,right);
	}
	else
		insertion(A+left,right-left+1);
}
void quick1(elementtype A[],int n)
{
	Qsort(A,0,n-1);
}


未完待续.......


总结:

     冒泡和插入是慢慢找到最大或最小的放在第一个去;选择直接找到最大或者最小放到第一个;归并、快排都用了devide-merge-conquer(分治策略),期间还是会用到前面提到的那几种最基本的算法;堆排序用了选择排序的思想;桶排序用了空间换时间的方法;万变不离其宗。

参考:

     数据结构与算法分析-C语言描述[M],机械工业出版社

     博客园 vamei的博客:http://www.cnblogs.com/vamei/archive/2013/03/12/2948847.html

         天津城市学院一个精品课程:http://sjjp.tjuci.edu.cn/sjjg/datastructure/ds/web/paixu/paixu8.1.1.1.htm

国外一个排序网站有动画,分析,为代码: http://www.sorting-algorithms.com/

数据结构与算法分析-排序

标签:

原文地址:http://blog.csdn.net/xiabodan/article/details/46360675

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!