Bubble Sort Algorithm Java
Here we are going to discuss java bubble sort algorithm examples. There are many sorting algorithm including bubble sort like merge sort, quick sort, selection sort, insertion sort in java.
In this post, We will see java programs for bubble sort algorithm with ascending and descending order in java.
Let's start bubble sort example step-by-step.
First, Question is arise what is bubble sort and how it works?
In this post, We will see java programs for bubble sort algorithm with ascending and descending order in java.
Let's start bubble sort example step-by-step.
First, Question is arise what is bubble sort and how it works?
What is Bubble Sort?
Bubble sort is a simple sorting algorithm in java. It is basically used to sort the elements in both cases i.e ascending and descending order.
In bubble sort, Array is traversed from first element to last elements and current element is compared with the next element. If current element is greater than the next element, it will swap.
Bubble Sort Ascending Order Java Example
This is the simple java program to arrange the digits of number in ascending order by using bubble sort. We will take integer array to traversing.
For example:
class BubbleSortExample
{
static void bubbleSort(int arr[])
{
int number = arr.length;
int temp = 0;
for(int i = 0; i < number; i++)
{
for(int j = 1; j < (number-i); j++)
{
if(arr[j-1] > arr[j])
{
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String args[])
{
int arr[] = {10, 7, 8, 50, 4, 2, 9, 17};
System.out.println("Array before using bubble sort");
for(int i = 0; i<arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();
bubbleSort(arr);
System.out.println("Array after using bubble sort");
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
}
}
Output: Array before using bubble sort
10 7 8 50 4 2 9 17
Array after using bubble sort
2 4 7 8 9 10 17 50
static void bubbleSort(int arr[])
{
int number = arr.length;
int temp = 0;
for(int i = 0; i < number; i++)
{
for(int j = 1; j < (number-i); j++)
{
if(arr[j-1] > arr[j])
{
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String args[])
{
int arr[] = {10, 7, 8, 50, 4, 2, 9, 17};
System.out.println("Array before using bubble sort");
for(int i = 0; i<arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();
bubbleSort(arr);
System.out.println("Array after using bubble sort");
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
}
}
Output: Array before using bubble sort
10 7 8 50 4 2 9 17
Array after using bubble sort
2 4 7 8 9 10 17 50
Bubble Sort Descending Order Java Example
This is another example of bubble sort but here we are going to perform array elements in descending order with little change. Change logic here " if(arr[j-1] > arr[j]) to if(arr[j-1] < arr[j]) ".
For example:
class BubbleSortExample
{
static void bubbleSort(int arr[])
{
int number = arr.length;
int temp = 0;
for(int i = 0; i < number; i++)
{
for(int j = 1; j < (number-i); j++)
{
if(arr[j-1] < arr[j])//change logic here for descending order
{
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String args[])
{
int arr[] = {10, 7, 8, 50, 4, 2, 9, 17};
System.out.println("Array before using bubble sort");
for(int i = 0; i<arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();
bubbleSort(arr);
System.out.println("Array after using bubble sort");
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
}
}
Output: Array before using bubble sort
10 7 8 50 4 2 9 17
Array after using bubble sort
50 17 10 9 8 7 4 2
Read more:
Java Selection Sort Algorithm Example.
Java Insertion Sort Algorithm Example.
Java Program to Find Area of Square.
Bubble sort algorithm is a simple algorithm. Above we performed java bubble sort with ascending and descending order. This is the simple sorting program in java.
static void bubbleSort(int arr[])
{
int number = arr.length;
int temp = 0;
for(int i = 0; i < number; i++)
{
for(int j = 1; j < (number-i); j++)
{
if(arr[j-1] < arr[j])//change logic here for descending order
{
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String args[])
{
int arr[] = {10, 7, 8, 50, 4, 2, 9, 17};
System.out.println("Array before using bubble sort");
for(int i = 0; i<arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();
bubbleSort(arr);
System.out.println("Array after using bubble sort");
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
}
}
Output: Array before using bubble sort
10 7 8 50 4 2 9 17
Array after using bubble sort
50 17 10 9 8 7 4 2
Read more:
Java Selection Sort Algorithm Example.
Java Insertion Sort Algorithm Example.
Java Program to Find Area of Square.
Bubble sort algorithm is a simple algorithm. Above we performed java bubble sort with ascending and descending order. This is the simple sorting program in java.
Thanks for sharing this informative blog java training in velachery
ReplyDeleteI was searching for this program thanks
ReplyDeletejava training in chennai