顺序表排序

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE_LENGTH_INSERT_SORT 7
#define MAXSIZE 10
#define ok 1
#define error 0 

typedef struct sq{
	int date[MAXSIZE+1];
	int lenth;
}sqlist;

void swap(sqlist *L,int i,int j)
{
	int temp=L->date[i];
	L->date[i]=L->date[j];
	L->date[j]=temp;
}

void insert(sqlist *l,int n)	//线性表的初始化 
{
	int i;
	int temp;
	l->lenth = 0;
	if(n>MAXSIZE)
	{
		printf("error no room");
		exit(0);
	} 
	for(i=1;i<=n;i++)
	{
		scanf("%d",&temp);
		l->date[i] = temp;
		l->lenth++;
	}
}
//冒泡排序初级版 (交换排序)
void BubbleSort0(sqlist *L)
{
	int i,j;
	for(i=1;i<L->lenth;i++)
	{
		
		for(j=i+1;j<=L->lenth;j++)
		{
			if(L->date[i]>L->date[j])
			{
				swap(L,i,j);
			}
		}
		
	}	
} 
//冒泡排序 
void BubbleSort1(sqlist *L)
{
	int i,j;
	for(i=1;i<L->lenth;i++)
	{
		for(j=L->lenth-1;j>=i;j--)			//j是从后往前循环 
		{
			if(L->date[j]>L->date[j+1])
				swap(L,j,j+1); 
		}	
	}
 } 

//冒泡排序优化
void BubbleSort2(sqlist *L)
{
	int i,j;
	int flag=1;						//初始化为1 
	for(i=1;i<L->lenth&&flag;i++)	
	{
		flag=0;								//循环内初始化为0 
		for(j=L->lenth-1;j>=i;j--)
		{
			swap(L,i,j);					//如果有数据进行交换说明排序未完成则把flag置为1继续循环 
			flag=1;							//无数据交换则,flag为0,即退出后续循环 
		}
	}	
} 
//简单选择排序 
void SelectSort(sqlist *L)
{
	int i,j,min;
	for(i=1;i<L->lenth;i++)
	{
		min = i;						//当前下标定为最下值下标 
		for(j=i+1;j<=L->lenth;j++) 
		{
			if(L->date[min]>L->date[j])
				min=j;
		}
		if(i!=min)					//i!=min说明找到最小值交换 
			swap(L,i,min);
	}		
}

//直接插入排序 
void InsertSort(sqlist *L)
{
	int i,j;
	for(i=2;i<=L->lenth;i++)
	{
		if(L->date[i]<L->date[i-1])
		{
			L->date[0]=L->date[i];
			for(j=i-1;L->date[j]>L->date[0];j--)
				L->date[j+1]=L->date[j];
			L->date[j+1]=L->date[0];
		}
	}
} 
//希尔排序 
void ShellSort(sqlist *L)
{
	int i,j;
	int increment=L->lenth;
	do
	{
		increment = increment/3+1;		//增量序列把increment=1,再把do-while去掉就是直接插入排序 
		for(i=increment+1;i<=L->lenth;i++)
		{
			if(L->date[i]<L->date[i-increment])
			{
				L->date[0]=L->date[i];
				for(j=i-increment;j>0&&L->date[0]<L->date[j];j-=increment)
					L->date[j+increment]=L->date[j];
				L->date[j+increment]=L->date[0];
				
			}
		}
	}
	while(increment>1);
} 
//堆排序 
void HeapAdjust(sqlist *L,int s,int m)
{
	int temp,j;
	temp=L->date[s];
	for(j=2*s;j<=m;j*=2)
	{
		if(j<m&&L->date[j]<L->date[j+1])
			++j;
		if(temp>=L->date[j])
			break;
		L->date[s]=L->date[j];
		s=j;
	}
	L->date[s]=temp;
}
void HeapSort(sqlist *L)
{
	int i;
	for(i=L->lenth/2;i>0;i--)
		HeapAdjust(L,i,L->lenth);
	for(i=L->lenth;i>1;i--)
	{
		swap(L,1,i);
		HeapAdjust(L,1,i-1);
	}
}

//----------------------------------------------------------------------------------//
//(递归算法)归并排序
void Merge(int SR[],int TR[],int i,int m,int n)
{
	int j,k,l;
	for(j=m+1,k=i;i<=m&&j<=n;k++)
	{
		if(SR[i]<SR[j])
			TR[k]=SR[i++];
		else
			TR[k]=SR[j++];
	}
	if(i<=m)
	{
		for(l=0;l<=m-1;l++)
			TR[k+l]=SR[i+l];
	}
	if(j<=n)
	{
		for(l=0;l<=n-j;l++)
			TR[k+l]=SR[j+l];
	}	
}
void MSort(int SR[],int TR1[],int s,int t)
{
	int m;
	int TR2[MAXSIZE];
	if(s==t)
		TR1[s]=SR[s];
	else
	{
		m=(s+t)/2;
		MSort(SR,TR2,s,m);
		MSort(SR,TR2,m+1,t);
		Merge(TR2,TR1,s,m,t);
	}
}
void MergeSort(sqlist *L)
{
	MSort(L->date,L->date,1,L->lenth);
}

//-----------------------------------------------------------------------------------//
//(非递归)归并排序
void MergePass(int SR[],int TR[],int s,int n)
{
	int i=1;
	int j;
	while(i <= n-2*s+1)
	{
		Merge(SR,TR,i,i+s-1,i+2*s-1);				//两两归并 
		i=i+2*s;
	}
	if(i<n-s+1)
		Merge(SR,TR,i,i+s-1,n);					//归并最后两个序列 
	else
		for(j=i;j<=n;j++)
			TR[j]=SR[j];						//归并单个子序列 
}
void MergeSort2(sqlist *L)
{
	int *TR=(int *)malloc(L->lenth*sizeof(int));
	int k=1;
	while(k<L->lenth)
	{
		MergePass(L->date,TR,k,L->lenth);			 
		k=2*k;										//子序列长度加倍 
		MergePass(TR,L->date,k,L->lenth);
		k=2*k;
	}
} 
//-------------------------------------------------------------------------------//
//快速排序
int Partition(sqlist *L,int low,int high)
{
	int pivotkey;
	//优化开始
	int m= low+(high-low)/2;
	if(L->date[low]>L->date[high])
		swap(L,low,high); 
	if(L->date[m]>L->date[high])
		swap(L,high,m); 
	if(L->date[m]>L->date[low])
		swap(L,m,low);
	//优化结束 
	pivotkey=L->date[low];
	while(low<high)
	{
		while(low<high&&L->date[high]>=pivotkey)
			high--;
		swap(L,low,high);
		while(low<high&&L->date[low]<=pivotkey)
			low++;
		swap(L,low,high);
	}
	return low;
}
int Partition1(sqlist *L,int low,int high)
{
	int pivotkey;
	//优化开始
	int m= low+(high-low)/2;
	if(L->date[low]>L->date[high])
		swap(L,low,high); 
	if(L->date[m]>L->date[high])
		swap(L,high,m); 
	if(L->date[m]>L->date[low])
		swap(L,m,low);
	//优化结束 
	pivotkey=L->date[low];
	L->date[0]=pivotkey;
	while(low<high)
	{
		while(low<high&&L->date[high]>=pivotkey)
			high--; 
		L->date[low]=L->date[high];
		while(low<high&&L->date[low]<=pivotkey)
			low++;
		L->date[high]=L->date[low];
	}
	L->date[low]=L->date[0];
	return low;
}


void QSort(sqlist *L,int low,int high)
{
	int pivot;
	if(low<high)
	{
		pivot=Partition(L,low,high);	//将l-date一分为二 
		QSort(L,low,pivot-1);			//算出pivot,再分别对低,高子表进行递归排序 
		QSort(L,pivot+1,high); 
	}
}

//优化小数组排序 
void QSort1(sqlist *L,int low,int high)
{
	int pivot;
	if((high-low)>MAXSIZE_LENGTH_INSERT_SORT)
	{
		pivot=Partition(L,low,high);	//将l-date一分为二 
		QSort1(L,low,pivot-1);			//算出pivot,再分别对低,高子表进行递归排序 
		QSort1(L,pivot+1,high); 
	}
	else
		InsertSort(L);
}

//尾递归优化
void QSort2(sqlist *L,int low ,int high)
{
	int pivot;
	if((high-low)>MAXSIZE_LENGTH_INSERT_SORT)
	{
		while(low<high)
		{
			pivot=Partition1(L,low,high);	//将l-date一分为二 
			QSort2(L,low,pivot-1);			//对低子表进行递归排序,
											//当qsort2递归返回时while对高子表递归 
			low=pivot+1; 					//尾递归  
		}	 
	}
	else
		InsertSort(L);
}
void QuickSort(sqlist *L)
{
	QSort2(L,1,L->lenth);
}

//-------------------------------------------------------------//
void print(sqlist l)
{
	int i;
	for(i=1;i<=l.lenth;i++)
		printf("%d ",l.date[i]);
	if(i==l.lenth)
	printf("\n");
} 

int main()
{
	sqlist *l;
	int n;
	int m;
	l=(sqlist*)malloc(sizeof(sqlist));
	l->lenth=0;				//初始化输入的数据个数,少于max_siax个 
	printf("请输入链表元素个数(不大于10个)");
	scanf("%d",&n);
	printf("请输入链表元素\n");
	insert(l,n);

//	BubbleSort0(l);
//	BubbleSort1(l);
//	BubbleSort2(l);
//	SelectSort(l);
//	InsertSort(l);
//	ShellSort(l);
//	MergeSort(l);	
//	MergeSort2(l);	
	QuickSort(l);
	print(*l);
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值