Skip to main content

Bubble Sort Algorithm

Bubble Sort is the Sort Alogrithm.In Bubble Sort first we take the Unsorted Array and then Sort the Array by using Bubble Sort Alogrithm.

tip

Sorting: Arranging the Data Elements in Ascending or Decending Order.

Two Type of Bubble Sort

  • Internal Sorting
  • External Sorting

Time complexity of Bubble Sort Alogrithm : Ο(n2) time. Note: In Bubble Sort Alogrithm the Sorted values are in the right side and the unsorted values are the left side.

Bubble Sort Algorithm

Steps:

  1. Bubble sort starts with very first two elements,comparing them to check which one is greater. we have two values first values is the x[j] Position and Second value is in the x[j+1] Position.In Case the First Value is Greater then Second Value then the Values are Swapped.In this array 5 is greater then 4 then values are swapped.
  2. When the Values are swapped then the Second value 5 at the consider as x[j] Position and third value 2 is x[j+1].In this case checked the value if the 5 is greater then 2 then value are swapped.
  3. The third value 5 is compare with the fourth value 1. 5 is in x[j] poisition and 1 is x[j+1] position. The condition is Checked x[j] > x[j+1] then value are swapped.
  4. After values are swapped then the Fourth value is 5 and Six value is 3. The 5 value at the x[j] poisition and 3 is x[j+1] position.The condition is Checked x[j] > x[j+1] then value are swapped.After Swapping the value 5 and 3 then you can See that the Bubble Sort First Iteration is Completed the value five is arrived on their path.
  5. In Second Iteration You can Compare the first two values if the first value is greater then second value then values are swapped otherwise values are not swapped. the first value 5 position is x[j] and Secodn value 2 is x[j+1] position.The condition is Checked x[j] > x[j+1].In this case the 5 is greater then 2 the value are swapped.
  6. After Swapping the Values then the second condition is checked. The second value 4 is compare with third value 1. the second value 4 is in the x[j] Position and the third value 1 is at x[j+1] Position.The condition is Checked x[j] > x[j+1].In this case the 4 is greater then 1 the value are swapped.
  7. After Swapping the values the the third condition is checked. The third value 4 is compare with fourth value 3. the third value 4 is in the x[j] Position and the fourth value 3 is at x[j+1] Position.The condition is Checked x[j] > x[j+1].In this case the 4 is greater then 3 the value are swapped.
  8. At the End the Fourth Condition is checked.The Four position value are 4 then five position value is 5. The condition is again checked again. Then the condition is false the values at the Second Iteration is Completed are sorted.Then again the third iteration is start.
  9. After completed the second iteration then Third iteration is start.In this iteration is the outer loop is start and the Four time inner loop are executed.The First Value 2 is Compare with the Second Value is 1. After Checked the condition again the first value 2 is in x[j] Position and the second value 1 is at x[j+1] Position.The condition is Checked x[j] > x[j+1].
  10. After Swapping the value the second value is the 2 and third value is 3.the second value 2 is in x[j] Position and the third value 3 is at x[j+1] Position.The condition is Checked x[j] > x[j+1].In this case the 2 is not greater then 3 the value are not swapped.
http://localhost:3000
import java.util.*;
class Sort{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int i,j,temp;
System.out.println("Enter Total Value");
int n=input.nextInt(); //Input Size of the Array

int[] x = new int[n];
System.out.println("Enter "+ n+" Value");
for(i=0;i<n;i++){
int a=input.nextInt();
x[i]=a;
}
for(i=0;i<n; i++){
for(j=0;j<n-1; j++){
if(x[j]>x[j+1]){ //Swap Values
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
System.out.println("Array Sorting");
for(i=0;i<n; i++){
System.out.println(x[i]);
}
}
}


// Method 02

class secondMethod{
public static void main(String [] args){
int x[] = {5,3,1,2,4};
int i,j,temp,n;
n = x.length; // Calculate the length of the Array
for(i=0;i<n; i++){
for(j=1;j<n; j++){
if(x[j]<x[j-1]){
temp=x[j];
x[j]=x[j-1];
x[j-1]=temp;
}
}
}
System.out.println("Array Sorting");
for(i=0;i<n; i++){
System.out.println(x[i]); //Print Swap Values
}
}
}