编制一维数组排序程序。数组大小n用全局常变量定义,数组数据从文本文件中读入或随机生成。包含冒泡排序、选择排序、插入排序三种排序方法。程序能够选择使用任何一种方法排序。谢谢!
急需答案,恳请会的帮忙!不胜感激!!
C++题:编制数组排序,用冒泡排序、选择排序、插入排序三种方法。急需答案!!
答案:1 悬赏:20
解决时间 2021-11-06 13:14
- 提问者网友:梧桐不渝
- 2021-11-06 03:29
最佳答案
- 二级知识专家网友:强势废物
- 2021-11-06 03:39
分太少了,还是给你写一个吧
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 10
void BubbleSort(int a[])
{
int i,j,t;
for (i=0;i<N;i++)
{
for (j=i+1;j<N;j++)
{
if(a[i]>a[j])
{
t=a[i];a[i]=a[j];a[j]=t;
}
}
}
}
void SelectSort(int a[])
{
int i,j,t,min_pos;
for (i=0;i<N;i++)
{
min_pos=i;
for (j=i+1;j<N;j++)
{
if (a[min_pos]>a[j])
min_pos=j;
}
if (i!=min_pos)
{
t=a[i];a[i]=a[min_pos];a[min_pos]=t;
}
}
}
void InsertSort(int a[])
{
int i,j;
int temp;
for(i=1;i<N;i++)
{
temp = a[i];
for(j=i;j>0 && temp<a[j-1] ;j--)
{
a[j]=a[j-1];
}
a[j]=temp;
}
}
void main()
{
int i,imethod,a[N];
while(1)
{
srand(time(0));
printf("Random Datas Are:\n");
for (i=0;i<N;i++)
{
a[i] = rand();
printf("%d ",a[i]);
}
printf("\nPlease Select Sort Method:\n1:Bubble Sort\n2:Select Sort\n3.Insert Sort\n0:Exit:");
scanf("%d",&imethod);
switch(imethod)
{
case 1:
BubbleSort(a);
break;
case 2:
SelectSort(a);
break;
case 3:
InsertSort(a);
break;
case 0:
printf("Thanks for using,bye!\n");
return;
default:
printf("Input Error!Please Input (0-3)\n");
break;
}
printf("After Sort:\n");
for (i=0;i<N;i++)
{
printf("%d ",a[i]);
}
printf("\n\n");
}
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 10
void BubbleSort(int a[])
{
int i,j,t;
for (i=0;i<N;i++)
{
for (j=i+1;j<N;j++)
{
if(a[i]>a[j])
{
t=a[i];a[i]=a[j];a[j]=t;
}
}
}
}
void SelectSort(int a[])
{
int i,j,t,min_pos;
for (i=0;i<N;i++)
{
min_pos=i;
for (j=i+1;j<N;j++)
{
if (a[min_pos]>a[j])
min_pos=j;
}
if (i!=min_pos)
{
t=a[i];a[i]=a[min_pos];a[min_pos]=t;
}
}
}
void InsertSort(int a[])
{
int i,j;
int temp;
for(i=1;i<N;i++)
{
temp = a[i];
for(j=i;j>0 && temp<a[j-1] ;j--)
{
a[j]=a[j-1];
}
a[j]=temp;
}
}
void main()
{
int i,imethod,a[N];
while(1)
{
srand(time(0));
printf("Random Datas Are:\n");
for (i=0;i<N;i++)
{
a[i] = rand();
printf("%d ",a[i]);
}
printf("\nPlease Select Sort Method:\n1:Bubble Sort\n2:Select Sort\n3.Insert Sort\n0:Exit:");
scanf("%d",&imethod);
switch(imethod)
{
case 1:
BubbleSort(a);
break;
case 2:
SelectSort(a);
break;
case 3:
InsertSort(a);
break;
case 0:
printf("Thanks for using,bye!\n");
return;
default:
printf("Input Error!Please Input (0-3)\n");
break;
}
printf("After Sort:\n");
for (i=0;i<N;i++)
{
printf("%d ",a[i]);
}
printf("\n\n");
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯