【重拾算法】三種排序:冒泡、插入、選擇

正文之前

最近在實驗室除了看論文貌似沒啥事情,但是老板論文都不給我推薦,所以我干脆自己再來找點東西看好了。。剛好前幾天在珞櫻網(wǎng)上找到了一份慕課的收費視頻的錄制版。。下下來好好看一下好了~

正文

//########### Compare.cpp ###########

#include <iostream>
#include "Student.h"
#include "SortTestHelper.h"
#include <algorithm>
#include <time.h>
using namespace std;


template<typename T> 
void insertSort(T arr[], int n){
    for (int i = 1; i < n; ++i)
    {
        T e = arr[i];
        int j=i;
        for (; j > 0; --j)
        {
            if (e < arr[j-1])
                arr[j] = arr[j-1];
            else
                break;
        }
        arr[j] = e;
    }
}

template<typename T> 
void selectSort(T arr[], int n){

    for(int i = 0 ; i < n ; i ++){

        int minIndex = i;
        for( int j = i + 1 ; j < n ; j ++ )
            if( arr[j] < arr[minIndex] )
                minIndex = j;
        swap( arr[i] , arr[minIndex] );
    }
}


template<typename T> 
void bubbleSort(T arr[], int n){
    for (int i = n-1; i > 0; --i)
    {
        for (int j = 0; j < i; ++j)
        {
            if (arr[j] > arr[j+1])
                swap(arr[j], arr[j+1]);
        }
    }
}


int main() {
    clock_t start,end;
    
    int n = 10000;
    // 測試模板函數(shù),傳入整型數(shù)組
    int *a  = SortTestHelper::generateRandomArray(n,0,n);
    start = clock();
    insertSort( a , n );
    end = clock();
    cout<<"insertSort: "<<double(end- start)<<endl;
    for (int i = 0; i < 50; ++i)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    cout<<endl;
    delete[] a;


    int *b  = SortTestHelper::generateRandomArray(n,0,n);
    start = clock();
    selectSort( b , n );
    end = clock();
    cout<<"selectSort: "<<double(end- start)<<endl;
    for (int i = 0; i < 50; ++i)
    {
        cout<<b[i]<<" ";
    }
    cout<<endl;
    cout<<endl;
    delete[] b;

    int *c = SortTestHelper::generateRandomArray(n,0,n);
    start = clock();
    bubbleSort( c , n );
    end = clock();
    cout<<"bubbleSort: "<<double(end- start)<<endl;
    for (int i = 0; i < 50; ++i)
    {
        cout<<c[i]<<" ";
    }
    cout<<endl;
    cout<<endl;
    delete[] c;

    // 測試模板函數(shù),傳入自定義結(jié)構體Student數(shù)組
    // Student d[4] = { {"D",90} , {"C",100} , {"B",95} , {"A",95} };
    // bubbleSort(d,4);
    // for( int i = 0 ; i < 4 ; i ++ )
    //     cout<<d[i];
    // cout<<endl;

    return 0;
}

emmm 以后要多用模板,不然的話很難受~感覺自己會很低級。。。所以還是好好的學習吧。。

//######## SortTestHelper.h #########

//
// Created by liuyubobobo on 7/13/16.
//

#ifndef INC_04_INSERTION_SORT_SORTTESTHELPER_H
#define INC_04_INSERTION_SORT_SORTTESTHELPER_H

#include <iostream>
#include <algorithm>
#include <string>
#include <ctime>
#include <cassert>

using namespace std;


namespace SortTestHelper {
    template<typename T>
    T *generateRandomArray(int n, T range_l, T range_r) {

        T *arr = new T[n];
        srand(time(NULL));
        for (int i = 0; i < n; i++)
            arr[i] = rand() % (range_r - range_l + 1) + range_l;
        return arr;
    }

    int *generateNearlyOrderedArray(int n, int swapTimes){

        int *arr = new int[n];
        for(int i = 0 ; i < n ; i ++ )
            arr[i] = i;

        srand(time(NULL));
        for( int i = 0 ; i < swapTimes ; i ++ ){
            int posx = rand()%n;
            int posy = rand()%n;
            swap( arr[posx] , arr[posy] );
        }

        return arr;
    }

    int *copyIntArray(int a[], int n){

        int *arr = new int[n];
        copy(a, a+n, arr);
        return arr;
    }

    template<typename T>
    void printArray(T arr[], int n) {

        for (int i = 0; i < n; i++)
            cout << arr[i] << " ";
        cout << endl;

        return;
    }

    template<typename T>
    bool isSorted(T arr[], int n) {

        for (int i = 0; i < n - 1; i++)
            if (arr[i] > arr[i + 1])
                return false;

        return true;
    }

    template<typename T>
    void testSort(const string &sortName, void (*sort)(T[], int), T arr[], int n) {

        clock_t startTime = clock();
        sort(arr, n);
        clock_t endTime = clock();
        cout << sortName << " : " << double(endTime - startTime) / CLOCKS_PER_SEC << " s"<<endl;

        assert(isSorted(arr, n));

        return;
    }

};

#endif //INC_04_INSERTION_SORT_SORTTESTHELPER_H

為什么上面的文件沒有高亮??臥槽????算了,無所謂了~~

把這兩個文件丟在一個文件夾下,然后運行第一個Cpp文件就OK。

大概來展示下效果。。

MMP 為什么冒泡程序這么菜?

正文之后

因為屬于復習階段,所以就不是很清白的講了,我也是過一遍的形式。。查漏補缺嗎。。。雖然冒泡這兒出了點小bug讓我很難受。。不過理解為上。。

這是Java的,我還有差不多多的python的。。C++的倒是比較少。。。還有不少機器學習的
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容