码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法(C++实现)

时间:2016-05-07 08:29:17      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
using namespace std;

template <class T>
void Swap(T & left, T & right) {
    T temp = left;
    left = right;
    right = temp;
}

/* 简单选择排序 */
template <class T>
void SelectSort(T * arr, int n) {
    for (int i = 0; i < n - 1; ++i) {
        int small = i;
        for (int j = i + 1; j < n; ++j)
           if (arr[j] < arr[small])
               small = j;
        Swap(arr[i], arr[small]);
    }
}

/* 直接插入排序 */
template <class T>
void InsertSort(T * arr, int n) {
    for (int i = 1; i < n; ++i) {
        T val = arr[i];
        int j = i;
        while (j > 0 && val < arr[j - 1]) {
            arr[j] = arr[j - 1];
            --j;
        }
        arr[j] = val;
    }
}

/* 冒泡排序 */
template <class T>
void BubbleSort(T * arr, int n) {
    int i = n - 1;
    while (i > 0) {
        int last = 0;
        for (int j = 0; j < i; ++j) {
            if (arr[j] > arr[j + 1]) {
                Swap(arr[j], arr[j + 1]);
                last = j;
            }
        }
        i = last;
    }
}

/* 快速排序 */
template <class T>
void qSort(T * arr, int left, int right) {
    if (left < right) {
        int i = left, j = right + 1;
        do {
            do ++i; while (arr[i] < arr[left]);
            do --j; while (arr[j] > arr[left]);
            if (i < j)
                Swap(arr[i], arr[j]);
        } while (i < j);
        Swap(arr[left], arr[j]);
        qSort(arr, left, j - 1);
        qSort(arr, j + 1, right);
    }
}

template <class T>
void QuickSort(T * arr, int n) {
    qSort(arr, 0, n - 1);
}

/* 两路合并排序 */
template <class T>
void Merge(T * arr, int ls, int le, int rs, int re) {
    T * temp = new T[re - ls + 1];
    int pl = ls, pr = rs, pt = 0;
    while (pl <= le && pr <= re) {
        if (arr[pl] <= arr[pr])
            temp[pt++] = arr[pl++];
        else
            temp[pt++] = arr[pr++];
    }
    while (pl <= le)
        temp[pt++] = arr[pl++];
    while (pr <= re)
        temp[pt++] = arr[pr++];
    for (int i = 0; i < pt; ++i)
        arr[ls + i] = temp[i];
    delete [] temp;
}

template <class T>
void MergeSort(T * arr, int n) {
    int ls, le, rs, re;
    int size = 1;
    while (size < n) {
        ls = 0;
        while (ls + size < n) {
            le = ls + size - 1;
            rs = ls + size;
            if (rs + size <= n)
                re = rs + size - 1;
            else
                re = n - 1;
            Merge(arr, ls, le, rs, re);
            ls = re + 1;
        }
        size *= 2;
    }
}

/* 堆排序 */
template <class T>
void AdjustDown(T * arr, int start, int end) {
    int child = 2 * start + 1;
    int val = arr[start];
    while (child <= end) {
        if (child < end && arr[child] < arr[child + 1])
            ++child;
        if (val >= arr[child])
            break;
        arr[(child - 1) / 2] = arr[child];
        child = 2 * child + 1;
    }
    arr[(child - 1) / 2] = val;
}

template <class T>
void HeapSort(T * arr, int n) {
    for (int i = (n - 2) / 2; i >= 0; --i)
        AdjustDown(arr, i, n - 1);
    for (int i = n - 1; i > 0; --i) {
        Swap(arr[0], arr[i]);
        AdjustDown(arr, 0, i - 1);
    }
}

int main() {
    int arr[10] = {5, 4, 6, 5, 2, 5, 7, 5, 5, 1};
    int b = 0;
    int c = 1;
    QuickSort(arr, 10);
    for (int i = 0; i < 10; ++i)
        cout << arr[i] << " ";
    cout << endl;
}


排序算法(C++实现)

标签:

原文地址:http://blog.csdn.net/optcaelum/article/details/51335493

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!