Java implementation of the Bubble Sort Algorithm
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 |
package com.example; public class BubbleSort { public static void main(String[] args) { int[] toSort = { 2, 9, 3, 6, 1, 0, 4, 5, 8, 7 }; System.out.print("Given Array: "); for (int i = 0; i < toSort.length; i++) { System.out.print(toSort[i] + " "); } int temp; for (int i = 0; i < toSort.length; i++) { for (int j = 1; j < toSort.length - i; j++) { if (toSort[j - 1] > toSort[j]) { temp = toSort[j - 1]; toSort[j - 1] = toSort[j]; toSort[j] = temp; } } } System.out.print("\nSorted Array: "); for (int i = 0; i < toSort.length; i++) { System.out.print(toSort[i] + " "); } } } |
Output: