Sorting Algorithms - Bubble Sort and Selection Sort

Continue to create, accelerate growth! This is the first day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event

Sorting Algorithm

1. Basic introduction

The sorting algorithm is relatively basic, but it is designed with many computer science ideas, as follows:

  1. Comparison and Non-Comparison Strategies
  2. Implementation of iteration and recursion
  3. the idea of ​​divide and conquer
  4. Best, worst, average case time complexity analysis
  5. random algorithm

2. Classification of sorting algorithms

Algorithm classification

1.jpg

Algorithm Summary

2.jpg

3. Bubble sort

(1) Introduction to Bubble Sort

Bubble sort is a simple sorting algorithm. It repeatedly walks through the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until no more exchanges are needed, that is, the sequence has been sorted. The name of this algorithm comes from the fact that the smaller elements are slowly "floated" to the top of the sequence by swapping.

(2) The principle of bubble sort:

  1. If the element size relationship is incorrect, swap the two numbers (a > b in this case),

  2. compare a pair of adjacent elements (a, b),

  3. Repeat steps 1 and 2 until we reach the end of the array (the last pair is the (N-2) and (N-1)th item since our array starts at zero)

  4. By far the largest element will be in the last position. Then we decrease N by 1 and repeat step 1 until N = 1.

(3) Motion picture demonstration

3.jpg

(4) Code demonstration

public static void bubbleSort(int array[]) {
        int t = 0;
        for (int i = 0; i < array.length - 1; i++){
            for (int j = 0; j < array.length - 1 - i; j++){
                if (array[j] > array[j + 1]) {
                    t = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = t;
                }
            }
        }
    }
复制代码

4. Selection sort

(1) Introduction to selection sort

Selection sort (Selection-sort) is a simple and intuitive sorting algorithm. How it works: first find the smallest (largest) element in the unsorted sequence, store it at the beginning of the sorted sequence, then continue to find the smallest (largest) element from the remaining unsorted elements, and put it in the sorted sequence end of . And so on until all elements are sorted.

(2) The principle of selection sort

  1. Find the position of the smallest item X in the range [L...N-1] ,

  2. Swap X with the Lth term,

  3. Increase the lower limit L by 1 and repeat step 1 until L = N-2 .

(3) Dynamic graph demonstration

4.jpg

(4) Code demonstration

public static void selectionSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int index = i;
            for (int j = i; j < array.length; j++) {
                if (array[j] < array[index]) 
                    index = j; 
            }
            int temp = array[index];
            array[index] = array[i];
            array[i] = temp;
        }
    }
复制代码

Guess you like

Origin juejin.im/post/7101502080984023047