C语言编程题 题目描述 使用冒泡排序法对数组元素从小到大进行排序,要求输出每一趟排序后的数组内容(

C语言编程题
题目描述
使用冒泡排序法对数组元素从小到大进行排序,要求输出每一趟排序后的数组内容(每一趟的结果把最小元素放在当前数组的首位)。数组大小N<10,数组元素定为正整型。
输入
依次输入数组各个元素,各元素之间用空格隔开。
输出
用一行输出一趟排序后的结果。
样例输入
4 9 2 8
样例输出
2,4,9,8
2,4,8,9
2,4,8,9

第1个回答  推荐于2017-11-25
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

void sort(int arry[],int counts)//冒泡排序法
{
for(int i=0;i<counts;i++)
{
for(int j=0;j<counts-i-1;j++)
{
if(arry[j]>arry[j+1])//比较大小
{
int temp;
temp=arry[j];
arry[j]=arry[j+1];
arry[j+1]=temp;
}
}
for (int k=0;k<counts;k++)//输出
{
cout<<arry[k]<<" ";
}
cout<<'\n';
}
}

int main()
{
int arry[10];
char c;
int counts=0;
while((c=getchar())!='\n')//获取一行输入
{
if(c>='0'&&c<='9')
{
ungetc(c,stdin);//将获取的字符返回流
cin>>arry[counts++];
}
}
sort(arry,counts);
system("pause");
return 0;
}本回答被网友采纳
第2个回答  2016-04-23
假设数组有10个数
#include <stdio.h>
int main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
int i,j,t;
for(i=1;i<10;i++){
for(j=0;j<10-i;j++)
{
if (a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(int k=0;k<10;k++){
printf("%4d\n",a[k]);
}

}
}
}本回答被网友采纳
相似回答