题目描述
已知直接插入排序的部分代码如下,勿改动,请补充实现插入排序函数。要求输出每趟排序结果。
#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 InsertSort(); //InsertSort
};
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.InsertSort();
//L.Display();
return 0;
}
输入
输出
样例输入
样例输出
12 21 32 2 4 24 21 432 23 9
12 21 32 2 4 24 21 432 23 9
2 12 21 32 4 24 21 432 23 9
2 4 12 21 32 24 21 432 23 9
2 4 12 21 24 32 21 432 23 9
2 4 12 21 21 24 32 432 23 9
2 4 12 21 21 24 32 432 23 9
2 4 12 21 21 23 24 32 432 9
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 InsertSort(){int j;for (int i = 2; i <= n; i++) {r[0] = r[i];for (j = i - 1; r[0] < r[j]; j--) {r[j + 1] = r[j];}r[j + 1] = r[0];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.InsertSort();}}
