Skip to main content

Sorting in Java | Bubble Sort Algorithm | Java Placement Course 16


Sorting in JAVA



  1. Bubble Sort


Bubble Sort is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. It gets its name from the way the smaller elements "bubble" to the top of the list. Here is the basic algorithm:


1) Start with an unsorted list of n elements
2) Loop through the list n-1 times (the outer loop).
3) For each iteration of the outer loop, loop through the unsorted portion of the list (the inner loop).
4) Compare each adjacent pair of elements. If they are in the wrong order, swap them.
5) After each iteration of the inner loop, the largest element in the unsorted portion of the list will "bubble up" to the end of the list.
6) After the outer loop has finished, the list will be sorted.

Idea: if arr[i] > arr[i+1] swap them. To place the element in their respective position, we have to do the following operation N-1 times. 

Time Complexity: O(N2)


Code

import java.util.*;

class Sorting {

   public static void printArray(int arr[]) {

       for(int i=0; i<arr.length; i++) {

           System.out.print(arr[i]+" ");

       }

       System.out.println();

   }

 

   public static void main(String args[]) {

       int arr[] = {7, 8, 1, 3, 2};

 

       //bubble sort

       for(int i=0; i<arr.length-1; i++) {

           for(int j=0; j<arr.length-i-1; j++) {

               if(arr[j] > arr[j+1]) {

                   //swap

                   int temp = arr[j];

                   arr[j] = arr[j+1];

                   arr[j+1] = temp;

               }

           }

       }

 

       printArray(arr);

   }

}





Sure! Here's an implementation of the Bubble Sort algorithm in Java:

css
public static void bubbleSort(int[] arr) {
 int n = arr.length
 for (int i = 0; i < n - 1; i++) {
 for (int j = 0; j < n - i - 1; j++)
 { if (arr[j] > arr[j+1]{ // Swap arr[j] and arr[j+1] 
 int temp = arr[j]
 arr[j] = arr[j+1]
 arr[j+1] = temp; } 
 }
 }
 }



In this implementation, arr is the unsorted array, n is the length of the array, and i and j are loop variables. The inner loop (for j) compares adjacent elements and swaps them if necessary. The outer loop (for i) controls how many times the inner loop should be executed.

To use this method, you can simply call it with your array as an argument:



int[] arr = {5, 2, 8, 3, 1, 6}; bubbleSort(arr);



After the method returns, arr will be sorted in ascending order.

Note that, like the Python implementation, this implementation of Bubble Sort has a time complexity of O(n^2), which means that it can be slow for large arrays.





 Others blog:-

My Gadgets 



Comments

Popular posts from this blog

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

2D Arrays | Java Complete Placement Course | Lecture 11

Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

Merge Sort | How do I learn merge sort?

       Merge Sort | For Beginners | Java Placement Course >> Merge sort is a popular sorting algorithm that utilizes a divide-and-conquer approach to sort elements in an array. The basic idea behind merge sort is to divide an unsorted array into two halves, recursively sort each half, and then merge the two sorted halves into a single sorted array. >>  Merge sort algorithm works by recursively dividing an array into two halves, sorting each half, and then merging them to obtain the sorted array. The basic steps of the merge sort algorithm are as follows: Divide the unsorted array into two halves, mid and left. Sort the left half recursively using merge sort algorithm. Sort the right half recursively using merge sort algorithm. Merge the two sorted halves to obtain the final sorted array. JAVA Implementation: // Merge sort public class MergeSort {     //time complexity=nlogn     public static void conquer ( int arr [], int ...