Home >Java >javaTutorial >Classic Java bubble sort implementation

Classic Java bubble sort implementation

WBOY
WBOYOriginal
2024-01-05 09:04:50843browse

Classic Java bubble sort implementation

Classic implementation method of Java bubble sort

The bubble sort algorithm is a simple and commonly used sorting algorithm that is often used in actual development. Its basic idea is to start from the starting position of the element sequence to be sorted, and compare the sizes of two adjacent elements in sequence. If the order is incorrect, swap them until the entire sequence is in order. The following will introduce the specific implementation method of bubble sorting, and attach Java code examples.

The core of the bubble sorting algorithm is to achieve the purpose of sorting by constantly comparing the sizes of adjacent elements and exchanging them as needed. The flow of the algorithm is as follows:

  1. Traverse the sequence of elements to be sorted and compare the current element with its next element.
  2. If the current element is larger than the next element, swap the positions of the two elements.
  3. Repeat the above operations until the entire sequence is in order.

The following is a code example using Java language to implement the bubble sort algorithm:

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 };
        bubbleSort(arr);
        System.out.println("排序后的数组:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

In the above code, the bubbleSort method is used to implement the bubble sort algorithm . First, we use two nested loops to iterate over the array to be sorted. The outer loop controls the number of rounds of traversal, and the inner loop controls the comparison and exchange of each round. By comparing the sizes of adjacent elements, if the order is incorrect, a swap operation is performed. Finally, when all elements are arranged in order, the sorting is completed.

In the main method, we create an array containing some integers and call the bubbleSort method to sort it. Finally, we output the sorted array to the console.

The time complexity of the bubble sort algorithm is O(n^2), where n is the length of the array to be sorted. Although the performance of the bubble sort algorithm is poor, because its implementation is simple and intuitive, it still has certain practicality for small-scale data sorting.

To sum up, the bubble sort algorithm is a simple and commonly used sorting algorithm. By constantly comparing the sizes of adjacent elements and performing swap operations, the entire sequence can be gradually ordered. In actual development, we can use Java language to implement the bubble sorting algorithm. Through the explanation and demonstration of code examples, we hope that readers can better understand and master this classic sorting algorithm.

The above is the detailed content of Classic Java bubble sort implementation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn