[Data structure] Basics: six sorting algorithms (simple insertion sort, hill sort, bubble sort, quick sort, simple selection sort, heap sort)

I. Introduction

The data structure has only five test points: linked list (including stack and queue), binary tree, graph, search, and
sorting. Three sorting test points: comparison of all sorting algorithms + principles of each sorting algorithm + characteristics of each sorting algorithm

Two, simple insertion sort

2.1 demo + running result

#include <iostream>

using namespace std;
void InsertSort(int r[],int n)   //0号单元为暂存单元和监视哨    
{
    
    
    int j=0;
    for(int i=2; i<=n; i++)
    {
    
    
        r[0]=r[i];                     //用于暂存待排序元素  
        for ( j=i-1; r[0]<r[j]; j--)    //寻找合适的插入位置
            r[j+1]=r[j];                  //移动
        r[j+1]=r[0];
    }

}
int main()
{
    
    
    int a[5];
    for (int i=0; i<5; i++)
        cin>>a[i];
    cout<<endl;
    InsertSort(a,5);
    for (int i=1; i<5; i++)
    {
    
    
        cout<<a[i];
        cout<<" ";
    }
    cout<<endl;
    return 0;
}

operation result:

​​​​Insert picture description here

2.2 Features: Simple insertion sort

Simple insertion sort:
1. The time complexity is O(n^2), because of two loops;
2. The space complexity is O(1), one temporary sentinel;
3. Only adjacent moves, no jitter, is one A stable sorting;
4. It is suitable for the situation where the sequence is basically ordered.

Three, Hill sort

Summary: Hill sort, an advanced sorting method, can be considered as an upgraded version of simple insertion sort

3.1 Demo + running result

#include <iostream>
 
using namespace std;
void shellSort(int r[],int n)
{
    
    
    for (int d=n/2; d>=1; d=d/2)
    {
    
    
 
        for (int i=d+1; i<=n; i++) //d为增量
        {
    
    
 
            r[0]=r[i];    //0号元素用于暂存  没有意义
            int j=0;
            for ( j=i-d; j>0&&r[0]<r[j]; j=j-d)
                r[j+d]=r[j];           //记录每次移动d个位置,跳跃移动
            r[j+d]=r[0];
        }
    }
}
int main()
{
    
    
    int a[6];
    for (int i=0; i<6; i++)
        cin>>a[i];
    cout<<endl;
    shellSort(a,6);
    for (int i=1; i<6; i++)
    {
    
    
        cout<<a[i];
        cout<<" ";
    }
 
    cout<<endl;
    return 0;
}

operation result:

Insert picture description here

3.2 Features: Hill sort

Hill sorting:
1. The time complexity is O(nlgn)~O(n^2), which is the result of a large number of repeated experiments;
2. The space complexity is O(1), a temporary sentinel;
3. There is a jitter Movement is an unstable sort.

Fourth, bubble sort

Summary: Bubble sort, a low-level sort, easy to understand

4.1 Demo + running result

#include <iostream>
 
using namespace std;
void bubbleSort(int r[],int n)
{
    
    
    int exchange=n;
    while(exchange!=0)
    {
    
    
 
        int  bound=exchange;
        exchange=0;
        for (int j=1; j<bound; j++)
        {
    
    
            if(r[j]>r[j+1])
            {
    
    
                r[0]=r[j];         //0号元素用于交换暂存
                r[j]=r[j+1];
                r[j+1]=r[0];
                exchange=j;
            }
        }
    }
}
int main()
{
    
    
    int a[6];
    for (int i=0; i<6; i++)
        cin>>a[i];
    cout<<endl;
    bubbleSort(a,6);
    for (int i=1; i<6; i++)
    {
    
    
        cout<<a[i];
        cout<<" ";
    }
 
    cout<<endl;
    return 0;
}

operation result:

Insert picture description here

4.2 Features: Bubble sort

Bubble sorting:
1. The time complexity is O(n^2), because of two loops;
2. The space complexity is O(1), one temporary sentinel;
3. There are only adjacent moves, no jitter, which is one A stable sort.

Five, quick sort (interview focus)

Summary: Quick sort, an advanced sort, can be seen as an upgraded version of bubble sort

5.1 Demo + running result

#include <iostream>
 
using namespace std;
int Partition(int r[],int first,int _end)
{
    
    
 
    int i=first;int j=_end;
    while(i<j)
    {
    
    
 
        while(i<j && r[i]<=r[j])  j--;
        if(i<j)
        {
    
    
 
            int temp=r[i];
            r[i]=r[j];
            r[j]=temp;
            i++;
        }
        while(i<j && r[i]<=r[j]) i++;
        if(i<j)
        {
    
    
 
            int temp=r[i];
            r[i]=r[j];
            r[j]=temp;
            j--;
        }
    }
    return i;
}
void QuickSort(int r[],int first,int _end)
{
    
    
    if(first<_end)
    {
    
    
        int pivot=Partition(r,first,_end);
        QuickSort(r,first,pivot-1);
        QuickSort(r,pivot+1,_end);
    }
}
int main()
{
    
    
    int a[6];
    for (int i=0; i<6; i++)
        cin>>a[i];
    cout<<endl;
    QuickSort(a,0,5);
    for (int i=0; i<6; i++)
    {
    
    
        cout<<a[i];
        cout<<" ";
    }
 
    cout<<endl;
    return 0;
}

operation result:

Insert picture description here

5.2 Features: Quick Sort

Quick sort:
1. The time complexity is O(nlgn);
2. The space complexity is O(1), an exchange variable (you can omit this variable when you make a reasonable use of addition and subtraction during exchange);
3. There is jitter, which is not Stable ranking;

Six, simple selection sort

Summary: Simple selection sorting, a low-level sorting method, easy to understand

6.1 Demo + running result

#include <iostream>
 
using namespace std;
void selectSort(int r[],int n)
{
    
    
    for (int i=1; i<n; i++)
    {
    
    
 
        int index=i;
        int j=0;
        for (j=i+1; j<=n; j++)
        {
    
    
            if(r[j]<r[index])
            {
    
    
                r[0]=r[j];           //0号元素用于暂存玩车个交换
                //  其实可以不用这样,使用加减法可以不需要暂存元素,这里方便读者理解,用简单的方式
                r[j]=r[index];
                r[index]=r[0];
            }
        }
        if(index!=i)
        {
    
    
 
            r[0]=r[i];
            r[i]=r[index];
            r[index]=r[0];
        }
    }
}
int main()
{
    
    
    int a[6];
    for (int i=0; i<6; i++)
        cin>>a[i];
    cout<<endl;
    selectSort(a,6);
    for (int i=1; i<6; i++)
    {
    
    
        cout<<a[i];
        cout<<" ";
    }
 
    cout<<endl;
    return 0;
}

operation result:

Insert picture description here

6.2 Features: Simple selection and sorting

Simple selection sorting:
1. The time complexity is O(n^2) because of two loops;
2. The space complexity is O(1), and a temporary element is used to realize the exchange (not necessary, you can add two numbers) Subtraction realizes the exchange of two numbers);
3. There is element bounce, which is an unstable sort;
4. There is no preference for application, because the best and worst average time complexity is O(n^2)

Seven, heap sort

Summary: Heap sorting, an advanced sorting method, can be seen as an upgraded version of simple selection sorting

7.1 Demo + running result

#include <iostream>
 
using namespace std;
void shift(int r[],int k,int m)
{
    
    
 
    int  i=k,j=2*i;   // i 为某个结点  2*i为该结点的左孩子
    while(j<=m)         //  其左孩子没到结束  说明该结点还是非叶子结点,因为叶子结点是没有左孩子的
    {
    
    
 
        if(j<m && r[j]<r[j+1]  )  j++;  //j和j+1  比较左右孩子,j指向较大者
        if(r[i]>r[j])  break;    //当根结点大于左右孩子中较大者,跳出本次循环
        else                //否则,交换根节点和左右孩子较大者
        {
    
    
            r[0]=r[j];   //0号元素用于交换暂存
            r[j]=r[i];
            r[i]=r[0];
            i=j;
            j=2*i;
        }
    }
}
 
void heapSort(int r[],int n)
{
    
    
    for (int i=n/2; i>=1; i--)
        shift(r,i,n);
    for (int i=1; i<n; i++)
    {
    
    
        r[0]=r[1];    //0号元素用于交换暂存
        r[1]=r[n-i+1];
        r[n-i+1]=r[0];
        shift(r,1,n-i);
    }
}
int main()
{
    
    
    int a[6];
    for (int i=0; i<6; i++)
        cin>>a[i];
    cout<<endl;
    heapSort(a,6);
    for (int i=1; i<6; i++)
    {
    
    
        cout<<a[i];
        cout<<" ";
    }
 
    cout<<endl;
    return 0;
}

operation result:

Insert picture description here

7.2 Features: Heap Sort

Heap sorting:
1. The time complexity is O(nlgn) because of two cycles;
2. The space complexity is O(1), and a temporary sentinel;
3. There is a jitter, which is an unstable sort;
4. Suitable for selecting the smallest first few elements and the largest first few elements

8. Interview cheat sheet (principle of sorting algorithm + characteristics of sorting algorithm)

The data structure has only five test points: linked list (including stack and queue), binary tree, graph, search, and
sorting. Three sorting test points: comparison of all sorting algorithms + principles of each sorting algorithm + characteristics of each sorting algorithm

The characteristics of each sorting algorithm are listed first, and the principle of the sorting algorithm will be discussed later.

Simple insertion sort:
1. The time complexity is O(n^2), because of two loops;
2. The space complexity is O(1), one temporary sentinel;
3. Only adjacent moves, no jitter, is one A stable sorting;
4. It is suitable for the situation where the sequence is basically ordered.
Hill sorting:
1. The time complexity is O(nlgn)~O(n^2), which is the result of a large number of repeated experiments;
2. The space complexity is O(1), a temporary sentinel;
3. There is a jitter Movement is an unstable sort.

Bubble sorting:
1. The time complexity is O(n^2), because of two loops;
2. The space complexity is O(1), one temporary sentinel;
3. Only adjacent moves, no beating, is one A stable sort.
Quick sort:
1. The time complexity is O(nlgn);
2. The space complexity is O(1), an exchange variable (you can omit this variable when you make a reasonable use of addition and subtraction during exchange);
3. There is jitter, which is not Stable ranking;

Simple selection sorting:
1. The time complexity is O(n^2) because of two loops;
2. The space complexity is O(1), and a temporary element is used to realize the exchange (not necessary, you can add two numbers) Subtraction realizes the exchange of two numbers);
3. There is element bounce, which is an unstable sort;
4. There is no preference for application, because the best and worst average time complexity is O(n^2)
heap sort:
1 , The time complexity is O(nlgn), because of two cycles;
2. The space complexity is O(1), and a temporary sentinel;
3. There is a jitter, which is an unstable sort;
4. Suitable for selection The smallest first few elements and the largest first few elements

Nine, summary

Six sorting algorithms (simple insertion sort, hill sort, bubble sort, quick sort, simple selection sort, heap sort) are completed.

Code every day, make progress every day! ! !

Guess you like

Origin blog.csdn.net/qq_36963950/article/details/108953996