Bubble sort, Insertion sort, merge sort, quick sort, heap sort

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
const int n = 80000;

//  bubblesort
void bubble(int * a, int n) {
    for (int i = 0; i < n-1; i++) {
        for (int j = n-1; j > i; j--) {
            if (a[j] < a[j-1]) {
                int temp = a[j];
                a[j] = a[j-1];
                a[j-1] = temp;
            }
        }
    }
}

//  insertionsort
void insert(int *a, int n) {
    for (int i = 1; i < n; i++) {
        int temp = a[i];
        while (i > 0 && a[i-1] > temp) {
            a[i] = a[i-1];
            i--;
        }
        a[i] = temp;
    }
}

// mergesort
void combine(int *a, int p, int q, int r) {
    int i = p, j = q+1;
    int *temp = new int[r - p + 1];
    for (int k = 0; k <= r - p; k++) {
        if (j <= r && (a[i] >= a[j] || i > q)) {
            temp[k] = a[j];
            j++;
        } else {
            temp[k] = a[i];
            i++;
        }
    }
    for (int k = p; k <= r; k++) {
        a[k] = temp[k - p];
    }
    delete temp;
}

void merge(int *a, int p, int q) {
    if (p >= q) return;
    merge(a, p, (p+q)/2);
    merge(a, (p+q)/2+1, q);
    combine(a, p, (p+q)/2, q);
}

//  quicksort
void qsort(int * a, int left, int right) {
    if (left >= right) return;
    int i = left, j = right, k = a[left];
    while (i < j) {
        while (i < j && a[j] >= k) {
            j--;
        }
        a[i] = a[j];
        while (i < j && a[i] <= k) {
            i++;
        }
        a[j] = a[i];
    }
    a[i] = k;
    qsort(a, left, i-1);
    qsort(a, i+1, right);
}

//  heapsort 
#define left(i) (i << 1) + 1
#define right(i) (i + 1) << 1

void max_heapfly(int * a, int i, const int & size) {
    int lagest, l = left(i), r = right(i);
    if (l < size && a[l] > a[i]) {
        lagest = l;
    } else {
        lagest = i;
    }
    if (r < size && a[r] > a[lagest]) {
        lagest = r;
    }
    if (lagest != i) {
        int temp = a[i];
        a[i] = a[lagest];
        a[lagest] = temp;
        max_heapfly(a, lagest, size);
    }
}

void build_max_heap(int * a, int n) {
    for (int i = n/2-1; i >= 0; i--) {
        max_heapfly(a, i, n);
    }
}

void heapsort(int * a, int n) {
    build_max_heap(a, n);
    for (int i = n-1; i > 0; i--) {
        int temp = a[0];
        a[0] = a[i];
        a[i] = temp;
        max_heapfly(a, 0, i);
    }
}

int main() {
    //  test bubble
    if (true) {
        cout << "test bubble\n";
        int a[n];
        srand(time(NULL));
        for (int i = 0; i < n; i++) a[i] = rand();
        clock_t start = clock();
        bubble(a, n);
        clock_t end = clock();
//        for (int i = 0; i < n; i++) cout << a[i] << " ";
        double time = (double)(end - start) / CLOCKS_PER_SEC;
        cout << endl << time << endl << endl;
    }
//      test insert
    if (true) {
        cout << "test insert\n";
        int a[n];
        srand(time(NULL));
        for (int i = 0; i < n; i++) a[i] = rand();
        clock_t start = clock();
        insert(a, n);
        clock_t end = clock();
//        for (int i = 0; i < n; i++) cout << a[i] << " ";
        double time = (double)(end - start) / CLOCKS_PER_SEC;
        cout << endl << time << endl << endl;
    }
    //  test merge
    if (true) {
        cout << "test merge\n";
        int a[n];
        srand(time(NULL));
        for (int i = 0; i < n; i++) a[i] = rand();
        clock_t start = clock();
        merge(a, 0, n-1);
        clock_t end = clock();
//        for (int i = 0; i < n; i++) cout << a[i] << " ";
        double time = (double)(end - start) / CLOCKS_PER_SEC;
        cout << endl << time << endl << endl;
    }
    //  test qsort
    if (true) {
        cout << "test qsort\n";
        int a[n];
        srand(time(NULL));
        for (int i = 0; i < n; i++) a[i] = rand();
        clock_t start = clock();
        qsort(a, 0, n-1);
        clock_t end = clock();
//        for (int i = 0; i < n; i++) cout << a[i] << " ";
        double time = (double)(end - start) / CLOCKS_PER_SEC;
        cout << endl << time << endl << endl;
    }
    //  test heapsort
    if (true) {
        cout << "test heapsort\n";
        int a[n];
        srand(time(NULL));
        for (int i = 0; i < n; i++) a[i] = rand();
        clock_t start = clock();
        heapsort(a, n);
        clock_t end = clock();
//        for (int i = 0; i < n; i++) cout << a[i] << " ";
        double time = (double)(end - start) / CLOCKS_PER_SEC;
        cout << endl << time << endl << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值