diff --git a/src/main/java/com/thealgorithms/backtracking/BubbleSort.java b/src/main/java/com/thealgorithms/backtracking/BubbleSort.java new file mode 100644 index 000000000000..698e9e681a02 --- /dev/null +++ b/src/main/java/com/thealgorithms/backtracking/BubbleSort.java @@ -0,0 +1,24 @@ +public class BubbleSort { + public static void main(String[] args) { + int[] array = {64, 34, 25, 12, 22, 11, 90}; + bubbleSort(array); + System.out.println("Sorted array:"); + for (int num : array) { + System.out.print(num + " "); + } + } + + public static void bubbleSort(int[] array) { + int n = array.length; + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (array[j] > array[j + 1]) { + // Swap array[j] and array[j + 1] + int temp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temp; + } + } + } + } +}