0

Can someone explain why this bubble sort is not working in my code. I would think this would sort easily. Maybe not all correctly but still somewhat sorting but instead it just returns the same array?

import java.util.Random;


public class Sorts {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //Set array to be sorted length here!
        int listLength = 20; 

        //Declares Array
        int[] toBeSortedArray = new int[listLength];
        int[] sortedArray = new int[listLength];

        //fills Array with random numbers
        for (int i = 0; i < listLength; i++)
        {
            Random rand = new Random();
            toBeSortedArray[i] = rand.nextInt(100);
        }


        //Passing toBeSortedArray to function
        sortedArray = SwapSort(toBeSortedArray, listLength);



        //Testing the filling of Array - *hint* select all comment lines and select "Toggle Block Comment" 
        for (int i = 0; i <listLength; i++)
        {
            System.out.print(toBeSortedArray[i] + ", ");
        }
        System.out.println();

        for (int i = 0; i <listLength; i++)
        {
            System.out.print(sortedArray[i] + ", ");
        }


    }

    public static int[] SwapSort(int[] array, int length)
    {
        int temp;
        for (int i = 0; i < length; i++)
        {
            for (int j = 1; j < length - 1; j++)
            {
                if (array[i] > array[j])
                {                       
                    temp = array[i];
                    array[i] = array[j+1];
                    array[j+1] = temp;
                }

            }
        }

        return array;
    }
}

Output:

55, 42, 50, 48, 9, 38, 84, 10, 81, 24, 5, 18, 32, 74, 2, 89, 15, 84, 84, 45, 
55, 42, 50, 48, 9, 38, 84, 10, 81, 24, 5, 18, 32, 74, 2, 89, 15, 84, 84, 45, 
1
  • Your output is not the same as your input, but rather the reverse: you are changing the array that is passed in, so you're modifying your input. And your algorithm is broken. But if your algorithm worked, they would still both be the same, but sorted. Commented Dec 25, 2016 at 5:43

2 Answers 2

2

Three things

First, you are swapping the wrong elements.

if (array[i] > array[j]) {
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}

You must swap the elements array[i] and array[j]

Second

Your inner loop starts must start with j = i + 1 and not 1 and should go upto length.

Third

Since you are printing both the arrays in your code after calling the function, both will give the same output since, java passes the array by reference and your original array also gets modified. So even if swapping was happening in your original code, you got the same output

Full code

class Sorts {
    public static void main(String[] args) {
        //Set array to be sorted length here!
        int listLength = 20;

        //Declares Array
        int[] toBeSortedArray = new int[listLength];
        int[] sortedArray = new int[listLength];

        //fills Array with random numbers
        for (int i = 0; i < listLength; i++) {
            Random rand = new Random();
            toBeSortedArray[i] = rand.nextInt(100);
        }

        for (int i = 0; i < listLength; i++) {
            System.out.print(toBeSortedArray[i] + ", ");
        }

        //Passing toBeSortedArray to function
        sortedArray = SwapSort(toBeSortedArray, listLength);
    }

    public static int[] SwapSort(int[] array, int length) {
        int temp;
        for (int i = 0; i < length; i++) {
            for (int j = 1; j < length; j++) {
                if (array[i] > array[j]) {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }

            }
        }
        System.out.println("");
        for (int i = 0; i < length; i++) {
            System.out.print(array[i] + ", ");
        }
        System.out.println();
        return array;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

why do you import java.lang. It is automatically imported by the jvm
@firephil I was using ideone to simulate the code. And just copy pasted the solution from there and didn't give a notice at the libraries I was importing
1

Your inner loop in SwapSort should start with int j = i + 1; j < length (and i < length - 1 in the outer loop), think about what happens when j is 1 and i is 2 in your algorithm. Also, your swap should occur on the elements you are comparing. Like,

public static int[] SwapSort(int[] array, int length) {
    int temp;
    for (int i = 0; i < length - 1; i++) {
        for (int j = i + 1; j < length; j++) {
            if (array[i] > array[j]) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
    return array;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.