Summarize the classic sorting algorithm

Sorting Algorithm

As an introduction to algorithms, you must master the sorting algorithm. I am just starting to brush the algorithm. Let me start to describe my algorithm history from here.

冒泡排序

Complexity O(n2)

  • Algorithm description
    1. The outer loop 0-n means that n elements are to be matched once.
    2. The inner j = n-1-jis to match one element and reduce the elements to be compared by one each time, that is, only compare the first n-1-jone of the unsorted sequence element.
    3. In each comparison, adjacent elements are compared, and the matched elements are "sinked" to the end, so you don't need to worry about it.
  • Dynamic presentation
    Insert picture description here
  • Code
#include <iostream>
using namespace std;

void swap(int *a, int *b){
    
    
        int temp = *a;
        *a = *b;
        *b = temp;
}
int main(int argc, char** argv){
    
    

        int             len;
        cout << "please input arr lenth:";
        cin >> len;
        int arr[len];
        for(int i=0; i<len; i++){
    
    
                cout << "please input" << i << "num:";
                cin >> arr[i];
        }
		for(int i=0; i<len; i++){
    
    
                for(int j=0; j<len-i-1; j++){
    
    
                        if(arr[j] > arr[j+1]){
    
    
                                swap(&arr[j],&arr[j+1]);
                        }
                }
        }

        for(int i=0; i<len; i++){
    
    
                cout << arr[i] << " ";
        }
        cout << "\n";
        return 0;
}

快速排序

The complexity of O(nlog2n) quick sort is like this.
First of all, from a set of data such as "3 2 3 5 1 9 6", the quick sort is divided into the following steps to complete.

  • 1. Find a base number, here is the first arr[0] (that is, 3).
  • 2. For comparison, change the number larger than 3 to the left of 3, and the number smaller than 3 to the right of 3.
  • 3. Then change the position of the value at the time of encounter with the reference number.
  • 4. Then use recursion to sort the left and right sides respectively

Note: The right side here must be moved first, because the reference number is the left side of the selection. If the leftmost number arr[left] is selected as the reference number, then starting from the right side can ensure that when i and j meet, the number of encounters is less than For the base number, the left side of the position of temp after the exchange is less than temp. But starting from the left, the number of encounters is greater than the reference number, and it cannot be satisfied that the number on the left of temp is less than it. So to scan, start from the opposite side of the reference number. for example:

初始数列:3 2 3 5 1 9 6
变换后数列:3 2 3 1 5 9 6(到这里只有15换了位置)
我们可以看到上面一步将15交换了位置,这时侯左边哨兵指向1,右边哨兵指向5,如果左边哨兵先走,
那么他就指向了5,然后停下来,右边哨兵也因为条件不满足停在了5这里,然后让35交换。这样逻辑就错误了,所以只能是右边先走。
  • Code
#include <iostream>
using namespace std;

//快速排序(从小到大)
void quickSort(int left, int right, int * arr)
{
    
    
        if(left >= right)
                return;
        int i, j, base, temp;
        i = left, j = right;
        base = arr[left];  //取最左边的数为基准数
        while (i < j)
        {
    
    
                while (arr[j] >= base && i < j)
                        j--;
                while (arr[i] <= base && i < j)
                        i++;
                if(i < j)
                {
    
    
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                }
        }
        //基准数归位
        arr[left] = arr[i];
        arr[i] = base;
        quickSort(left, i - 1, arr);//递归左边
        quickSort(i + 1, right, arr);//递归右边
}

int main(int argc, char** agrv){
    
    
        int             len;
        cout << "please input lenth:";
        cin >> len;
        int arr[len];
        for(int i=0; i<len; i++){
    
    
                cout << "please input num:";
                cin >> arr[i];
        }
        quickSort(0,len-1,arr);
        for(int i=0; i<len; i++){
    
    
                cout << arr[i] << " ";
        }
        cout << "\n";
        return 0;
}

插入排序

The complexity of O(n2)
insertion sort is to compare with the previous one from the first one, because the zeroth one is already treated as an ordered array. Then start from the first one and compare with the front, and then move backward.
Take [1, 5, 4, 3, 4, 5, 6, 8] as an example:
key = arr[i]
Insert picture description here
First, j is determined to be in a good order, and then start from i to compare with the previous one, i>j Therefore, 5 remains unchanged.
Insert picture description here
The second round is here, and then 5>4, so the operation performed is arr[j+1] = arr[j], that is, 5 is shifted to 4, and then 4 is compared with the previous 1.
Insert picture description here
Then it’s like this, then compare 1 with 4, 1<4, so no operation.
Insert picture description here
Then i and j point to 3 and 5 respectively
Insert picture description here
. For the first comparison , 5>3, so 5 is moved back

 while(j>=0 && arr[j]> key){
    
    
                        arr[j+1] = arr[j];
                        j--;
                }

Insert picture description here
When comparing 4 and 3, 4>3, so 4 is shifted back

 while(j>=0 && arr[j]> key){
    
    
                        arr[j+1] = arr[j];
                        j--;
                }

Insert picture description here

Insert picture description here

Then repeat the above steps.

  • Code
#include <iostream>

using namespace std;



void Insert_Sort(int *arr, int len){
    
    
        int j,key;
        for(int i=1; i<len; i++){
    
    
                key = arr[i];
                j = i-1;
                while(j>=0 && arr[j]> key){
    
    
                        arr[j+1] = arr[j];
                        //swap(&arr[j+1], &arr[i]);
                        j--;
                }
                arr[j+1] = key;
                //swap(&arr[j+1], &key);
        }
}

int main(){
    
    
        int             len;
        cout << "please input len:";
        cin >> len;
        int arr[len];
        for(int i=0; i<len;i++){
    
    
                cout << "please input num:";
                cin >> arr[i];
        }
        Insert_Sort(arr,len);
        for(int i=0; i<len; i++){
    
    
                cout << arr[i] << " " ;
        }
        cout << "\n";
        return 0;
}

Guess you like

Origin blog.csdn.net/qq_45125250/article/details/110152159