题目描述

已知希尔排序的部分代码如下,勿改动,请补充实现希尔排序函数,要求输出每趟排序结果,增量序列取值依次为5,3,1。
#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 ShellSort(); //ShellSort
};
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.ShellSort();
// L.Display();
return 0;
}

输入

输出

样例输入

12 21 32 2 4 24 21 432 23 9 0

样例输出

12 21 32 2 4 24 21 432 23 9
2 4 23 9 21 24 12 432 32 21
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 ShellSort(){
  21. int j;
  22. int increment = 5;
  23. for (int d = increment; d >= 1; d -= 2) {
  24. for (int i = d + 1; i <= n; i++) {
  25. r[0] = r[i];
  26. for (j = i - d; j > 0 && r[0] < r[j]; j -= d) {
  27. r[j + d] = r[j];
  28. }
  29. r[d + j] = r[0];
  30. }
  31. Display();
  32. }
  33. }
  34. }
  35. public class Main {
  36. public static void main(String[] args) {
  37. Ilist ilist = new Ilist();
  38. Scanner scanner = new Scanner(System.in);
  39. while(true){
  40. int x = scanner.nextInt();
  41. if(x == 0)break;
  42. ilist.InsertR(x);
  43. }
  44. ilist.ShellSort();
  45. }
  46. }