Home > database >  Bubble Sort Algorithm - Java
Bubble Sort Algorithm - Java

Time:12-20

Can someone please explain this code used for Bubble sort?

I'm confused with all sorting techniques. So I'm learning again from scratch. This time wanted to make sure I'm clear with the code.

NB: I know how bubble sorting works. But don't know how the code works(Nobody explains the code :( ). So I request anyone to explain each line for me(how the swapping works).

public static void bubbleSort(int [] sort_arr, int len){
        
        for (int i=0;i<len-1;  i){

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

                if(sort_arr[j 1]<sort_arr[j]){

                    int swap = sort_arr[j];
                    sort_arr[j] = sort_arr[j 1];
                    sort_arr[j 1] = swap;

                }
            }
        }
    }

CodePudding user response:

Bubble sort is an algorithm that repeatedly iterates through an array, compares adjacent elements, and swaps their positions if they are not in the correct order. The algorithm repeats this process until the array is sorted.

Here is an explanation of the code:

public static void bubbleSort(int [] sort_arr, int len){

This is the function definition for a bubble sort function. It takes in an array of integers, sort_arr, and the length of the array, len. The keyword public means that the function can be called from anywhere in the program, static means that the function does not depend on any particular instance of a class, and void means that the function does not return a value.

for (int i=0;i<len-1;  i){

This is the first loop. It iterates i from 0 to len - 1, and will execute the code inside the loop body for each value of i.

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

This is the second loop, nested inside the first loop. It iterates j from 0 to len - i - 1. The purpose of this loop is to iterate through the array from the beginning and compare each element with its neighbor to the right.

if(sort_arr[j 1]<sort_arr[j]){ This is an if statement that checks if the element at index j 1 is less than the element at index j. If this condition is true, it means that the elements are not in the correct order, and they need to be swapped.

int swap = sort_arr[j];
sort_arr[j] = sort_arr[j 1];
sort_arr[j 1] = swap;

This is the code that performs the swap. It stores the value of the element at index j in a temporary variable called swap. Then, it assigns the value of the element at index j 1 to the element at index j, and assigns the value stored in swap to the element at index j 1. This effectively swaps the positions of the two elements.

} This closes the if statement.

} This closes the second loop.

} This closes the first loop.

} This closes the function definition.

CodePudding user response:

The way bubble sort works is that the largest element of the array will move to the len-i-1 position for each iteration of the outer for loop (the one with the i)

So, if I have: 0-5-3-4-1 The largest element will float until len-i-1 (with i=0): O-O-O-O-X

After the first iteration of i you will get: 0-3-4-1-5

Then the i will increment, so len-i-1 will now correspond to the 3rd index: O-O-O-X-O

So the largest element will again float until it reaches that spot, resulting in: 0-3-1-4-5

Then the process just repeats itself until len-i-1 corresponds to the 1st index, when it does, the array should be ordered as intended.

This gif might help you understand it a bit better

enter image description here

  • Related