Java BubbleSort

Java BubbleSort,java,bubble-sort,Java,Bubble Sort,我面临一个问题,需要按字母顺序对字符串数组进行排序。我可以对一个数组进行排序,但当有两个数组与第一个数组相对应时,问题就开始了。每个数组中的每个值都应该位于相同的位置,以使信息不会混乱。排序array1后,它是按字母顺序排列的,但我不知道如何在排序完成后使array2和array3中的值与array1中的位置相同 到目前为止,我的代码是: public void sort() { boolean finish = false; while(finish == false)

我面临一个问题,需要按字母顺序对字符串数组进行排序。我可以对一个数组进行排序,但当有两个数组与第一个数组相对应时,问题就开始了。每个数组中的每个值都应该位于相同的位置,以使信息不会混乱。排序
array1
后,它是按字母顺序排列的,但我不知道如何在排序完成后使
array2
array3
中的值与
array1
中的位置相同

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

public  void sort() 
{

    boolean finish = false;

    while(finish == false){

        finish = true;

        for(int i=0;i<Country.length-1;i++)

        {
            int num = 0;
            if(Country[i] != null && Country[i + 1] != null)
            {
                String name1=Country[i]; String name2=Country[i+1];
                num=name1.compareTo(name2);
            }
            else if(Country[i] == null && Country[i + 1] == null){
                num = 0;
            }
            else if(Country[i] == null){
                num = 1;
            }
            else {
                num = -1;
            }
            if(num>0)
            {
                String temp=Country[i];

                Country[i]=Country[i+1];
                Country[i+1]=temp;
                finish=false;
            }
        }
    }
public void sort()
{
布尔完成=假;
while(finish==false){
完成=真;
对于(int i=0;i0)
{
字符串温度=国家[i];
国家[i]=国家[i+1];
国家[i+1]=临时工;
完成=错误;
}
}
}

如果希望根据在国家/地区阵列中进行的比较来交换所有阵列。您可以在一次比较后交换多个数组

If(array1[i] > array1[i+1]){
    Swap(array1[i],array1[i+1)
    Swap(array2[i],array2[i+1])
}

通过使用交换函数,可以使在更多数组中进行交换更加简单

您必须同时交换
国家
城市
数组中的元素

public class BubbleSortTmp {
    public String[] Country = {"z", "h", "a"};
    public int[] City = {3, 2, 1};

    public void printCountry() {
        for (String s : Country) {
            System.out.printf("%s ", s);
        }
        System.out.println();
    }

    public void printCity() {
        for (int s : City) {
            System.out.printf("%s ", s);
        }
        System.out.println();
    }

    public void sort() {
        for (int outer = Country.length - 1; outer > 0; outer--) {
            for (int inner = 0; inner < outer; inner++) {
                if (Country[inner].compareTo(Country[inner+1]) > 0) {
                    swapCountry(inner, inner+1);
                    swapCity(inner, inner+1);
                }
            }
        }
    }

    private void swapCountry(int first, int second) {
        String tmp = Country[first];
        Country[first] = Country[second];
        Country[second] = tmp;
    }

    private void swapCity(int first, int second) {
        int tmp = City[first];
        City[first] = City[second];
        City[second] = tmp;
    }

    public static void main(String[] args) {
        BubbleSortTmp bs = new BubbleSortTmp();

        System.out.println("Before: ");
        bs.printCountry();
        bs.printCity();

        bs.sort();

        System.out.println("After: ");
        bs.printCountry();
        bs.printCity();
    }
}
公共类BubbleSortTmp{
公共字符串[]国家={“z”、“h”、“a”};
公共int[]城市={3,2,1};
公共资源(国家){
for(字符串s:国家){
System.out.printf(“%s”,s);
}
System.out.println();
}
公共城市(){
for(int s:城市){
System.out.printf(“%s”,s);
}
System.out.println();
}
公共无效排序(){
对于(int-outer=Country.length-1;outer>0;outer--){
for(int-inner=0;inner0相比{
swapCountry(内部,内部+1);
swapCity(内部,内部+1);
}
}
}
}
私有无效交换国家(整数第一,整数第二){
字符串tmp=国家[第一];
国家[第一]=国家[第二];
国家[第二]=tmp;
}
私有无效交换(整数第一,整数第二){
int tmp=城市[第一];
城市[第一]=城市[第二];
城市[第二]=tmp;
}
公共静态void main(字符串[]args){
BubbleSortTmp bs=新的BubbleSortTmp();
System.out.println(“之前:”);
bs.printCountry();
bs.printCity();
b.sort();
System.out.println(“之后:”);
bs.printCountry();
bs.printCity();
}
}

到目前为止,最推荐的方法是重新设计程序,并将所有相关项目安排在一个类中。毕竟,这就是对象的用途。然后您可以使对象
可比较
,给它一个
compareTo
方法,并对其排序

但是,如果您确实无法做到这一点,那么您应该做的是,每当您交换排序数组中的任意两个项时,请确保交换其他数组中的相应项

因此,如果您有数组
country
capital
headOfState
,则必须编写如下内容:

  String temp=country[i];

  country[i]=country[i+1];
  country[i+1]=temp;

  temp=capital[i];
  capital[i]=capital[i+1];
  capital[i+1]=temp;

  temp=headOfState[i];
  headOfState[i]=headOfState[i+1];
  headOfState[i+1]=temp;
这样,无论何时移动主数组中的任何内容,都会在其他数组中移动相应的项,以便它们保持在一起

但是,如果你重新设计你的程序,它会更受欢迎


还要注意Java语言惯例-变量名不应以大写字母开头,只应以类型名开头。

例如,国家数组值为{z,a};城市值是{2,1}在对国家数组进行排序后,我希望城市数组在国家数组中交换值的位置与在国家数组中交换值的位置相同。是否允许自定义可比较的对象类来保存所有数据?这样,你只需要对一个数组而不是多个数组进行排序。对不起,我的错误是我开始创建基于3个数组的GUI,现在如果我想更改它,我还需要更改整个程序和设计。这就是为什么我试图得到一些提示,至少该做些什么,以节省我的时间?有一些内置的Java排序方法执行得更好。不管怎样,你的数据是什么?你有一个
城市
,一个
国家
,还有其他代表一个地方的东西?因此,您创建了一个
Place[]
(一个自定义类),并根据其
Country
字符串值对所有
Place
对象进行排序。这样,所有数据都是“以正确的顺序”的。手动实现冒泡排序不是强制性的。我只是想了解它到底是如何工作的,这就是为什么我想在第一次使用它时手动实现它。我的数组是城市、国家和人口。当所有的县都按字母顺序排列时,我希望城市和人口保持在同一个地方,所以在文本区显示后,一排将是英国、伦敦和任何人口。目前,在按照重新设计建议对Country ArrayGree进行排序后,城市和人口数量都变得一团糟。OP真的应该创建一个包含相关数据的新对象。非常感谢!这对我有用。我同意你的观点,我会在完成这个版本后重新设计它,只是为了能够比较每种方式的优缺点。无论如何,谢谢你!!