几种常见的排序-数据结构课程设计 第2页

 

void MergeSort(int a[], int b[])//归并排序
{
int i = 1, j = 1, k = 0;
int n, c[30];

Bubble(a);
Bubble(b);

while (i < M && j < M)//这里循环语句要写清楚 一个if 对应一个else 否则编译器会搞晕 会出错
{
if (a[i] < b[j])
{
c[k++]  = a[i++];
}
else if (a[i] > b[j])
{
c[k++] = b[j++];
}
else
{
c[k++] = a[i++];
}
}
while (i < M)
{
c[k++] = a[i++];
}
while (j < M)
{
c[k++] = b[j++];
}

n = k;

printf("进行归并排序:");
for ( i = 0; i < n; i++)
{
printf("%4d",c[i]);
}
printf("\n");
}

int main(void)
{
int a[M] = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int b[M] = {0, 3, 11, 34, 13, 10, 19, 15, 14, 16, 18};
int select, i;

printf("排序之前的元素为");
for ( i = 1; i < M; i++)
{
printf("%4d",a[i]);
}
printf("\n");
printf("1: 进行插入排序\n");
printf("2: 进行折半排序\n");
printf("3: 进行冒泡排序\n");
printf("4: 进行堆排序\n");
printf("5: 进行希尔排序\n");
printf("6: 进行归并排序\n");
printf("请选择:");
scanf("%d",&select);

switch(select)
{
case 1:
InsertSort(a);
break;
case 2:
BinarySort(a);
break;
case 3:
Bubble(a);
break;
case 4:
HeapSort(a);
break;
case 5:
ShellSort(a);
break;
case 6:
MergeSort(a, b);
break;
default:
break;
}

system("pause");
return 0;
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=965288

上一页  [1] [2] 

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有