Write a Java Program to bubble sort 10,20,15,0,6,7,2,1,-5,55.
Including Algorithm flowchart of the program.
public class BubbleSort { public static void bubbleSort(int[] array) { // outer for loop for (int i = 0; i < array.length; i++) { // inner for loop for (int j = 0; j < array.length - i - 1; j++) { if (array[j] > array[j + 1]) { // swap the elements int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } public static void main(String[] args) { int array[] = {10, 20, 15, 0, 6, 7, 2, 1, -5, 55}; System.out.println("Before Sorting"); for (int num : array) System.out.print(num + " "); System.out.println(); bubbleSort(array); System.out.println("\nAfter Sorting"); for (int num : array) System.out.print(num + " "); System.out.println(); } }
====================================================================
FLOW CHART ( Please fit the text in the box)
Get Answers For Free
Most questions answered within 1 hours.