In the video below, Aishwarya Rai will walk you through the Java implementation of Bubble sort code based on the pseudocode discussed earlier. Let's get going. Please download the following java file for your reference.
Now let us take a look at a simple implementation of bubble sort. For that we have defined a bubble sort function which takes in an input array called numbers. So here I am declaring a variable int n which equals to numbers dot length. And I am running two for loops nested one inside each other. This for loop runs I from zero till and this inner for loop starts j from one and goes till numbers dot length minus i. Now I'm comparing all the adjacent j elements. That is if numbers of j is less than numbers of j minus one, then what I do, I swap the elements located at j minus one and j. For that I have also defined a swap function here. What the swap function does is that it takes in two numbers I and j and an array. First it stores the value of array of I in a temporary variable temp. Then I overwrite my array of I with array of j. And finally the value which I had stored in temp is getting stored now in array of j. So in this way I was able to exchange the values in I and j. So after running
these two for loops, I simply return my output array which will now consist of my final sorted array. Let us now run this code and see.
So here you have seen that I was given this particular array and I called my bubble sort function on this array. So here the array is unsorted. And take a look here on the output console here all the elements are in the sorted order. So our bubble sort works perfectly.
Bubble sort is a simple sorting algorithm used to sort an array of elements in ascending or descending order.
It works by repeatedly swapping adjacent elements if they are in the wrong order until the entire array is sorted.
The implementation of bubble sort involves defining a function that takes an input array called 'numbers'.
The function uses two nested for loops to compare adjacent elements of the array and swap them if they are not in the correct order.
A 'swap' function is also defined to facilitate the swapping of elements.
After running the two for loops, the sorted array is returned as output.
The bubble sort algorithm is demonstrated with an example array and is shown to work correctly.
Here is a question for you.
Don't you think that for each inner loop iteration if the number of swaps at any stage is 0, then we can say that the sequence is sorted and we do not need to move any further? Coming out of the loop at such a point can reduce the number of redundant steps in the code and hence the code can be optimised. For each inner loop iteration, you can check for the number of swaps at any step and compare it with 0. If the number of swaps at any step is 0, then you know the sequence is sorted now and there should be no computation further. This logic helps to optimize the bubble sort algorithm time complexity. Please look into the code below.
import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Enter the no. of elements :"); Scanner input = new Scanner(System.in); int n = input.nextInt(); int i = 0; int[] arr = new int[n]; System.out.println("Enter the elements of the array :"); for (i = 0; i < n; i++) { arr[i] = input.nextInt(); } for (i = 0; i < n; i++) { // setting the initial value of swap to zero. int swap = 0; for (int j = 1; j < n - i; j++) { if (arr[j - 1] > arr[j]) { //swapping when element at position j-1 is greater than element at j position. int temp = arr[j - 1]; arr[j - 1] = arr[j]; arr[j] = temp; swap = 1; /* if at any step the swapping is done, we change the swap value to one, so that we know that the sequence is not sorted at this step.*/ } } /*This will check if the swap value is not changed to 1, because if the value is not updated then it has not entered the inner loop even once, and no swapping was done. Therefore the sequence is sorted now.*/ if (swap == 0) { break; } } System.out.println("sorted array "); for (i = 0; i < n; i++) { System.out.println(arr[i]); } } }