八大排序算法,1:冒泡排序 2:插入排序 3:希尔排序 4:快速排序 5:堆排序 6:归并排序 7:基数排序 8:选择排序

我这只有干货

#include
#include
#include
#define OK 1
typedef int Status;

struct str{
int data;
struct str *next;
}List[10];

Status *ChaRu(int ss[],int len){//插入排序

int i,j,k;

int t = 0;

for(i=1;i<=len;i++)
{
	j = 1;
	ss[0] = ss[i];
	t++;
	while(j=t)	k = t;
	int m = t;
	while(m>k)
	{
		ss[m] = ss[m-1];
		m--;
	}
	ss[k] = ss[0];
}

return ss;

}
Status XiEr(int ss1[],int len){//希尔排序
int ss[len+1];
for(int a=0;a<=len;a++)
ss[a] = ss1[a];

int d[3] = {5,2,1};
int i,j;
for(i=0;i<3;i++)//3次不同的增量 
{

	for(int i1=1;i1<=d[i];i1++)
	{
		j = 1;
		int len1 = i1;
		int dp[len/d[i]+1];//存放待排序数;
		dp[0] = 0;
		while(len1<=len)	{dp[j++] = ss[len1];len1+=d[i];};
	 	int *b = ChaRu(dp,j-1);
	 	len1 = i1;	j = 1;
		while(len1<=len)	{ss[len1] = b[j++];len1+=d[i];};
		
	}
	printf("\n  希尔排序:"); 
for(int i=1;i<=len;i++)
printf("%4d",ss[i]);	
}
printf("\n  希尔排序:"); 
for(int i=1;i<=len;i++)
printf("%4d",ss[i]);
return OK;

}

Status KuaiSu(int ss1[],int len){//快速排序
int ss[len+1];
for(int a=0;a<=len;a++)
ss[a] = ss1[a];

int low,high,pd;
while(1)
{
	ss[0] = ss[1];
	low = 1;high = len,pd = 1;
	while(high>=low)
	{
		while(ss[high]>=ss[0])	
		{
			high--;
			if(high<=1)	break;
		}
		ss[pd] = ss[high];
		pd = high;
		high--;
		while(ss[low]<=ss[0]&&high>=low)	low++;
		ss[pd] = ss[low];
		pd = low;
		low++;
	}
	ss[pd] = ss[0];
	if(pd == 1)	break;
		printf("\n  快速排序:"); 
for(int i=1;i<=len;i++)
printf("%4d",ss[i]);
}//此时已经基本有序 
int *b = ChaRu(ss,len);

printf("\n  快速排序:"); 
for(int i=1;i<=len;i++)
printf("%4d",b[i]);
return OK;

}

Status DuiTest(int *heap, int len)
{
int i,low,high;
int t;

for (i = len/2-1; i >= 0; i--)
{
	low = 2*i+1;	
	high = 2*low+1;
    if (low < len && heap[i] < heap[low]) 
    {
        t = heap[i];
        heap[i] = heap[low];
        heap[low] = t;
        if ((high < len && heap[low] < heap[high]) || (high+1 < len && heap[low] < heap[high+1]))
        {
            DuiTest(heap, len);
        }
    }
    low = 2*i+2;	
	high = 2*low+1;
    if ((2*i+2) < len && heap[i] < heap[low]) 
    {
        t = heap[i];
        heap[i] = heap[low];
        heap[low] = t;

        if ((high < len && heap[low] < heap[high]) || (high+1 < len && heap[low] < heap[high+1]))
        {
            DuiTest(heap, len);
        }
    }
    

}
return OK;

}

Status Dui(int ss1[],int len){//堆排序
int ss[len];
for(int a=0;a ss[a] = ss1[a+1];
int i,j,ki;
int low,high;
i = len;
while(i>0)
{
DuiTest(ss, i);
printf("\n 堆排序:");
for(int i1=0;i1 printf("%4d",ss[i1]);
int t = ss[0];
ss[0] = ss[i-1];
ss[i-1] = t;

 	i--;
 }
printf("\n    堆排序:"); 
for(int i=0;i

}

void mergeArray(int ss[], int first, int mid, int last, int temp[]) {
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;
while (i <= m && j <= n) {
if (ss[i] <= ss[j])
temp[k++] = ss[i++];
else
temp[k++] = ss[j++];
}
while (i <= m) {
temp[k++] = ss[i++];
}
while (j <= n) {
temp[k++] = ss[j++];
}
for (i = 0; i < k; i++) {
ss[first + i] = temp[i];
// printf("%4d",temp[i]);
}
}

void mergeSort(int ss[], int first, int last, int temp[]) {
if (first < last) {
int mid = (first + last) / 2;
mergeSort(ss, first, mid, temp);
mergeSort(ss, mid + 1, last, temp);
mergeArray(ss, first, mid, last, temp);
printf("\n 归并排序:");
for(int i=0;i<=last;i++)
printf("%4d",ss[i]);
}
}

Status GuiBing(int ss1[],int len){//归并排序
int ss[len];
for(int a=0;a ss[a] = ss1[a+1];
int temp[len];
mergeSort(ss, 0, len-1, temp);
printf("\n 归并排序:");
for(int i=0;i printf("%4d",ss[i]);
return OK;
}

Status JiShu(int ss1[],int len){//基数排序
int ss[len];

int k;

for(int a=0;a0;i--)
{
	int Q[10][len+1];
	for(int i1=0;i1<10;i1++)
	Q[i1][0] = 1;
	int t;
	for(int j=0;j

}
Status MaoPao(int ss1[],int len){//冒泡排序
int ss[len];
for(int a=0;a ss[a] = ss1[a+1];

int i,j,k;
for(i=0;iss[j+1])
		{
			k = ss[j];
			ss[j] = ss[j+1];
			ss[j+1] = k; 	
		}
	}
printf("\n  冒泡排序:"); 
for(int i=0;i

}

Status XuanZe(int ss1[],int len){//选择排序
int ss[len+1];
for(int a=0;a<=len;a++)
ss[a] = ss1[a];
for(int i=1;i {
ss[0] = 1;
for(int j = 2;j<=len+1-i;j++)
{
if(ss[ss[0]] {
ss[0] = j;
}
}
int t = ss[len+1-i];
ss[len+1-i] = ss[ss[0]];
ss[ss[0]] = t;
printf("\n 选择排序:");
for(int i=1;i<=len;i++)
printf("%4d",ss[i]);
}
printf("\n 选择排序:");
for(int i=1;i<=len;i++)
printf("%4d",ss[i]);

return OK;

}

int main()
{
int ss[12] = {0,503,17,512,61,908,170,897,275,653,426,154};
int ss1[12] = {0,503,17,512,61,908,170,897,275,653,426,154};
int len = 11;
int xz = 1;
printf(“待排序数组:”);
for(int i=1;i<=len;i++)
printf("%4d",ss[i]);
while(xz!=0)
{
printf("\n选择排序方式:");
printf("\n1:冒泡排序 2:插入排序 3:希尔排序 4:快速排序 5:堆排序 6:归并排序 7:基数排序 8:选择排序\n");
scanf("%d",&xz);
switch(xz)
{
case 1: MaoPao(ss,len);//冒泡排序
break;
case 2: ChaRu(ss1,len);
printf("\n 插入排序:");
for(int i=1;i<=len;i++)
printf("%4d",ss1[i]);//插入排序
break;
case 3: XiEr(ss,len);//希尔排序
break;
case 4: KuaiSu(ss,len);//快速排序
break;
case 5: Dui(ss,len);//堆排序
break;
case 6: GuiBing(ss,len);//归并排序
break;
case 7: JiShu(ss,len);//基数排序
break;
case 8: XuanZe(ss,len);//选择排序
break;
default:xz = 0;
break;
}
}

return 0;

}

你可能感兴趣的:(八大排序算法,1:冒泡排序 2:插入排序 3:希尔排序 4:快速排序 5:堆排序 6:归并排序 7:基数排序 8:选择排序)