Bubble sort & select sort

Bubble Sort

1. Idea: Bubble sort can only operate on two adjacent data. Each bubble sorting will compare two adjacent elements to see if the size relationship requirements are met. If not satisfied, let them swap.

2. Illustration: Bubble sort the array [4, 5, 6, 3, 2, 1], the detailed process of each bubble operation is shown in the figure below:
image.png

It can be seen from the figure that only need array.length-1 bubbling, the array can be arranged.

3. Code implementation

public void bubbleSort(int[] arr) {
    
            
   if (arr == null || arr.length < 2) return;
  
    // 需要进行arr.length - 1次冒泡
   for (int i = 0; i < arr.length - 1; i++) {
    
    
        // 每进行一次冒泡,下一次冒泡需要操作的元素数量减一
        for (int j = 0; j < arr.length - i - 1; j++) {
    
    
            if (arr[j] > arr[j + 1]) {
    
    
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

If the array has been arranged before arr.length-1 bubble, there is no need to bubble sort. We can optimize based on this feature.

Optimized bubble sort

public void bubbleSort(int[] arr) {
    
    
    if (arr == null || arr.length < 2) return;

    for (int i = 0; i < arr.length - 1; i++) {
    
    
    	boolean flag = true;  // 判断在一次冒泡中是否一次交换也没有发生
        for (int j = 0; j < arr.length - i - 1; j++) {
    
    
            if (arr[j] > arr[j + 1]) {
    
    
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                flag = false;  // 发生了交换,将flag设为false
            }
        }
       // 一次冒泡中若一次交换都没有发生,则排序已经完成,可提前结束冒泡排序
        if (flag)  return;  
    }
}

4. Properties:
①Time complexity: O(n^2) ②Space complexity: O(1)    
③Stable sorting ④Stable sorting in place
: If the values ​​of a and b in the original array are the same, a is in b In front, if a is still in front of b after the sorting, it is a stable sorting.
In-place sorting: No extra space is applied for in the sorting process.

Select sort

1. Idea: Each time the unsorted part of the array is traversed, the minimum (maximum) value is selected and exchanged with the first element of the unsorted part.

2. Graphic: Select and sort the array [4, 5, 6, 3, 2, 1]. The detailed process of each exchange is shown in the figure below:

image.png

3. Code implementation:

public void selectSort (int[] arr) {
    
    
    if (arr == null || arr.length < 2) return;
           
    for (int i = 0; i < arr.length - 1; i++) {
    
    
        // 假定最小值的下标为i
        int min = i;   
        for (int j = i + 1; j < arr.length; j++) {
    
    
            if (arr[j] < arr[min]) {
    
    
                // 改变最小值的下标
                min = j;   
            }
        }
        // 交换
        int temp = arr[i];
        arr[i] = arr[min];
        arr[min] = temp;
    }
}

4. Properties:
①Time complexity: O(n^2)
②Space complexity: O(1) ③In-place sorting ④Instable sorting: For the array [4,5,4,6,1], the first time When traversing, the first 4 and the last 1 will be exchanged, and the order of the first 4 and the second 4 will be changed, so it is an unstable sort.

Guess you like

Origin blog.csdn.net/qq_46122005/article/details/112435065