洛谷基础算法

/* 桶排序 */
#include <iostream>
#include <algorithm>

using namespace std;
int a, m, n[1000] = { 0 };
int main()
{
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        cin >>a,  ++n[a];
    }

    for (int j = 0; j < 1000; j++)
    {
        while (n[j]--)
        {
            cout << j << " ";
        }
        
    }

    return 0;
}
/* 二分排序*/
#include <iostream>
using namespace std;

void sort_(int l, int m);

int n, a[10000];
int main()
{
    cin >> n;
    
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    sort_(1, n);

    for (int j = 1; j <= n; j++)
    {
        cout<<a[j]<<" ";
    }

    return 0;
}

void sort_(int l, int m)
{
    int mid = a[(l + m) / 2];
    int i = l, j = m;
    do {
        while (a[i] < mid) i++;
        while (a[j] > mid) j--;
        if (i <= j)
        {
            swap(a[i], a[j]);
            i++;
            j--;
        }
    } while (i <= j);
    if (l < j) sort_(l, j);
    if (i < m) sort_(i, m);
}

 一些递归递推示例: https://blog.csdn.net/weixin_39721214/article/details/103833530

posted @ 2020-08-19 11:19  strive-sun  阅读(347)  评论(0编辑  收藏  举报