Bubble sort and selection sort

Bubble Sort

  • Compare adjacent elements. If the first is bigger than the second, swap the two of them.
  • Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the largest number.
  • Repeat the above steps for all elements except the last one.
  • Keep repeating the above steps for fewer and fewer elements each time, until there are no pairs of numbers to compare

time complexity

O(n^2)

stability

Bubble sort is to move small elements forward or large elements backward. A comparison is a comparison of two adjacent elements, and a swap also occurs between these two elements. So, if two elements are equal, I think you will not be boring to swap them; if two equal elements are not adjacent, then even if the two are adjacent by the previous pairwise exchange, this The time will not be exchanged, so the order of the same elements does not change, so the bubble sort is a stable sorting algorithm.

accomplish

public static int[] bubbleSort(int[] arr) {
        //长度为n的数组,外循环n-1次
        for(int i = 0; i < arr.length; i++) {
            //长度为n的数组,内循环比外循环小1
            for(int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j+1]) {
                    arr[j] = arr[j] +arr[j+1];
                    arr[j+1] = arr[j] - arr[j+1];
                    arr[j] = arr[j] - arr[j+1];
                }
            }
        }
        return arr;
    }

selection sort

Selection sort is a simple and intuitive sorting algorithm. Its working principle is to select the smallest (or largest) element from the data elements to be sorted each time, and store it at the beginning of the sequence until all the data elements to be sorted are exhausted. Selection sort is an unstable sorting method (for example, the sequence [5, 5, 3] swaps the first [5] with [3] for the first time, causing the first 5 to move behind the second 5).

time complexity

O(n^2)

stability

Selection sort is to select the smallest current element for each position, for example, select the smallest for the first position, select the second smallest for the second element in the remaining elements, and so on, until the n-1th element, the first The n elements do not need to be selected, because only the largest element is left. Then, in a selection, if an element is smaller than the current element, and the smaller element appears after an element equal to the current element, then the stability after the exchange is destroyed. For example, the sequence 5 8 5 2 9, we know that the first selection of the first element 5 will be exchanged with 2, then the relative order of the two 5s in the original sequence will be destroyed, so the selection sort is An unstable sorting algorithm.

accomplish

public static int[] selectionSort(int[] arr) {
        int min;
        //外循环,n-1次
        for(int i = 0; i < arr.length; i++) {
            //每次寻找最小值,交换
            min = i;
            //内循环,循环n-i-1次
            for(int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
            }
            if (arr[min] < arr[i]) {
                arr[min] = arr[min] + arr[i];
                arr[i] = arr[min] - arr[i];
                arr[min] = arr[min] - arr[i];
            }
        }
        return arr;
    }

Bubble sort and selection sort

From the two different implementations above, we can see that bubbling highlights a lift, both lifting the maximum or minimum number we need. The selection sort focuses on exchange, exchanging the largest or smallest number we need with the element of the corresponding index of the sorted array.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325526990&siteId=291194637