package sorting; public class BubbleSort { public static void main(String[] args) { new BubbleSort(); } public BubbleSort() { int[] arr=new int[]{1,4,3,2,5}; System.out.println("Before Sorting..."); printArray(arr); sort(arr); System.out.println(); System.out.println("After Sorting..."); printArray(arr); } private void sort(int[] arr) { for (int i = 0; i < arr.length-1; i++) { for (int j = 0; j < (arr.length-i)-1; j++) { if(arr[j]>arr[j+1]){ int t = arr[j]; arr[j]=arr[j+1]; arr[j+1]=t; } } } } private void printArray(int arr[]){ for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
Questions on Stack, Queues, Linkedlist, Binary Trees, Sorting, Searching, Graphs etc with solution using Java Language.
Wednesday, 24 December 2014
Bubble Sort
Labels:
Sorting
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment