gpt4 book ai didi

java - 使用数组进行冒泡排序 alg

转载 作者:行者123 更新时间:2023-12-01 06:18:11 25 4
gpt4 key购买 nike

好的,今天我已经为此工作了一段时间,我知道它已经非常接近完成了。我一直在试图找到一个逃脱条款,以便我最终能够返回列表。这是我编写的所有代码。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package algorithm;

import java.util.Arrays;
import java.util.Collections;

/**
*
*
*/
public class Algorithm {

/**
* @param args the command line arguments
*/
private static int list[] = {10, 9, 8, 7, 6};

public static void main(String[] args) {
Algorithm alg = new Algorithm();
alg.bubblesort(list);
}

public Algorithm() {
}

public int[] bubblesort(int[] i) {
for (int a = 0; a < i.length;) {
for (int b = 1; b < i.length + 1;) {
int currentNumber = i[a];
if (b < i.length) {
if (currentNumber > i[b]) {
i[a] = i[b];
i[b] = currentNumber;
a++;
b++;
} else if (currentNumber < i[b]) {
a++;
b++;
}
} else if (b == i.length) {
a = 0;
b = 1;
}
}
}
return i;
}

public void isSorted(int[] i) {
for (int x = 0; x < i.length;) {
if (i[x + 1] < i[i.length - 1]) {
if (i[x] < i[x + 1]) {
x++;
}
}
}

System.out.println(Arrays.toString(i));
}
}

好吧,我的问题是如何返回最终的排序列表?

最佳答案

首先,您应该决定是 (1) 直接更改 list 还是 (2) 不更改。

alg.bubblesort(list);

我认为这句话的意思是(1),因为你没有捕获结果。如果你想做(2),它应该是

int[] result = alg.bubblesort(list);

并且,如果 (1) 更好,请修复定义冒泡排序

public void bubblesort (int[] args)

此外,您不应该使用int[] i,因为变量i通常表示数组的索引。

关于java - 使用数组进行冒泡排序 alg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18642841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com