java输出数组交换后的元素_java - Java-如何交换2个数组元素并使用toString输出shell-sort的每个交换? - SO中文参考 - www.soinside.com...

我还是Java的新手,这是一个学校的任务,我要创建一个ShellSort类和驱动程序类。 ShellSort类应该创建并填充n个大小的数组,并使用以下伪代码包含shell排序方法:

eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS94c2VQTi5wbmcifQ==

这是我到目前为止的代码:

import java.util.*;

public class ShellArray {

int array[] = null;

int size=0;

Random random = new Random();

int gap = 0;

public ShellArray (int size) {

this.size = size;

array=new int[size];

for (int i = 0; i

this.array[i]=random.nextInt(100);

}

System.out.println("Your array is: ");

for(int i=0; i

System.out.print(" " +array[i] + " ");

}

}

public int[] shellSort(int size) {

gap = size/2;

do {

boolean swapflag = true;

do {

swapflag = false;

for (int s = 0; s

if (array[s] > array[s+gap]) {

//swap array[s] with array[s+gap]

swapflag = true;

}

}

}

while (swapflag != false);

}while (gap>0);

gap = gap/2;

}

public String toString () {

//to output array results of each swap

}

}

我迷失了如何用数组[s + gap]交换数组[s]以及如何在每次交换时使用toString输出数组。

我已经尝试使用“temp”变量来放入数组[s],所以我可以将数组[s + gap]放入数组[s]的索引中,但是我收到了“ShellArray @ 7c53a9eb”错误?

任何帮助都感激不尽!谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值