kSingh
kSingh

Reputation: 23

BubbleSort Algorithm Returning Undesired Results

Here is my implementation of the bubble sort algorithm.

 import java.util.Arrays;

 public class BubbleSort {
    public static void main(String[] args) {
        int[] unsorted = {1,3,5,6,2};
        System.out.println(Arrays.toString(unsorted));
        bubbleSort(unsorted);
        System.out.println(Arrays.toString(unsorted));
    }

    public static void bubbleSort(int[] unsorted){
        int i;
        int j;
        int temp;
        for (i = 0; i < unsorted.length; i++) {
            for (j = 1; j < unsorted.length-1; j++) {
                if (unsorted[i] > unsorted[j]) {
                    temp = unsorted[i];
                    unsorted[i] = unsorted[j];
                   unsorted[j] = temp;
                 }
             }
        }
   }
 }

Here is the output:

[1, 3, 5, 6, 2]
[1, 6, 5, 3, 2]

This is clearly wrong, but my logic seems alright.

Upvotes: 2

Views: 79

Answers (4)

otc
otc

Reputation: 754

A few things 1) you can declare i and j within the loop scope 2) try to keep it consistent both iterations 0 -> length 3) the less then sign in i < unsorted.length will make sure that you do not go past the index bounds. because array indexes range from 0 to length-1. 4) your swap logic is great the only thing that needed attention was the loop range. 5) unsorted[i] < unsorted[j] will determine which way the order will go.

public static void bubbleSort(int[] unsorted) {
    for (int i = 0; i < unsorted.length; i++) {
        for (int j = 0; j < unsorted.length; j++) {
            if (unsorted[i] < unsorted[j]) {
                int temp = unsorted[i];
                unsorted[i] = unsorted[j];
                unsorted[j] = temp;
            }
        }
    }
}

Upvotes: 1

Dani
Dani

Reputation: 2046

I have saved bubble sort algorithm in my computer, is this (without copy to a new array):

public static void bubbleSort (int[] v) {
      for (int i=0; i<v.length-1; i++)
         for (int j=v.length-1; j>i; j--)
            if (v[j-1]>v[j]) {
               int aux = v[j-1];
               v[j-1] = v[j];
               v[j] = aux;   
            }
   }

Upvotes: 0

Vivien SA&#39;A
Vivien SA&#39;A

Reputation: 767

here is the correct code

> import java.util.Arrays;

 public class BubbleSort {
    public static void main(String[] args) {
        int[] unsorted = {1,3,5,6,2};
        System.out.println(Arrays.toString(unsorted));
        bubbleSort(unsorted);
        System.out.println(Arrays.toString(unsorted));
    }

    public static void bubbleSort(int[] unsorted){
        int i;
        int j;
        int temp;
        for (i = 0; i < unsorted.length-1; i++) {
            for (j = i+1; j < unsorted.length; j++) {
                if (unsorted[i] > unsorted[j]) {
                    temp = unsorted[i];
                    unsorted[i] = unsorted[j];
                   unsorted[j] = temp;
                 }
             }
        }
   }
 }

the second loop begins after i so j alway start at i+1 and end at unsorted.length while i end at unsorted.length-1

here is the result I got when I ran this code

Upvotes: 0

Michael
Michael

Reputation: 44250

Both of your problems are with this line:

for (j = 1; j < unsorted.length-1; j++) {

You don't want to loop from the first element, you want to loop from the first element after i:

for (j = i+1; j < unsorted.length-1; j++) {

You also need to go all the way to the end:

for (j = i+1; j < unsorted.length; j++) {

Upvotes: 3

Related Questions