Sorting algorithm exchange sorting (bubble sorting and quick sorting)

The core idea of ​​swap sorting is: according to the comparison result of the key values ​​of two records in the sequence, it is judged whether it is necessary to swap the positions of the records in the sorting. It is characterized by moving records with larger (or smaller) key values ​​to the front end of the sequence, and moving records with smaller (or larger) key values ​​to the back end of the sequence.

Basic sorting algorithm implementation:

    void Sort (int arr[],int n)
    {
        int i,j;
        int k;
        for(i=0;i<n-1;i++)  //n-1轮循环
        {
            for(j=i+1;j<n;j++)
            {
                if(arr[i]<arr[j])   //比较判断
                {
                    swap(&arr[i],&arr[j]);  //位置交换
                }
            }
        }
    }

Bubble Sort

The basic principle of bubble sorting is: compare the keywords of two adjacent records, so that the records that do not meet the sequence requirements are exchanged until the end of the n-1 cycle. The bubble sort algorithm is stable.

Bubble sort animation demo:

 The first round of sorting, pay attention to observation, you can see pairwise comparison, the small one moves to the right, and the big one moves to the left. At the end of the first round of sorting, you can see that "0" has been sorted, and it has been ranked on the far right. Corresponding to the position, the next few rounds of sorting "0" will no longer move.


In the second round, it can be seen that the position of "1" has been determined again, and in the next few rounds of sorting, "1" will not move again. 


 By analogy, the third round puts "2" in the corresponding position, and the sorting behind "2" will not move anymore. There will be a fourth round, a fifth round, and until the n-1th round, the sorting will end.

The effect of sorting is completed:


quick sort

Quick sorting is an improvement on bubble sorting. The basic idea of ​​quick sorting is: divide the data in the sequence into two parts by one-pass sorting, and all the values ​​in one part are smaller than those in the other part; method, perform quick sorting on the two parts of the data, until the two parts involved in the sorting are in order.

At the beginning, you need to set a reference value (usually select the key value of the first record in the sequence as a parameter), divide the data by comparing with the reference value, and divide the sequence into the above two parts. Quicksort is an unstable sorting algorithm.

Give an example of ascending order

 The first-level sorting key (reference value) key=5, the first-level quick sorting process is as follows:

First, regard the head and tail positions of the sequence as both "i" and "j". The two sides are like playing checkers. "i" moves based on i++ until the value of the position is greater than the key value and stops ++, and "j" moves According to j-- the value of the position is less than the key value, stop "j"--, when one party stops, replace the other party to carry out the operation of moving until the two parties move to the middle position "i" = "j" means to use this The sorting of layer key value division ends.

In the first round, "i" is operated first, and the value 5 at the position of "i" is the reference value, so let the position of the reference value be regarded as a null value first, and "i" ends the operation, and the operation is changed to "j".

In the second round, "j" operation, the value 5 at the "j" position is compared with the key value 5, and they are equal and cannot be moved; so "j" moves forward one space to the position with a value of 7 (as shown in the figure below), and uses the value 7 is compared with the key value 5, and it is found that 7 is greater than 5, and it cannot be moved; so "j" moves forward one more space to the position with a value of 3 (as shown in the figure below), and compares the value 3 with the key value 5, and finds 3 It is less than 5, so move the value 3 to the empty value of "i" (as shown in the figure below), and the operation ends when "j" is successfully moved, and the operation is changed to "i".

 The third round, "i" operation, "i" advances one space to the position with a value of 0 (as shown in the figure below), compares with 0 and key value 5, and finds that 0 is less than 5, and does not move; so "i" advances one space , go to the position with value 9 (as shown in the figure below), compare 9 with key value 5, and 9 is greater than 5, so move 9 to the position of "j" empty value (as shown in the figure below), "i" successfully moves to end the operation, change "j" to operate.

 The fourth round, "j" operation, "j" advances one space to the position with the value 6 (as shown in the figure below), compares the key value with 6, if 6 is greater than 5, do not move; continue to advance one space until the value is 4 position (as shown in the figure below), use 4 to compare with the key value, if 4 is less than 5, move 4 to the position of the empty value of "i" (as shown in the figure below); if "j" is successfully moved, change to "i" operation.

 The fifth round, "i" operation, "i" advances one space, to the position of value 8 (as shown in the figure below), compares 8 with key value 5, 8 is greater than 5, so move 8 to the empty value of "j" Position (as shown in the figure below), "i" ends the operation, change to "j" operation.

 The sixth round, "j" operation, "j" advances one space, to the position with value 1 (as shown in the figure below), compares 1 with key value 5, 1 is less than 5, so move 1 to the empty value of "i" Go to the position (as shown in the figure below), the "j" operation is over, and the "i" operation is changed.

 In the seventh round, "i" operation, advance one grid, and found that "i" and "j" overlap (as shown in the figure below), before starting to play checkers, we stipulated that when i=j, it means that the sorting of this layer is over. Next, put the key value in the overlapping position, and the end sorting result of this layer is (as shown in the figure below) 3041586975.

 From the above figure, it can be seen intuitively that the left side of the key value 5 is not greater than 5, and the right side is not less than 5. Successfully divided into two parts.


The next two parts are sorted internally through recursive calls. The sorting is also the same as the checkers example above. The value of the first position is set as the key value, the "i" position is empty, "j" is operated first, and "i" Alternate operations in turn until the two overlap. After each layer of recursive operation, the position changes as shown in the figure below.

The second layer is sorted as shown in the figure below:

 The three-level sorting is as shown in the figure below, directly excluding 4 and 9, which are part of the single number generated after the three-level sorting, and there is no need for sorting:

 The four layers are sorted as shown in the figure below:

 The final sorting result is:


If it is helpful to you, please leave a small footprint before leaving!

 

Guess you like

Origin blog.csdn.net/m0_61843874/article/details/124898777