个人手撕代码一、排序相关

 相关链接:

C++十种排序方法(快速排序、冒泡排序等等)_超威蓝猫的哥哥的博客-CSDN博客_c++排序方法有哪几种

六大排序算法:插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序_双鱼211的博客-CSDN博客

以下交换值可以均改用swap,懒得改了

一、冒泡排序

#include 
#include 

using namespace std;

vector vec = {
    3,5,8,7,9,4,14,15,82,37,27,45,66,1
};

void BubbleSort(vector& vec)
{
    int n=vec.size();
    if (n == 0)
        return;
    for (int i = 0; i < n; ++i)
    {
        for (int j = i; j < n; ++j)
        {
            if (vec[i] > vec[j])
            {
                int tmp = vec[j];
                vec[j] = vec[i];
                vec[i] = tmp;
            }
        }
    }
}

void PrintVec(const vector& vec)
{
    for (auto it : vec)
    {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    PrintVec(vec);
    BubbleSort(vec);
    PrintVec(vec);
    std::cout << "Hello World!\n";
}

二、选择排序

每次外循环选择剩余最小排好。

#include 
#include 

using namespace std;

vector vec = {
    3,5,8,7,9,4,14,15,82,37,27,45,66,1
};

void ChooseSort(vector& vec)
{
    int n=vec.size();
    if (n == 0)
        return;
    for (int i = 0; i < n-1; ++i)
    {
        int smallest = i;
        for (int j = i+1; j < n; ++j)
        {
            if (vec[j] < vec[smallest])
            {
                smallest = j;
            }
        }
        int tmp = vec[i];
        vec[i] = vec[smallest];
        vec[smallest] = tmp;
    }
}

void PrintVec(const vector& vec)
{
    for (auto it : vec)
    {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    PrintVec(vec);
    ChooseSort(vec);
    PrintVec(vec);
    std::cout << "Hello World!\n";
}

三、插入排序

十大经典排序算法之插入排序(Insertion Sort)_bfhonor的博客-CSDN博客_插入排序

插入排序详解_云杉木屋的博客-CSDN博客_插入排序

#include 
#include 

using namespace std;

vector vec = {
    3,5,8,7,9,4,14,15,82,37,27,45,66,1
};

void InsertSort(vector& vec)
{
    int n=vec.size();
    if (n == 0)
        return;
    for (int i = 1; i < n; ++i)
    {
        for (int j = i; j >=1; --j)
        {
            if (vec[j-1] >  vec[j])
            {
                int tmp = vec[j-1];
                vec[j-1] = vec[j];
                vec[j] = tmp;
            }
        }

    }
}

void PrintVec(const vector& vec)
{
    for (auto it : vec)
    {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    PrintVec(vec);
    InsertSort(vec);
    PrintVec(vec);
    std::cout << "Hello World!\n";
}

四、希尔排序

排序——希尔排序(Shell's sort)_努力的老周的博客-CSDN博客_希尔排序是把记录按下标

#include 
#include 

using namespace std;

vector vec = {
    3,5,8,7,9,4,14,15,82,37,27,45,66,1
};

void ShellSort(vector& vec)
{
    int n=vec.size();
    if (n == 0)
        return;
    int gap = n;
    while (gap > 1)
    {
        gap = gap / 2;
        //Insert Sort
        //from gap+1 on
        for (int i = gap; i < n; ++i)
        {
            for (int j = i; j >=gap; j -= gap)
            {
                if (vec[j - gap] > vec[j])
                {
                    int tmp = vec[j-gap];
                    vec[j - gap] = vec[j];
                    vec[j] = tmp;
                }
            }
        }
    }
}

void PrintVec(const vector& vec)
{
    for (auto it : vec)
    {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    PrintVec(vec);
    ShellSort(vec);
    PrintVec(vec);
    return 0;
}

五、归并排序

十大经典排序算法-归并排序算法详解_小小学编程的博客-CSDN博客_归并排序算法

#include 
#include 

using namespace std;

vector vec = {
    3,5,8,7,9,4,14,15,82,37,27,45,66,1
};

void Merge(vector& vec, int l,int m, int r)
{
    if (l >= r)
        return;
    int len = r - l + 1;
    vector tmp(len);
    int i = l, j = m + 1;
    int k = 0;
    while ((i <= m) && (j <= r))
    {
        if (vec[i] <= vec[j])
        {
            tmp[k++] = vec[i++];
        }
        else
        {
            tmp[k++] = vec[j++];
        }
    }
    while (i <= m)
    {
        tmp[k++] = vec[i++];
    }
    while (j <= r)
    {
        tmp[k++] = vec[j++];
    }
    for (k = l; k <= r; k++)
    {
        vec[k] = tmp[k-l];
    }
}

void MergeSort(vector& vec,int l,int r)
{
    if (l >= r)
        return;
    int m = (l + r) / 2;
    MergeSort(vec, l, m);
    MergeSort(vec, m+1, r);
    Merge(vec, l, m, r);
}

void PrintVec(const vector& vec)
{
    for (auto it : vec)
    {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    PrintVec(vec);
    MergeSort(vec,0,vec.size()-1);
    PrintVec(vec);
    return 0;
}

六、快速排序

重点!!!必须牢牢掌握并且善于化用

注意:哨兵的选择和先移动左/右的关系

快速排序法(详解)_李小白~的博客-CSDN博客_快速排序

#include 
#include 

using namespace std;

vector vec = {
    3,5,8,7,9,4,14,15,82,37,27,45,66,1
};

void QuickSort(vector& vec, int l, int r)
{
    if (l >= r)
    {
        return;
    }
    int i = l, j = r;
    int pivot = vec[l];
    while (i < j)
    {
        /*
        此处非常重要!!!以最左边为pivot,则需要先移动右边j
        */
        while (vec[j] >= pivot && i < j)
        {
            j--;
        }
        while (vec[i] <= pivot && i < j)
        {
            i++;
        }
        if (i < j)
            swap(vec[i],vec[j]);
    }
    swap(vec[l],vec[i]);
    QuickSort(vec,l,i - 1);
    QuickSort(vec, i + 1, r);
}


void PrintVec(const vector& vec)
{
    for (auto it : vec)
    {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    PrintVec(vec);
    QuickSort(vec, 0, vec.size() - 1);
    PrintVec(vec);
    return 0;
}

七、堆排序

https://blog.csdn.net/weixin_41545534/article/details/115567889?spm=1001.2014.3001.5506

 

堆排序算法(图解详细流程)_阿顾同学的博客-CSDN博客_堆排序的详细过程

#include 
#include 

using namespace std;

vector vec = {
    3,5,8,7,9,4,14,15,82,37,27,45,66,1
};

void Adjust(vector& vec,int end, int idx)
{
    int left_idx = 2 * idx + 1;
    int right_idx = 2 * idx + 2;
    int max_idx=idx;
    if (left_idx < end && vec[left_idx]>vec[max_idx])
        max_idx = left_idx;
    if (right_idx < end && vec[right_idx]>vec[max_idx])
        max_idx = right_idx;
    //if changed
    if (max_idx != idx)
    {
        swap(vec[max_idx],vec[idx]);
        Adjust(vec,end, max_idx);
    }
    
}

void HeapSort(vector& vec)
{
    //Build Heap
    int n = vec.size();
    if (n == 0)
        return;
    for (int i = (n/2-1); i >=0; --i)
    {
        Adjust(vec, n,i);
    }
    //swap and re-adjust 0-i element
    for (int i = n - 1; i >= 1; --i)
    {
        swap(vec[i],vec[0]);
        Adjust(vec,i, 0);
    }
}


void PrintVec(const vector& vec)
{
    for (auto it : vec)
    {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    PrintVec(vec);
    HeapSort(vec);
    PrintVec(vec);
    return 0;
}

你可能感兴趣的:(c++)