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);//三个参数分别是头指针,尾指针和目标属组
        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)区间里的最小值
        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)区间里的最小值
        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];//判断当前元素是否要待到当前位置,判断条件是当前元素与前一个元素进行比较
        }
        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;
}

//生成一个近乎有序的数组
    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;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,694评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,855评论 0 23
  • 00:05 前奏 男: 00:37那年春色长街,灯火微光 00:41我望你方向 00:44轻抬眼眸见你匹马单枪 ...
    梨织阅读 652评论 1 2
  • 文/石天成 战略布局大于战术动作,明了此点,你开始进入高阶管理; 第一每天必须拿出一个时间段,用于自己通盘思考当下...
    凤舞九天9527阅读 225评论 0 0
  • 很多人喜欢把“阅读好书”写进自己的新年计划中,所以这段时间我收到了好多找我推荐好书的咨询短信。刚开始收到这些短信时...
    木瓜姐姐阅读 917评论 12 3

友情链接更多精彩内容