Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Programming code:
import java.util.*;
class BubbleSort_fun
{
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+1] and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
/* Prints the array */
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method to test above
public static void main(String args[])
{
int no,i;
int arr[]=new int[10];
Scanner sc=new Scanner(System.in);
BubbleSort_fun ob = new BubbleSort_fun();
System.out.println("How many array elements want to create: ");
no=sc.nextInt();
System.out.println("Input "+no+" array elements: ");
for(i=0;i<no;i++)
arr[i]=sc.nextInt();
//print unsorted array
System.out.println("Unsorted array elements are: ");
for (i=0; i<no; ++i)
{
System.out.print(arr[i] + " ");
}
System.out.println();
//print sorted array
ob.bubbleSort(arr);
System.out.println("Sorted array elements are: ");
ob.printArray(arr);
}
}
class BubbleSort_fun
{
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+1] and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
/* Prints the array */
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method to test above
public static void main(String args[])
{
int no,i;
int arr[]=new int[10];
Scanner sc=new Scanner(System.in);
BubbleSort_fun ob = new BubbleSort_fun();
System.out.println("How many array elements want to create: ");
no=sc.nextInt();
System.out.println("Input "+no+" array elements: ");
for(i=0;i<no;i++)
arr[i]=sc.nextInt();
//print unsorted array
System.out.println("Unsorted array elements are: ");
for (i=0; i<no; ++i)
{
System.out.print(arr[i] + " ");
}
System.out.println();
//print sorted array
ob.bubbleSort(arr);
System.out.println("Sorted array elements are: ");
ob.printArray(arr);
}
}
Output:
How many array elements want to create:
5
How many array elements want to create:
5
Input 5 array elements:
44
78
90
12
3
44
78
90
12
3
Unsorted array elements are:
44 78 90 12 3
44 78 90 12 3
Sorted array elements are:
0 0 0 0 0 3 12 44 78 90
0 0 0 0 0 3 12 44 78 90