Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bubble Sort Algorithm – Java

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;

                }
            }
        }
    }

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading