0

Hi I'm trying to figure out how to use BubbleSort in Java and my code is erroring and I don't know why

import java.util.ArrayList;

public class SortsRunner {

    public static void BubbleSort(ArrayList<Integer> nums) {
        ArrayList<Integer> arr = new ArrayList<Integer>();
        int n = arr.size();
        for (int i = 0; i < n-1; i++) 
            for (int j = 0; j < n-i-1; j++) 
                if (arr.get(j) > arr.get(j+1)) 
                { 

                    int temp = arr.get(j); 
                    arr.get(j) = arr.get(j+1); 
                    arr.get(j+1) = temp; 
                }
        }
    public static void SelectionSort(ArrayList<Integer> nums) {

        }
    public static void printArrayList(ArrayList<Integer> nums) {
        for(int i = 0; i < nums.size(); i++) {
            System.out.println(nums.get(i) + " ");
            }
        System.out.println();
        }
    public static ArrayList<Integer> makeRandomArrayList() {
        ArrayList<Integer> nums = new ArrayList<>();
        for(int i = 0; i < (int)(Math.random() * 11) + 5; i++) {
            nums.add((int)(Math.random() * 100));
            }
        return nums;
        }

public static void main(String[] args) {
printArrayList(makeRandomArrayList());

}

}

When I get to arr.get(j) = arr.get(j+1); and arr.get(j+1) = temp; the left side errors saying "The left-hand side of an assignment must be a variable." can anyone help me fix this?

3
  • 2
    Does this answer your question? ArrayList replace element if exists at a given index? Commented Feb 27, 2020 at 14:47
  • Use .set() not .get(). Commented Feb 27, 2020 at 14:48
  • You may want to use a boolean to determine if your inner loop did a swap. If it didn't, the list is sorted. Commented Feb 27, 2020 at 16:13

2 Answers 2

2
arr.get(j) = arr.get(j+1);
arr.get(j+1) = temp; 

You're trying to assign a value to the result of a method call.

You just can't do this. You can only assign to a local variable, a field in the current class, a field access (e.g. foo.bar = ...) or an array element (e.g. foo[0] = ...).

Instead, you should use set to update a list element:

arr.set(j, arr.get(j+1));
arr.set(j+1, temp);

For the specific case of swapping two elements around in a list, you can instead use Collections.swap:

Collections.swap(arr, j, j+1);
Sign up to request clarification or add additional context in comments.

Comments

0

You are doing several things wrong.

  1. The obivous get and set issues already mentioned.
  2. The fact that your are sorting an empty list. You pass in nums but sort the one you create which is empty.
  3. You should use a boolean to prevent unnecessary repeats of the outer loop. Think of it like this, if you don't make a swap on the first iteration of the outer loop, then you won't swap on subsequent iterations.

And one style suggestion. Don't use loops or if statements without {}. Even if they only contain a single line of code. You will be less likely to make coding errors if you do so.

Try the following:

    public static void BubbleSort(List<Integer> nums) {
        int n = nums.size();
        for (int i = 0; i < n; i++) {
            boolean swapped = false;
            for (int j = 0; j < n-1; j++) {
                if (nums.get(j) > nums.get(j + 1)) {
                    int temp = nums.get(j);
                    nums.set(j, nums.get(j + 1));
                    nums.set(j + 1, temp);
                    swapped = true;
                }
            }
            if (!swapped) {
                break;
            }
        }
    }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.