Home  >  Article  >  Java  >  Simple and easy-to-understand quick learning method for Java bubble sort

Simple and easy-to-understand quick learning method for Java bubble sort

PHPz
PHPzOriginal
2024-01-30 10:21:06378browse

Simple and easy-to-understand quick learning method for Java bubble sort

Quickly master the simplest way to write Java bubble sort

Bubble sort is a simple but inefficient sorting algorithm. It sorts unused elements by repeatedly exchanging adjacent elements. The sorted maximum or minimum bubbles to the end or beginning of the sequence. This article will introduce one of the simplest ways to write bubble sort in Java and provide specific code examples.

The basic idea of ​​bubble sorting is to compare two adjacent elements and swap their positions if they are in the wrong order, so that each sorting pass will bubble the largest (or smallest) element to the sequence. end (or beginning). Repeat this process until the entire sequence is sorted. The following is the simplest way to write bubble sort:

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]) {
                    // 交换 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 : arr) {
            System.out.print(i + " ");
        }
    }
}

In the above code example, we define a BubbleSort class, in which the bubbleSort method is used to implement bubble sorting logic. In the bubbleSort method, we use a two-level loop to traverse the entire array and perform comparison and swap operations. The outer loop controls the number of sorting passes, and each pass bubbles the unsorted maximum value to the end of the sequence. The inner loop controls the comparison and exchange operations of each pass, and sorts by comparing two adjacent elements and exchanging their positions. After completing all the passes, the elements in the array will be sorted in ascending order.

In the main method, we create an array with some unordered elements and pass it to the bubbleSort method for sorting. Finally, we output the sorted results by looping over the sorted array.

Through the above code examples, we can quickly master the simple way of writing Java bubble sort. Although bubble sort is simple, it is not an efficient sorting algorithm. Its time complexity is O(n^2), and its performance is poor in sorting large-scale data. Therefore, in actual development, we prefer to use other more efficient sorting algorithms, such as quick sort, merge sort, etc.

The above is the detailed content of Simple and easy-to-understand quick learning method for Java bubble sort. 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