排序算法

一、插入排序—直接插入排序(Straight Insertion Sort)
時間複雜度:O(n^2).

#include <iostream>
using namespace std;

const int MAX_SIZE = 10;

template<class T>
void insert(T a[], int n);

int main(int argc, char** argv) {
    int s[10] = {9,6,4,8,5,3,1,0,3,2};
    insert(s, MAX_SIZE);

    for(int i = 0; i < MAX_SIZE; i++)
        cout<< s[i] << " ";
    cout<<endl;
    return 0;
}

template<class T>
void insert(T a[], int n)
{
    //對數組a[0:n-1]進行插入排序 
    for(int i = 1; i < n; i++)
    {
        //把a[i]插入到a[0:i-1] 
        T t = a[i];
        int j;
        for(j = i-1; j >= 0 && t < a[j]; j--)
            a[j+1] = a[j];
        a[j+1] = t;
    }
}

選擇排序
首先找出最大元素將其放入a[n-1],然後在剩下的元素中找出最大的元素放入a[n-2].
如此進行下去。直到剩下下一個元素。

及時終止:
在程序中加入bool值state可判斷元素序列是否
已經有序。如果有序則停止,節約時間。

#include<iostream>
using namespace std;

const int MAX_SIZE = 10;

template<class T>
void selectionSort(T a[], int n);

int main()
{
    int s[10] = {9,6,4,8,5,3,1,0,3,2};
    selectionSort(s, MAX_SIZE);

    for(int i = 0; i < MAX_SIZE; i++)
        cout<< s[i] << " ";
    cout<<endl;

    return 0;
}

template<class T>
void selectionSort(T a[], int n)
{
    //及時終止的選擇排序
    bool state = false;
    for(int i = n; !state && (i > 1); i--)
    {
        int MaxValue = 0;
        state = true;
        //查找最大元素
        for(int j = 1; j < i; j++)
            if(a[MaxValue] <= a[j])
                MaxValue = j;
            else
                state = false;  //表明無序
            swap(a[MaxValue], a[i-1]);
    }
}

冒泡排序:
每當兩相鄰的數比較後發現它們的排序與排序要求相反時,就將它們互換。

及時終止:
在程序中加入bool值state可判斷元素序列是否
已經有序。如果有序則停止,節約時間。

#include<iostream>
using namespace std;

const int MAX_SIZE = 10;

template<class T>
void bubbleSort(T a[], int n);

int main()
{
    int s[10] = {9,6,4,8,5,3,1,0,3,2};
    bubbleSort(s, MAX_SIZE);

    for(int i = 0; i < MAX_SIZE; i++)
        cout<< s[i] << " ";
    cout<<endl;

    return 0;
}

template<class T>
void bubbleSort(T a[], int n)
{
    bool state = false;
    for(int i = n; !state && (i > 0); i--)
    {
        state = true;
        //把a[0:i]中最大元素移動到最右端 
        for(int j = 1; j < i; j++)
            if(a[j] <= a[j-1])
            {
                swap(a[j], a[j-1]);
                state = false;
            }
    }
}

名次排序
名次計算(ranking):
一個元素在序列中的名詞(rank)是所有比他小的元素個數加上
在他左邊出現相同元素的個數之和,a[5]={4,3,9,3,7},個元素的
次詞爲r[5]={2,0,4,1,3};

#include<iostream>
using namespace std;

const int MAX_SIZE = 20;
template<class T>
void rank(T a[], int n, int r[]);

template<class T>
void ranksort(T a[], int n, int r[]);

int main()
{
    int a[MAX_SIZE] = {5,9,3,7,4,3,7,9,1,6,8,1,3,4,8,5,2,1,4,5};
    int r[MAX_SIZE];

    rank(a, MAX_SIZE, r);
    ranksort(a, MAX_SIZE, r);

    for(int i = 0; i < MAX_SIZE; i++)
        cout<< a[i] << " ";
    cout<<endl;

    return 0;
}

template<class T>
void rank(T a[], int n, int r[])
{
    for(int i = 0; i < n; i++)
        r[i] = 0;

    //比較所有元素對
    for(i =1; i < n; i++)
        for(int j = 0; j < i; j++)
        {
            if(a[j] <= a[i])
                r[i]++;
            else
                r[j]++;
        }
}

template<class T>
void ranksort(T a[], int n, int r[])
{
    T *u = new T [n];   //創建附加數組

    //把a中元素移到u中的正確位置
    for(int i = 0; i < n; i++)
        u[r[i]] = a[i];

    //u中元素移回a
    for(i = 0; i < n; i++)
        a[i] = u[i];

    delete u;
}
發佈了47 篇原創文章 · 獲贊 24 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章