Bubble Sort in Java: A Easy Sorting Algorithm

Bubble Sort is a widely-used sorting algorithm in Java that arranges elements in ascending or descending order. In this article, we will dive into Bubble Sort in Java, understand how it works, and explore a code example to implement this sorting algorithm in Java. By the end, you will have a clear understanding of Bubble Sort in Java and its practical application.

Bubble Sort in Java

Bubble Sort in Java: Explained with Code Example

public class BubbleSort {
    public static void bubbleSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {64, 34, 25, 12, 22, 11, 90};

        System.out.println("Array before sorting:");
        for (int num : arr) {
            System.out.print(num + " ");
        }

        bubbleSort(arr);

        System.out.println("\n\nArray after sorting:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

Output

Array before sorting:
64 34 25 12 22 11 90 

Array after sorting:
11 12 22 25 34 64 90

Bubble Sort in Java: Step-by-Step Process

  1. Start with an unsorted array.
  2. Compare adjacent elements and swap them if they are in the wrong order.
  3. Repeat the comparison and swap process until the largest element moves to the end of the array.
  4. Continue the same process for the remaining unsorted elements, excluding the last sorted element.
  5. Repeat steps 2-4 until the entire array is sorted.

The code example provided demonstrates the Bubble Sort algorithm in action. It takes an array of integers, performs the sorting operation, and displays the sorted array. The “bubbleSort” method implements the Bubble Sort logic by comparing and swapping adjacent elements.

Time Complexity: The time complexity of Bubble Sort in Java is O(n^2), where n is the number of elements in the array. This is because in the worst case scenario, Bubble Sort requires nested loops to compare and swap adjacent elements until the array is sorted. The outer loop runs n times, and the inner loop runs n – 1 times for each iteration of the outer loop.

Space Complexity : The space complexity of Bubble Sort in Java is O(1) because the algorithm sorts the elements in-place, without requiring additional space proportional to the input size. Bubble Sort only requires a constant amount of extra space to store temporary variables used for swapping elements.

Bubble Sort in Java: Conclusion

Bubble Sort in Java is a simple yet effective sorting algorithm for small data sets. However, it is not the most efficient choice for larger arrays due to its time complexity. Nonetheless, understanding Bubble Sort is valuable as it forms the foundation for learning more advanced sorting algorithms.

By implementing Bubble Sort in Java using the provided code example and following the step-by-step process, you can gain a solid understanding of this sorting technique. Use this knowledge to explore other sorting algorithms and enhance your Java programming skills. Mastering Bubble Sort in Java will empower you to tackle various sorting challenges efficiently.

Related Links :

Leave a Reply

Your email address will not be published. Required fields are marked *