用C/C++码经典算法——排序

排序

桶排序

时间复杂度 O(N+M), N为待排序数的个数,M为桶的个数(即数的范围)

空间复杂度 O(M),M为桶的个数(即数的范围)

优点

· 快速

缺点:

· 空间消耗大
· 被排序的数组元素只能是整数
· 这还不是一个真正意义上的桶排序,因为只能对数进行排序,而不涉及数对应的项

代码

//
//  Created by Diane on 7/26/19.
//  Copyright © 2019 Diane. All rights reserved.
//

// Sort an int array, given each element resides in 0~10000
// Bucket Sort

#include <iostream>
using namespace std;

int main()
{
    int bucket[10001],n;
    // init array book with 0s
    for(int i = 0; i < 10001; i++)
        bucket[i]=0;
    // take input
    scanf("%d", &n);
    int tmp;
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &tmp);
        bucket[tmp]++;
    }
    // sort and print
    for(int i = 0; i < 10001; i++)
    {
        while(bucket[i]>0)
        {
            cout << i << ' ';
            bucket[i]--;
        }
    }
    cout << endl;
    return 0;
}

冒泡排序

时间复杂度 O(N^2), N为待排序数的个数

空间复杂度 O(N)

缺点:

· 慢

代码

//
//  Created by Diane on 7/26/19.
//  Copyright © 2019 Diane. All rights reserved.
//

// Sort Students with according to their weight reversely
// Bubble Sort
#include <iostream>
using namespace std;
void bubbleSort(struct student *bubble, int n);
struct student
{
    char name[10];
    int weight;
};

int main()
{
    struct student bubble[100];
    int n;
    // take input
    scanf("%d", &n);
    for(int i = 0; i < n;i++)
    {
        scanf("%s", bubble[i].name);
        scanf("%d", &bubble[i].weight);
    }
    
    bubbleSort(bubble, n);
    
    // print
    for(int i=0; i < n; i++)
    {
        cout << bubble[i].name <<' ' << bubble[i].weight << endl;
    }
    return 0;
    
}

void bubbleSort(struct student *bubble, int n)
{
    struct student tmp;
    for(int i=0; i < n; i++)
    {
        for(int j=0; j < n-i-1; j++)
        {
            if(bubble[j].weight < bubble[j+1].weight)
            {
                tmp = bubble[j+1];
                bubble[j+1] = bubble[j];
                bubble[j] = tmp;
            }
        }
    }
}

快速排序

时间复杂度

· 最差O(N^2), N为待排序数的个数
· 最好、平均O(NlogN)

空间复杂度 O(logN))

优点

· 快速

代码

//
//  Created by Diane on 7/26/19.
//  Copyright © 2019 Diane. All rights reserved.
//

// Sort an int array
// Quick Sort

#include<iostream>
using namespace std;
void quickSort(int a[], int n);

int main()
{
    // take input
    int n, a[100];
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    
    quickSort(a, n);
    
    // output
    for(int i = 0; i < n; i++)
    {
        cout << a[i] << ' ';
    }
    cout << endl;
    
    return 0;
}

void quickSort(int a[], int n)
{
    if(n < 2)
    {
        return;
    }
    int i = 1;
    int j = n-1;
    int tmp;
    while(i < j)
    {
        while(a[j] > a[0] && i < j)
        {
            j--;
        }
        while(a[i] < a[0] && i < j)
        {
            i++;
        }
        tmp = a[j];
        a[j] = a[i];
        a[i] = tmp;
    }
    tmp = a[i];
    a[i] = a[0];
    a[0] = tmp;
    quickSort(&a[0], i);
    quickSort(&a[i+1], n-i-1);
}
原文地址:https://www.cnblogs.com/DianeSoHungry/p/11247956.html