Bubble Sort Algorithm in Java in decreasing order
public class BubbleSortDescending { public static void bubbleSort(int[] list) { int temp; for (int i = 0; i < list.length; ++i) { for (int j = 0; j < list.length - 1; ++j) { if (list[j] < list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } } public static void print(int[] list) { for (int i = 0; i < list.length; ++i) { System.out.print(list[i] + " "); } System.out.println(); } public static void main(String[] args) { int[] arr = {9, 12, 17, 19, 21, 15, 6, 5}; System.out.print("Original array: "); print(arr); bubbleSort(arr); System.out.print("Sorted array: "); print(arr); } }
Get Answers For Free
Most questions answered within 1 hours.