题目描述

已知简单选择排序的部分代码如下,勿改动,请补充实现简单选择排序函数。要求:输出每趟排序序列。
#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 SelectSort(); //SelectSort
};
void List::Display()
{
for(int i=1;i<=n;i++)
cout< cout<<”\n”;
}
int main()
{
List L;
while(1)
{
int k;
cin>>k;
if(!k) break;
L.InsertR(k);
}
//L.Display();
L.SelectSort();
// L.Display();
return 0;
}

输入

输出

样例输入

12 21 32 2 4 24 21 432 23 9 0

样例输出

2 21 32 12 4 24 21 432 23 9
2 4 32 12 21 24 21 432 23 9
2 4 9 12 21 24 21 432 23 32
2 4 9 12 21 24 21 432 23 32
2 4 9 12 21 24 21 432 23 32
2 4 9 12 21 21 24 432 23 32
2 4 9 12 21 21 23 432 24 32
2 4 9 12 21 21 23 24 432 32
2 4 9 12 21 21 23 24 32 432

提示

来源

提交

  1. import java.util.Scanner;
  2. class Ilist{
  3. int[] r;
  4. int n;
  5. int MaxSize = 100;
  6. public Ilist() {
  7. n = 0;
  8. r = new int[MaxSize];
  9. }
  10. void InsertR(int k){
  11. r[++n] = k;
  12. }
  13. void Display(){
  14. // System.out.print("Data:");
  15. for (int i = 1; i <= n; i++) {
  16. System.out.print(r[i]+" ");
  17. }
  18. System.out.println();
  19. }
  20. void SelectSort(){
  21. for (int i = 1; i < n; i++) {
  22. int index = i;
  23. for (int j = i + 1; j <= n; j++) {
  24. if (r[index] > r[j])
  25. index = j;
  26. }
  27. if (index != i){
  28. int temp = r[i];
  29. r[i] = r[index];
  30. r[index] = temp;
  31. }
  32. Display();
  33. }
  34. }
  35. }
  36. public class Main {
  37. public static void main(String[] args) {
  38. Ilist ilist = new Ilist();
  39. Scanner scanner = new Scanner(System.in);
  40. int key;
  41. while(true){
  42. int x = scanner.nextInt();
  43. if(x == 0)break;
  44. ilist.InsertR(x);
  45. }
  46. ilist.SelectSort();
  47. }
  48. }