Sorting algorithm---bubble sort (java version)

Bubble Sort

principle

Bubble Sort is a simple sorting algorithm that compares two adjacent elements in turn to see whether the two elements meet the requirements of the size relationship, and if they do not, exchange the two elements. Each bubbling will move at least one element to the position where it should be, so that n bubbling completes the sorting of n data. The origin of the name of this algorithm is because the smaller the element will slowly "float" to the top of the sequence through exchange.

Algorithm process description

  • Compare adjacent elements. If the first one is larger than the second, swap the two;
  • Do the same work for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end, so that the last element should be the largest number;
  • Repeat the above two steps for all elements except the last one;
  • Repeat the first three steps until the sorting is complete.

The algorithm execution process is implemented by the code as shown in the figure below
Insert picture description here

public class BubbleSort {
    
    
    public static void main(String[] args) {
    
    
        int arr[] = {
    
    3, 9, -1, 10, 11};
        BubbleSort(arr);
    }

    public static int[] BubbleSort(int arr[]) {
    
    
        int temp = 0;
        //标志是否一次排序经过元素之间的交换
        boolean flag = false;
        for (int i = 0; i < arr.length - 1; i++) {
    
    
            for (int j = 0; j < arr.length - 1 - i; j++) {
    
    
                if (arr[j] > arr[j + 1]) {
    
    
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            System.out.println(Arrays.toString(arr));
            if (!flag) {
    
    
                break;
            } else {
    
    
                flag = false;
            }
        }
        return arr;
    }
}

1: What is the time complexity of bubble sort?

In the best case, the data to be sorted is already in order, and we only need to perform a bubbling operation once to end, so the time complexity in the best case is O(n). The worst case is that the data to be sorted happens to be in reverse order, and we need to perform n bubbling operations, so the worst case time complexity is O(n2).

2: What is the space complexity of bubble sort?

The bubbling process only involves the exchange operation of adjacent data, and only requires constant-level temporary space, so its space complexity is O(1), which is an in-place sorting algorithm.

3: Is bubble sorting a stable sorting algorithm?

In bubble sorting, only exchange can change the order of two elements. In order to ensure the stability of the bubble sorting algorithm, when two adjacent elements are equal in size, we do not exchange, and the same size data will not change the order before and after sorting, so bubble sorting is a stable sorting algorithm.

Guess you like

Origin blog.csdn.net/qq_33626996/article/details/113179064
Recommended