简体   繁体   English

Java BubbleSort

[英]Java BubbleSort

I am facing a problem where I need to sort a String array in alphabetical order. 我遇到一个需要按字母顺序对String数组排序的问题。 I am able to sort one array, but the problem starts when there are 2 more arrays, that correspond to the first array. 我能够对一个数组进行排序,但是当还有两个数组对应于第一个数组时,问题就开始了。 Each value in each array should be in the same place, to make information not messed up. 每个数组中的每个值都应位于同一位置,以使信息不会混乱。 After sorting array1 , it is in alphabetical order, but i don't have any idea how to make values from array2 and array3 change the positions the same like in array1 after sorting is finished. 在对array1进行排序之后,它是按字母顺序排列的,但是我不知道如何在排序完成后如何使array2array3值像在array1一样改变位置。

My code so far is: 到目前为止,我的代码是:

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;
            }
        }
    }

If you want all the array to be swaped based on the compare you did in the country array. 如果要基于对country数组所做的比较交换所有数组。 You can just swap more than one array after one compare. 一次比较后,您可以交换多个数组。

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

By using a swap function, you can make it more simpler to do swaping in much more array. 通过使用交换功能,可以使在更多阵列中进行交换更加简单。

By far the most recommended way is to re-design your program, and arrange all the related items in a single class. 到目前为止,最推荐的方法是重新设计程序,并将所有相关项目安排在一个类中。 This is what objects are for, after all. 毕竟,这是对象的目的。 Then you can make the object Comparable , give it a compareTo method, and sort it. 然后,可以使对象Comparable ,为其提供一个compareTo方法,然后对其进行排序。

But if you are really unable to do that, what you should do is, whenever you exchange any two items in your sort array, make sure you exchange the corresponding items in the other arrays. 但是,如果您真的无法做到这一点,那么您应该做的是,每当交换sort数组中的任何两个项目时,请确保交换其他数组中的相应项目。

So, if you have arrays country , capital and headOfState , you will have to write something like: 因此,如果您具有数组countrycapitalheadOfState ,则必须编写如下内容:

  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;

This way, whenever you move anything in your main array, you'll also be moving the respective item in the other arrays, so they will stay together. 这样,每当您在主数组中移动任何内容时,您还将在其他数组中移动各自的项目,因此它们将保持在一起。

But again, it's much more preferred if you re-designed your program. 但是,再次重申,如果您重新设计程序,它将更为可取。

Also note the Java language conventions - variable names should not start with a capital letter, only type names should. 还要注意Java语言约定-变量名不应以大写字母开头,而只能是类型名。

You have to swap elements in Country and City arrays simultaneously. 您必须同时交换CountryCity数组中的元素。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM