Bubble sort algorithm is the simplest sorting algorithm.
In bubble sort algorithm, array is traversed from first element to last element.
Here, first element is compared with the next element.If first element is greater than the next element, then it is swapped ,and this process continue till last element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class BubbleSort { public static void method_1(int[] arr){ System.out.println("ELEMENTS BEFORE SORTING : "); for (int i : arr) { System.out.print(i+","); } for(int i=0 ; i < arr.length ; i++){ for(int j=0 ; j < arr.length-1 ;j++){ if(arr[j] > arr[j+1]){ int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } System.out.println("\n\nELEMENTS AFTER SORTING : "); for (int i : arr) { System.out.print(i+","); } } public static void main(String[] args) { int[] arr = {5,6,3,1,2}; BubbleSort.method_1(arr); } } |
Output:
ELEMENTS BEFORE SORTING :
5,6,3,1,2,
ELEMENTS AFTER SORTING :
1,2,3,5,6,
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment