Bubble sort in java with explanation: –
If arr[j] > arr[j+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)
Bubble Sort in java Program
Example 1
public class Sorting {
public static void main(String[] args) {
int[]arr = {7,8,3,1,2}; //unsorted Array
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1; j++) {
if(arr[j]>arr[j+1]) {
//swap
int temp;
temp =arr[j];
arr[j] =arr[j+1];
arr[j+1] =temp;
}
}
}
System.out.print("Sorted Array =" +" ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
Output: Sorted Array = 1 2 3 7 8
You Also Learn –
- What is Abstract Class in Java
- What is Lambda Expression in Java 8
- What is Exception Handling in Java
- Types of Exceptions in Java
- What is inheritance in Java
- What is the Final Keyword in Java
- What is Interface in Java
- What is Wrapper Class in Java
- Break Keywords in Java with Example
- What is Method In Java With Example
- What is Constructor in Java With Example
- What is Polymorphism in java
- What is a non-static variable in java
- Types of access specifiers in java
- What is the local variable in java
- Types of loops in java with program
- Remove duplicate elements in an Array in java
- What is var type in java
- What is a static variable in java
- What is the difference between return and return value?
Example 2 – Bubble Sort in java Program
public class Bubble_Sort {
public static void main(String[] args) {
int arr[] = {3,2,5,8,5,2,1,5};
System.out.println ("Before sorting Array ="+" ");
for (int i= 0; i< arr.length; i++) {
System.out.print(arr[i]+" ");
}
//Bubble sorting
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1 ; j++) {
if (arr[j]>arr[j+1]) {
//swap
int temp;
temp =arr[j];
arr[j] =arr[j+1];
arr[j+1]= temp;
}
}
}
System.out.println();
System.out.println("After Swapping ="+" ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
Output:
Before sorting Array =
3 2 5 8 5 2 1 5
After Swapping =
1 2 2 3 5 5 5 8
Note: After sorting remove duplicate elements in an Array
public class Bubble_Sort {
public static void main(String[] args) {
int arr[] = {3,2,5,8,5,2,1,5};
System.out.println ("Before swapping Array ="+" ");
for (int i= 0; i< arr.length; i++) {
System.out.print(arr[i]+" ");
}
//Bubble sorting
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1 ; j++) {
if (arr[j]>arr[j+1]) {
//swap
int temp;
temp =arr[j];
arr[j] =arr[j+1];
arr[j+1]= temp;
}
}
}
System.out.println();
System.out.println("After Swapping Array ="+" ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
int j=0;
for (int i = 0; i < arr.length-1; i++) {
if (arr[i]!=arr[i+1]) {
arr[j]=arr[i];
j++;
}
}
arr[j]=arr[arr.length-1];
System.err.println();
System.out.println("Remove Duplicate elements in Array =");
for (int k = 0; k <=j; k++) {
System.out.print(arr[k]+" ");
}
}
}
Output:
Before swapping Array =
3 2 5 8 5 2 1 5
After Swapping Array =
1 2 2 3 5 5 5 8
Remove Duplicate elements in Array =
1 2 3 5 8
Remove Duplicate Elements in Unsorted Array
Example 1
in this program, first sort an array and then remove duplicate elements in a sorted array.
public class Remove_Duplicate {
public static void main(String[] args) {
int[] arr = {2,3,6,5,7,5,4,3}; //unsorted Arry
// first Sorting Array
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1; j++) {
if (arr[j]>arr[j+1]) {
// swap
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
// Second Step - Remove Duplicate Elements
int j=0;
for (int i = 0; i < arr.length-1; i++) {
if (arr[i]!=arr[i+1]) {
arr[j]=arr[i];
j++;
}
}
System.out.println();
arr[j]=arr[arr.length-1];
for (int i = 0; i <=j; i++) {
System.out.print(arr[i]+" ");
}
}
}
Output:
2 3 3 4 5 5 6 7
2 3 4 5 6 7
Bubble sort best example – unsorted Array to convert sort Array by bubble sort and then remove Duplicate elements in an Array
public class A {
public static void sort(int[] x) {
for (int i = 0; i < x.length-1; i++) {
for (int j = 0; j < x.length-1; j++) {
if (x[j]>x[j+1]) {
int temp =x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
}
public static void removeDuplicate(int[] x) {
int[] temp = new int [x.length];
int j=0;
for (int i = 0; i < temp.length-1; i++) {
if (x[i]!=x[i+1]) {
temp[j]=x[i];
j++;
}
}
System.out.println();
temp[j]=x[x.length-1];
for (int i = 0; i < j+1; i++) {
System.out.print(temp[i]+" ");
}
}
public static void main( String[] args) {
int[] y= {1,2,1,3,4,5,0};
A.sort(y);
for (int m = 0; m < y.length; m++) {
System.out.print(y[m]+" ");
}
A.removeDuplicate(y);
}
}
Output:
0 1 1 2 3 4 5
0 1 2 3 4 5
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.