The bubble sort Algorithm simply compares adjacent elements and exchanges them if they are out of order.
To apply the Bubble Sort we follow the following steps.
- Compare 1st two elements and exchange them if they are out of order.
- Move down one element and compare 2nd and 3rd elements. Exchange if necessary. Continue until end of array.
- Pass through array again, repeating process and exchanging as necessary.
- Repeat until a pass is made with no exchanges.
In this lesson we will learn how to write a source code in Java programming language for doing simple bubble sort using array in ascending order.
How to Implement Bubble Sort Algorithm in Java
public class BubbleSort { public static void main(String[] args) { int[] c={154,58,5,542,5,84,96,35,954,269,516,169,503,85,695,415,492};// System.out.print("Before Bubble sort \n"); printA(c); bubble(c); System.out.print("\nAfter Bubble sort \n"); printA(c); } public static void printA(int[] a) { for(int i=0 ; i< a.length; i++) { System.out.print(a[i]+" "); } } public static void bubble(int[] a) { int temp = 0; for(int i =a.length;i>=0; i--) { for(int j=0;j< i-1;j++) { if(a[j]>a[j+1]) { temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } } } } } /** * Output Before Bubble sort 154 58 5 542 5 84 96 35 954 269 516 169 503 85 695 415 492 After Bubble sort 5 5 35 58 84 85 96 154 169 269 415 492 503 516 542 695 954 */
- Java Simple Programs And Examples
- Java Example – Java Hello World Example
- Java Example – Math and Arithmetic Operators in Java
- Java Example – Variables and Types in Java
- Java Example – Scanner class and Getting User Input using Java
- Java Example – The If-Else If Statement, Nested If Statements, Logical Operators
- Java Example – Arrays in Java Example
- Java Example – ListIterator in Java Example
- Java Example – How to get current timestamp using Java
- Java Example – HashSet in Java with Example
- Java Example – How to read file in Java using BufferedReader
- Java Example – How to get Current Directory in Linux/Windows
- Java Example – Program to reverse an array or string
- Java Example – Sort String Array
- Java Example – Comparing two strings
- Java Example – String concatenation (join strings)
- Java Example – Java String Contains example
- Java Conversion
- Java Example – Convert String to int
- Java Example – Convert Date to String
- Java Example – Convert String to Character Array
- Java Example – Convert into string
- Java Example – Convert ArrayList to String Array
- Java Example – Convert Char Array To String
- Java Example – String Array To List
- Java Sorting algorithms & Techniques
- Java Example – Bubble Sort Algorithm
- Java Example – Insertion Sort Algorithm
- Java Example – Selection Sort Algorithm
- Java Example – Quick Sort Algorithm
- Java Example – Merge Sort Algorithm
- Java Handling Files
- Java I/O – Check If File Path Absolute or not
- Java I/O – Get file name and file path
- Java I/O – Get parent directory of the file or directory
- Java I/O – How to write to a file using BufferedWriter
- Java I/O – How to write to a file using FileOutputStream
- Java I/O – Check file permission and Set file permission
- Java I/O – Read File Using Java BufferedInputStream
- Java I/O – How to create a file in Java
Leave a Reply