简体   繁体   中英

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.

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..

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

Can be done with ONE loop (although it is not the usual way to present the bubble sort):

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. Whenever you go in the i = 0 branch, you end up restarting at 1 because of the i++ . Resulting in always skipping the 8 after the first iteration where the 9 is moved to the end.

So, either restart at -1 , or use a while loop and only increment in an else block. 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) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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