题目描述
已知起泡排序的部分代码如下,勿改动,请补充实现起泡排序函数,要求输出每趟排序结果。
提示:起泡排序时,当某趟排序时,一次交换也未发生,则该趟排序后,即结束。
#include
using namespace std;
const int MaxSize=100;
class List
{
private:
int r[MaxSize+1];
int n;
public:
List(){n=0;} //empty list
void InsertR(int k) //表尾插入
{ r[++n]=k;}
void Display(); //display
void BubbleSort(); //BubbleSort
};
void List::Display()
{
for(int i=1;i<=n;i++)
cout<
}
int main()
{
List L;
while(1)
{
int k;
cin>>k;
if(!k) break;
L.InsertR(k);
}
//L.Display();
L.BubbleSort();
//L.Display();
return 0;
}
输入
输出
样例输入
样例输出
12 21 2 4 24 21 32 23 9 432
12 2 4 21 21 24 23 9 32 432
2 4 12 21 21 23 9 24 32 432
2 4 12 21 21 9 23 24 32 432
2 4 12 21 9 21 23 24 32 432
2 4 12 9 21 21 23 24 32 432
2 4 9 12 21 21 23 24 32 432
2 4 9 12 21 21 23 24 32 432
提示
来源
提交
import java.util.Scanner;class Ilist{int[] r;int n;int MaxSize = 100;public Ilist() {n = 0;r = new int[MaxSize];}void InsertR(int k){r[++n] = k;}void Display(){// System.out.print("Data:");for (int i = 1; i <= n; i++) {System.out.print(r[i]+" ");}System.out.println();}void BubbleSort(){int exchange = n;while (exchange!=0) {int bound = exchange;exchange = 0;for (int i = 1; i < bound; i++) {if (r[i] > r[i + 1]) {int temp = r[i + 1];r[i + 1] = r[i];r[i] = temp;exchange = i;}}Display();}}}public class Main {public static void main(String[] args) {Ilist ilist = new Ilist();Scanner scanner = new Scanner(System.in);while(true){int x = scanner.nextInt();if(x == 0)break;ilist.InsertR(x);}ilist.BubbleSort();}}
