C语言:冒泡排序算法实现将一个长度为N的数组ary所有整形元素按升序排列。试完成相关函数。
冒泡排序算法实现将一个长度为N的数组ary所有整形元素按升序排列。试完成相关函数。#include<stdio.h>#defineN10voidshow(intr[],i...
冒泡排序算法实现将一个长度为N的数组ary所有整形元素按升序排列。试完成相关函数。
#include<stdio.h>
#define N 10
void show(int r[] , int n)
{
/*elements are stored in r[1] to r[n]*/
}
void bubblesort(int r[],int n)
{ /*elements are stored in r[1] to r[n]*/
} /*bubblesort*/
void main()
{
int a[N],i;
printf("请输入 %d 个整数:\n",N);
for(i=0;i<N+1;i++)
scanf("%d",&a[i]);
printf("数组A的%d个元素是:\n",N);
show(a,N);
printf("冒泡法排序每趟的情形是:\n");
bubblesort(a,N);
printf("\nafter sorted by bubblesort:\n");
show(a,N);
} 展开
#include<stdio.h>
#define N 10
void show(int r[] , int n)
{
/*elements are stored in r[1] to r[n]*/
}
void bubblesort(int r[],int n)
{ /*elements are stored in r[1] to r[n]*/
} /*bubblesort*/
void main()
{
int a[N],i;
printf("请输入 %d 个整数:\n",N);
for(i=0;i<N+1;i++)
scanf("%d",&a[i]);
printf("数组A的%d个元素是:\n",N);
show(a,N);
printf("冒泡法排序每趟的情形是:\n");
bubblesort(a,N);
printf("\nafter sorted by bubblesort:\n");
show(a,N);
} 展开
2013-11-28
展开全部
数组中的元素应该是从0~n-1的
否则你开始定义的a[n]空间不够,会发生错误
#include<stdio.h>
#define N 10
void show(int r[] , int n)
/*elements are stored in r[0] to r[n-1]*/
{
int i = 0;
printf("\n");
for(i = 0; i < n; i++)
printf("%5d",r[i]);
printf("\n");
return;
}
void bubblesort(int r[],int n)
/*elements are stored in r[0] to r[n-1]*/
{
int i = 0, j = 0;
int temp = 0;
for(i = 0; i < n; i++)
{
for(j = i; j < n; j++)
{
if(r[i] > r[j])
{
temp = r[j];
r[j] = r[i];
r[i] = temp;
}
}
}
return;
} /*bubblesort*/
void main()
{
int a[N] = {0},i;
printf("请输入 %d 个整数:\n",N);
for(i=0;i<N;i++)
scanf("%d",&a[i]);
printf("数组A的%d个元素是:\n",N);
show(a,N);
printf("冒泡法排序每趟的情形是:\n");
bubblesort(a,N);
printf("\nafter sorted by bubblesort:\n");
show(a,N);
}
运行过程:
否则你开始定义的a[n]空间不够,会发生错误
#include<stdio.h>
#define N 10
void show(int r[] , int n)
/*elements are stored in r[0] to r[n-1]*/
{
int i = 0;
printf("\n");
for(i = 0; i < n; i++)
printf("%5d",r[i]);
printf("\n");
return;
}
void bubblesort(int r[],int n)
/*elements are stored in r[0] to r[n-1]*/
{
int i = 0, j = 0;
int temp = 0;
for(i = 0; i < n; i++)
{
for(j = i; j < n; j++)
{
if(r[i] > r[j])
{
temp = r[j];
r[j] = r[i];
r[i] = temp;
}
}
}
return;
} /*bubblesort*/
void main()
{
int a[N] = {0},i;
printf("请输入 %d 个整数:\n",N);
for(i=0;i<N;i++)
scanf("%d",&a[i]);
printf("数组A的%d个元素是:\n",N);
show(a,N);
printf("冒泡法排序每趟的情形是:\n");
bubblesort(a,N);
printf("\nafter sorted by bubblesort:\n");
show(a,N);
}
运行过程:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询