c++ STL---The comparision between selectionSort and insertionSort

The following programs are the comparision of between selectionSort and insertionSort.

//SortTestHelper.h file
#ifndef SELECTIONSORT_SORTTESTHELPER_H
#define SELECTIONSORT_SORTTESTHELPER_H

#include <iostream>
#include <cassert>
#include <ctime>
#include <string>
using namespace std;

namespace SortTestHelper {

//produce an random array
    int * generateRandomArray(int n,int rangL, int rangR) {
        assert(rangR >= rangL);//judge the boundary of the array

        int *arr = new int[n];
        srand(time(NULL));//the seed of random

        for (int i = 0; i < n; i++)
        {
            arr[i] = rand() % (rangR - rangL) + rangL;//value the array
        }

        return arr;
    }

//print the elements of the array
    template <typename T>
    void printArr(T arr[], int n) {
        for (int i = 0; i < n; i++)
            cout << arr[i] << " ";
        cout << endl;
        return;
    }

//judge the array is a sorted array or not
    template <typename T>
    bool is_Sorted(T arr[], int n) {
        for (int i = 0; i < n - 1; i++)
        {
            if (arr[i] > arr[i + 1])
                return false;
        }

        return true;
    }

//a test function to calculate the using time
    template <typename T>
    void testSort(string sortName, void(*sort)(T[], int n), T arr[], int n) {
        clock_t startTime = clock();
        sort(arr, n);
        clock_t endTime = clock();

        assert(is_Sorted(arr, n));

        cout << sortName << ":" << double(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
        return;
    }
//a function to copy an integer array
    int * copyIntArray(int a[], int n) {
        int * arr = new int[n];
        copy(a, a + n, arr);//三個參數(shù)分別是頭指針,尾指針和目標(biāo)屬組
        return arr;
    }

}

#endif // !SELECTIONSORT_SORTTESTHELPER_H


//main driver program
#include <iostream>
#include <algorithm>
#include "SortTestHelper.h"
using namespace std;

//選擇排序
template <typename T>
void selectionSort(T arr[], int n) {

    for (int i = 0; i < n; i++)
    {
        //尋找[i,n)區(qū)間里的最小值
        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 insertionSort(T arr[], int n) {
    for (int i = 1; i < n; i++)
    {   //尋找元素arr[i]合適的插入位置
        for (int j = i; j > 0 && arr[j] < arr[j - 1]; j--) {
            swap(arr[j], arr[j - 1]);
        }
                
    }
}

int main()
{
    int n = 10000;
    int *arr = SortTestHelper::generateRandomArray(n, 0, n);
    int *arr2 = SortTestHelper::copyIntArray(arr, n);
    //selectionSort(arr, n);
    //SortTestHelper::printArr(arr, n);
    SortTestHelper::testSort("SelectionSort", selectionSort, arr2, n);
    SortTestHelper::testSort("InsertionSort", insertionSort, arr, n);
    delete[] arr;
    delete[] arr2;
    getchar();
    return 0;
}

//the improvement of the insertionSort
#include <iostream>
#include <algorithm>
#include "SortTestHelper.h"
using namespace std;

//選擇排序
template <typename T>
void selectionSort(T arr[], int n) {

    for (int i = 0; i < n; i++)
    {
        //尋找[i,n)區(qū)間里的最小值
        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 insertionSort(T arr[], int n) {
    for (int i = 1; i < n; i++)
    {   //尋找元素arr[i]合適的插入位置
        T e = arr[i];
        int j;
        for (j = i; j > 0 && arr[j-1] > e; j--) {
            arr[j] = arr[j - 1];//判斷當(dāng)前元素是否要待到當(dāng)前位置,判斷條件是當(dāng)前元素與前一個元素進行比較
        }
        arr[j] = e;
                
    }
}

int main()
{
    int n = 10000;
    int *arr = SortTestHelper::generateRandomArray(n, 0, n);
    int *arr2 = SortTestHelper::copyIntArray(arr, n);
    //selectionSort(arr, n);
    //SortTestHelper::printArr(arr, n);
    SortTestHelper::testSort("SelectionSort", selectionSort, arr2, n);
    SortTestHelper::testSort("InsertionSort", insertionSort, arr, n);
    delete[] arr;
    delete[] arr2;
    getchar();
    return 0;
}

//生成一個近乎有序的數(shù)組
    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 j = 0; j < swaptimes; j++) {
            int posx = rand() % n;
            int posy = rand() % n;

            swap(arr[posx], arr[posy]);
        }
        return arr;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,817評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,018評論 0 23
  • 00:05 前奏 男: 00:37那年春色長街,燈火微光 00:41我望你方向 00:44輕抬眼眸見你匹馬單槍 ...
    梨織閱讀 661評論 1 2
  • 文/石天成 戰(zhàn)略布局大于戰(zhàn)術(shù)動作,明了此點,你開始進入高階管理; 第一每天必須拿出一個時間段,用于自己通盤思考當(dāng)下...
    鳳舞九天9527閱讀 225評論 0 0
  • 很多人喜歡把“閱讀好書”寫進自己的新年計劃中,所以這段時間我收到了好多找我推薦好書的咨詢短信。剛開始收到這些短信時...
    木瓜姐姐閱讀 922評論 12 3

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