几种排序算法的总结

//写了几种常用的排序算法如下:

#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#define LEN 100
#define POWER2(a) (1<<(a))



void insertSort(int a[]);
void print(int a[]);
void BinsertSort(int a[]);
void shellsort(int a[]);
void bubbleSort(int a[]);
int  Partition(int a[], int low, int high);
void quicksort(int a[], int low, int high);
void selectsort(int a[]);
void heapsort(int a[]);

void heapAdjust(int a[], int s, int m);
int comint(int a, int b);

void main(void)
{
	int a[LEN+1];	
	int i;

	srand((unsigned)time(NULL));

	a[0]=0;
	for(i=1; i<=LEN; i++)
	{
		a[i]=rand()%LEN+1;
	}
	//int a[10]={9,4,5,0,7,4,6,4,5,2};
	print(a);
	printf("\n");
	/******直接插入排序********/
	//insertSort(a);   
	
	/******二分插入排序********/
	//BinsertSort(a);

	/*******希尔排序************/
	//shellsort(a);

	/*********冒泡排序****************/
	//bubbleSort(a);

	/**********快速排序******************/
	//quicksort(a,0,LEN-1);

	/***********选择排序************************/
	//selectsort(a);

	/*********对排序**********/
	heapsort(a);

	printf("排序后的数列:\n");
	print(a);
	printf("\n");
	
}

//基数排序
/************************************
排序思想:1、首先,要求出数的位数
***********************************/
void radixsort(int a[])
{
	
}

int bytenum(int digital)
{
	int byte=1;
	while(digital/10 !=0 )
	{
		byte++;
		digatal=digital/10;
	}
	return byte;
}
//堆排序
/*********************************************
堆排序的思想:首先是建堆,对一个顺序表(长度length)来说,从表尾(即最后一个叶子节点,a[length])
的父节点开始(a[length/2]),做如下操作,然后减1,重复如下操作:

调整堆:从父节点开始,比较该节点,和他的左孩子以及右孩子。
找出最小的值放在父节点,把交换孩子节点作为父节点,向下做如上操作,否则,调整完成。

排序:建成的堆的根节点就是最小值,和i=length,进行交换,然后对1..i-1,重新调整堆


***********************************************/
void heapsort(int a[])
{
	int i;
	int temp;
	//建堆过程
	for(i=LEN/2; i>0; i--) //从最后一个非叶子节点开始,对它下面的堆进行调整
	{
		heapAdjust(a,i,LEN);
	}

	//排序过程,把最小的元素放在最后,然后对前面的元素,进行堆调整
	for(i=LEN; i>1; i--)  
	{
		//swap(a[1],a[i]);
		temp=a[1];
		a[1]=a[i];
		a[i]=temp;
		heapAdjust(a, 1, i-1);
	}
}

//调整堆
void heapAdjust(int a[], int s, int m)
{
	int i;
	int root=a[s];
	for(i=2*s; i<=m; i*=2)
	{
		if(i<m && comint(a[i],a[i+1]))++i; //先对左孩子和有孩子进行比较
		if(!comint(root, a[i]))break;      //和根节点,进行比较,当根节点不动时,说明树
		a[s]=a[i];                         //不需要调整,跳出循环 
		s=i;
	}
	a[s]=root;
}

int comint(int a, int b)
{
	if(a<b)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//直接插入排序
void insertSort(int a[])
{
	int temp;
	int i,j;
	
	for(i=1; i<LEN; i++)
	{
		temp=a[i];
		for(j=i-1; j>=0; j--)
		{
			if(temp<a[j])
			{
				
				a[j+1]=a[j];
				a[j]=temp;
			}
			else
			{
				break;	
			}
		}
	}
}

void print(int a[])
{
	int i;
	for(i=0; i<LEN; i++)
	{
		printf("%d\t",a[i]);
	}
}

//二分插入排序
void BinsertSort(int a[])
{
	int i,j;
	int low, high,mid;
	int temp;

	for(i=1; i<LEN; i++)
	{
		low=0;
		high=i-1;
		temp=a[i];
		while(low<=high)
		{
			mid=(low+high)/2;
			if(a[mid]>temp)
			{
				high=mid-1;
			}
			else 
			{
				low=mid+1;
			}

		}
		for(j=i-1; j>=high+1; j--)
		{
			a[j+1]=a[j];
		}
		a[high+1]=temp;
	}
}


//增量序列取2的k次方减1,希尔排序
void shellsort(int a[])
{
	int h=(int)(log(LEN)/log(2));
	printf("h=%d\n",h);
	int d=0;//增量序列
	int i=0,j=0,k=0,m=0;
	int temp=0;

	for(i=h; i>0; i--)
	{
		d=POWER2(i)-1;
		printf("d=%d\n",d);
		for(j=0; j<d; j++)
		{
			//temp=a[j]
			for(k=j+d; k<LEN; k+=d)
			{
				temp=a[k];
				for(m=k-d; m>=0; m-=d)
				{
					if(temp<a[m])
					{
						a[m+d]=a[m];
						//a[m]=temp;
					}
					else
					{
						a[m+d]=temp;
						break;
					}
				}
			}
		}
	}
}

//定位
int Partition(int a[], int low, int high)
{
	//选定基准点
	int base=a[low];
	while(low<high)
	{
		while(low<high && a[high]>=base)high--;
		a[low]=a[high];
		while(low<high && a[low]<=base)low++;
		a[high]=a[low];
	}
	a[low]=base;
	return low;
}
//快速排序
void quicksort(int a[], int low, int high)
{
	int position;
	
	position=Partition(a,low,high);
	if(low<position-1)
	{
		quicksort(a,low,position-1);
	}
	
	if(position+1<high)
	{
		quicksort(a,position+1,high);
	}
		
}

//选择排序
void selectsort(int a[])
{
	int i,j;
	int mark;
	int temp;
	for(i=0; i<LEN-1; i++)
	{
		mark=i;
		for(j=i+1; j<LEN; j++)
		{
			if(a[mark]>a[j])
			{
				mark=j;
			}
		}
		if(mark!=i)
		{
			temp=a[mark];
			a[mark]=a[i];
			a[i]=temp;
		}
	}
}


//冒泡排序
void bubbleSort(int a[])
{
	int i,j;
	int temp;
	for(i=0; i<LEN-1; i++)
	{
		for(j=0; j<=LEN-i-1; j++ )
		{
			if(a[j]>a[j+1])
			{
				temp=a[j];
				a[j]=a[j+1];
				a[j+1]=temp;
			}
		}
	}
}

关于算法的具体介绍和改进,可参看: http://blog.csdn.net/nifenggege8888/article/details/13732409
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值