简体   繁体   English

Java气泡排序

[英]Java bubble sort

i'm trying to create a bubble sort but I there is something wrong with my code. 我正在尝试创建冒泡排序,但是我的代码有问题。 The output is : 82345679. I would like it to be : 23456789. 输出是:82345679。我希望它是:23456789。

package com.company;

public class Main {

    public static void main(String[] args) {
        // write your code here

        int[] tab = {9,8,7,6,5,4,3,2};
        int[] result = {9,8,7,6,5,4,3,2};

        for (int i = 0; i < result.length; i++ ) {
            if (i < result.length - 1 ) {
                if (result[i] > result[i+1]) {
                    result = permute(result, i);
                    i = 0;
                }
            }
        }

        for (int i: result) {
            System.out.print(i);
        }

    }

    public static int[] permute (int[] tableau, int index) {
        int temp;
        temp = tableau[index];
        tableau[index] = tableau[index+1];
        tableau[index+1] = temp;
        return tableau;
    }
}

You need two loops. 您需要两个循环。

int swap;
for (int i = 0; i < ( result.length - 1 ); i++) {
    for (int j = 0; j < result.length - 1; j++) {
        if (result[j] > result[j+1]) {
          swap = result[j];
          result[j] = result[j+1];
          result[j+1] = swap;
        }
    }
}

You need to have 2 loops in order to compare each number to the whole array.. 您需要有2个循环才能将每个数字与整个数组进行比较。

example of bubble sorting 气泡分选示例

public static void bubbleSort(int[] numArray) {

    int n = numArray.length;
    int temp = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n - i); j++) {

            if (numArray[j - 1] > numArray[j]) {
                temp = numArray[j - 1];
                numArray[j - 1] = numArray[j];
                numArray[j] = temp;
            }

        }
    }
}

Refer to this question 参考这个问题

Sorting an Array of int using BubbleSort 使用BubbleSort对int数组进行排序

Can be done with ONE loop (although it is not the usual way to present the bubble sort): 可以使用ONE循环完成(尽管这不是呈现冒泡排序的常用方法):

public static void main (String args[]) {

    int[] tab = {9,8,7,6,5,4,3,2};

    int i=1;                   // let's do the bubble sort again
    while (i < tab.length) {

        // loop invariant :  t[0] <= t[1] .... <= t[i-1]

        if (tab[i-1] < tab[i]) {   // bubble here
            swap(tab, i-1, i);
            if (i>1) {
                i = i-1;  // one step to the left....
            }
        } else {
            i = i +1;     // one step to the right 
        }
    }

    for (int x: tab) {
        System.out.print(x);
    }
}

static void swap(int[] t, int i, int j) {
    int x = t[i];
    t[i] = t[j];
    t[j] = x;
}

The issue is with the combination of i = 0 and i++ in the for loop. 问题在于for循环中i = 0i++的组合。 Whenever you go in the i = 0 branch, you end up restarting at 1 because of the i++ . 每当您进入i = 0分支时,由于i++ ,最终都会从1重新开始。 Resulting in always skipping the 8 after the first iteration where the 9 is moved to the end. 导致总是在第一次迭代之后跳过8 ,将9移到末尾。

So, either restart at -1 , or use a while loop and only increment in an else block. 因此,要么从-1重新开始,要么使用while循环,仅在else块中递增。 For example: 例如:

int i = 0;
while (i < result.length - 1) {
    if (result[i] > result[i+1]) {
        permute(result, i)
        i = 0;
    } else {
        i++;
    }
}

However, I would advise against the one-loop bubble sort, because the algorithm complexity is harder to see (it is still O(n^2) , but with only one loop it can give the impression that it is O(n) ). 但是,我建议不要使用单循环冒泡排序,因为算法的复杂性很难看清(它仍然是O(n^2) ,但是只有一个循环就可以给人以O(n)的印象) 。

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

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