Summary and implementation of internal sort selection, bubbling, insertion, hill, fast, merge, and heap sorting principles

table of Contents

 

One, select sort

Two, bubble sort

Three, insertion sort

Four, Hill sort

Five, quick sort

Six, merge sort

Seven, heap sort

Eight, code and call

 

One, select sort

     In the linear table, it is divided into the first part V that is sorted and the latter part that has not been sorted. At the beginning, the entire linear table is not sorted. Selection sorting is to select the minimum value and change to the top every time in the part of the unsorted U set. Then it is included in set V.

Two, bubble sort

    Bubble sorting, each time sorting compares two consecutive elements, the small element is exchanged to the front, and the large element is exchanged to the back, so that each time the small element "floats" the front of the array or the big element "sinks" to the back of the array. process.

Three, insertion sort

     Insertion sorting is divided into the former part V which is sorted and the latter part which is not sorted yet U. Start with i=1 and compare with the 0th element. If the sorted set is larger than the compared element, move one position backward, and finally put the compared element after the element that is smaller than itself.

Four, Hill sort

     That is, insertion sort with step size. The step length of the last sorting must be 1. Add the step length set to {4,3,1}. The code is inserted and sorted once every element in the original linear table with a mutual interval of 4. After that, every mutual in the original linear table The elements with an interval of 3 are inserted and sorted once, and finally every element with an interval of 1 in the original linear table is inserted and sorted once. The general insertion sort is actually a Hill sort with a step length of 1. Since the time complexity of insertion sort in the order set can reach O(n). So Hill sort is to sort the sub-sets as much as possible. If the appropriate step size set by the step size set, the time complexity of Hill sorting can be reduced to O(n(log n)^2).

Five, quick sort

    Quick sorting, each sorting puts the element to be sorted into its final position, and its right side is larger than it, and the left side is smaller than it.

The specific operation steps are to determine the element to be compared, if the traversal starts from back to front, then when the traversal to the element is smaller than the element to be compared, the exchange is performed, and then the traversal starts from the exchange position from the front to the back. If it is traversed at this time that the element is larger than the element to be compared, exchange. Repeated traversal, and finally confirm its position.

In the second sorting, as long as the elements whose positions are determined last time, the left set and the right set are quickly sorted separately.

Six, merge sort

    The initial elements are set up as a set, and each set can be a set that has been sorted. Then the sets are merged in pairs. By comparing the "head" elements in the two sets, the small elements are moved and merged into a new space to form a new set. Continue to merge and finally merge into a big collection.

Seven, heap sort

   To construct a linear table sorted from small to large, a "big top heap" needs to be constructed, which is actually a complete binary tree with each parent node larger than its child nodes. For the linear table [0...length], it is hierarchically mapped to a complete binary tree. The left child of the nth element has 2n+1 elements, and the right child is the 2n+2th element.

The steps to adjust to "big top" are as follows. Two children choose the bigger one, and then compare with the parent node. If the child node and the parent node are larger, then exchange. Then compare with the child until the leaf node is compared. In the complete binary tree [0....length], the position of the last non-leaf node is floor(heaplen/2)-1.

The operation steps are as follows.

First, adjust the disordered array into a large top heap. Adjust from floor(heaplen/2)-1 to 0.

When adjusting to a large top pile, the top element of the pile is exchanged with the last element, and the large top element is pushed to the back of the array. Then adjust the pusher node.

Eight, code and call

 

hpp

//
// Created by Administrator on 2020/11/2.
//

#ifndef SMARTDONGLIB_SORT_H
#define SMARTDONGLIB_SORT_H
#include "const.h"
#include <vector>
//#include<cstring>
#include <iostream>
 namespace SmartDongLib{
    class Sort{
    public:
        enum sortMethod {

            SelectionSort =1,
            BubbleSort ,
            InsertionSort,
            ShellSort,
            QuickSort,
            MergingSort=6,
            HeapSort
        };
        template<class _tp,class _RandomAccessIterator , class _Compare>
        static void sort(_RandomAccessIterator  _first, _RandomAccessIterator  _last,
                                _Compare _comp ,sortMethod method=QuickSort){
            switch (method) {
                case SelectionSort:
                    selectionSort(_first,_last,_comp);
                    break;
                case BubbleSort:
                    bubbleSort(_first,_last,_comp);
                    break;
                case InsertionSort:
                    insertionSort(_first,_last,_comp);
                    break;
                case ShellSort:
                    shellSort(_first,_last,_comp);
                    break;
                case QuickSort:
                    quickSort(_first,_last-1,_comp);
                    break;
                case MergingSort:
                    mergingSort<_tp>(_first,_last,_comp);
                case HeapSort:
                    heapSort(_first,_last,_comp);
                default:
                    quickSort(_first,_last-1,_comp);
                    break;
            }
        }
        template<class _tp,class _RandomAccessIterator>
        static void sort(_RandomAccessIterator _first, _RandomAccessIterator _last,sortMethod method=QuickSort){
            switch (method) {
                case SelectionSort:
                    selectionSort(_first,_last);
                    break;
                case BubbleSort:
                    bubbleSort(_first,_last);
                    break;
                case InsertionSort:
                    insertionSort(_first,_last);
                    break;
                case ShellSort:
                    shellSort(_first,_last);
                    break;
                case QuickSort:
                    quickSort(_first,_last-1);
                    break;
                case MergingSort:
                    mergingSort<_tp>(_first,_last);
                case HeapSort:
                    heapSort(_first,_last);
                default:
                    quickSort(_first,_last-1);
                    break;
            }
        }

    protected:
       /**
       * <p>堆排序.每次选择最值放到子节点前
       * @tparam _RandomAccessIterator    线性表地址类型
       * @tparam _Compare                 比较函数类型
       * @param _first                    线性表起始地址
       * @param _last                     线性表结束地址
       * @param _comp                     比较函数
       */
        template< class _RandomAccessIterator, class _Compare>
        static void heapSort(_RandomAccessIterator _first, _RandomAccessIterator _last,
                             _Compare _comp ){
            int heaplen  =_last - _first;
            //把无序堆建立成有序堆,从最后一个节点的父节点 heaplen -1,堆的一半开始调整
            for (int i = heaplen/2 -1 ; i >= 0 ; --i) {
                HeapAdjust(_first,i,heaplen-1,_comp);
            }
            //有序堆,每次调整后堆顶和最后一元素交换。
            for (int j = heaplen-1; j >0 ; --j) {
                iteratorSwap(_first,_first+j);
                HeapAdjust(_first,0,j-1,_comp);
            }
        }
        template< class _RandomAccessIterator >
        static void heapSort(_RandomAccessIterator _first, _RandomAccessIterator _last ){
            int heaplen  =_last - _first;
            //把无序堆建立成有序堆,从最后一个节点的父节点 heaplen -1,堆的一半开始调整
            for (int i = heaplen/2 -1 ; i >= 0 ; --i) {
                HeapAdjust(_first,i,heaplen-1);
            }
            //有序堆,每次调整后堆顶和最后一元素交换。
            for (int j = heaplen-1; j >0 ; --j) {
                iteratorSwap(_first,_first+j);
                HeapAdjust(_first,0,j-1);
            }
        }

        /**
        * <p>归并排序.每次选择最值放到最前面
        * @tparam _RandomAccessIterator    线性表地址类型
        * @tparam _Compare                 比较函数类型
        * @param _first                    线性表起始地址
        * @param _last                     线性表结束地址
        * @param _comp                     比较函数
        */
        template<class _tp,class _RandomAccessIterator, class _Compare>
        static void mergingSort(_RandomAccessIterator _first, _RandomAccessIterator _last,
                                _Compare _comp ){
            Size len = _last -_first;
            _tp temp[len];
            Sort::mergeSort(_first,temp,0,len-1,_comp);
        }

        template<class _tp,class _RandomAccessIterator>
        static void mergingSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last){
            Size len = _last -_first;
            _tp temp[len];
            Sort::mergeSort(_first,temp,0,len-1);

        }

        /**
         * <p>选择排序.每次选择最值放到最前面
         * @tparam _RandomAccessIterator    线性表地址类型
         * @tparam _Compare                 比较函数类型
         * @param _first                    线性表起始地址
         * @param _last                     线性表结束地址
         * @param _comp                     比较函数
         */
        template<class _RandomAccessIterator, class _Compare>
        static void selectionSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last,
                _Compare _comp ){
            for (_RandomAccessIterator iter = _first ; iter < _last ;iter++){
                _RandomAccessIterator maxValue = iter;
                for (_RandomAccessIterator index= iter; index <  _last; ++index) {
                    if ( _comp (*index,*maxValue)  ){
                        maxValue=index;
                    }
                }
                iteratorSwap(maxValue,iter);
            }
        }
        template<class _RandomAccessIterator>
        static void selectionSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last){
            for (_RandomAccessIterator iter = _first ; iter < _last ;iter++){
                _RandomAccessIterator maxValue = iter;
                for (_RandomAccessIterator index= iter; index <  _last; ++index) {
                    if (*index<*maxValue ){
                        maxValue=index;
                    }
                }
                iteratorSwap(maxValue,iter);
            }
        }
        /**
         * <p> 冒泡排序。每次双循环把最值交换到最前面
         * @tparam _RandomAccessIterator    线性表地址类型
         * @tparam _Compare                 比较函数类型
         * @param _first                    线性表起始地址
         * @param _last                     线性表结束地址
         * @param _comp                     比较函数
         */
        template<class _RandomAccessIterator, class _Compare>
        static void bubbleSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last,
                                  _Compare _comp ){
            for (_RandomAccessIterator iter = _last ; iter > _first+1 ;iter--){
                for (_RandomAccessIterator index= _first; index <  iter -1; ++index) {
                    if ( _comp (*(index +1),*index)  ){
                        iteratorSwap(index,index +1);
                    }
                }

            }
        }
        template<class _RandomAccessIterator>
        static void bubbleSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last){
            for (_RandomAccessIterator iter = _last ; iter > _first+1 ;iter--){
                for (_RandomAccessIterator index= _first; index <  iter -1; ++index) {
                    if ( *(index +1)<*index  ){
                        iteratorSwap(index,index +1);
                    }
                }

            }
        }
        /**
         * <p> 直接插入排序。每次双循环i次表示前 i-1 个数都已经排好序。
         * @tparam _RandomAccessIterator    线性表地址类型
         * @tparam _Compare                 比较函数类型
         * @param _first                    线性表起始地址
         * @param _last                     线性表结束地址
         * @param _comp                     比较函数
         */
        template<class _RandomAccessIterator, class _Compare>
        static void insertionSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last,
                               _Compare _comp ){
            singleShellInsert(_first,_last,_comp,1);
        }
        template<class _RandomAccessIterator>
        static void insertionSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last){
            singleShellInsert(_first,_last,1);
        }
        /**
         * <p> 希尔排序,有步长的插入排序
         * @tparam _RandomAccessIterator
         * @tparam _Compare
         * @param _first
         * @param _last
         * @param _comp
         */
        template<class _RandomAccessIterator, class _Compare>
        static void shellSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last,
                              _Compare _comp){
            Size len =_last-_first;
            std::vector<Size> dlta;
//            int loopnum =(int)(log10(2*len+1) / log10(3) );
            for (int i = (len>>1); i >1; i=(i>>1) ){
                dlta.push_back(i);
            }
            dlta.push_back(1);
            for (int j = 0; j < dlta.size(); ++j) {
                singleShellInsert(_first,_last,_comp,dlta[j]);
            }
        }
        template<class _RandomAccessIterator>
        static void shellSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last){
            Size len =_last-_first;
            std::vector<Size> dlta;
//            int loopnum =(int)(log10(2*len+1) / log10(3) );
            for (int i = (len>>1); i >1; i=(i>>1) ){
                dlta.push_back(i);
            }
            dlta.push_back(1);
            for (int j = 0; j < dlta.size(); ++j) {
                singleShellInsert(_first,_last,dlta[j]);
            }
        }

        /**
         * <p> 快速排序,每一次partitio确定一个元素对应的位置,左右两边序列递归
         * @tparam _RandomAccessIterator    线性表地址类型
         * @tparam _Compare                 比较函数类型
         * @param _first                    线性表起始地址
         * @param _last                     线性表结束地址
         * @param _comp                     比较函数
         */
        template<class _RandomAccessIterator, class _Compare>
        static void quickSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last,
                              _Compare _comp){
            if (_first<_last){
               _RandomAccessIterator pivotloc = singlePartition(_first,_last,_comp);
               quickSort(_first,pivotloc-1,_comp);
               quickSort(pivotloc+1,_last,_comp);

           }
        }
        template<class _RandomAccessIterator>
        static void quickSort(_RandomAccessIterator  _first, _RandomAccessIterator  _last){
            if (_first<_last){
                _RandomAccessIterator pivotloc = singlePartition(_first,_last);
//                for (_RandomAccessIterator i = _first; i < _last; ++i) {
//                    std::cout<< *i <<"  ";
//                }
//                std::cout<<"\n";
                quickSort(_first,pivotloc-1 );
                quickSort(pivotloc+1,_last );

            }
        }


        template<class _RandomAccessIterator, class _Compare>
        static void
        mergeSort(_RandomAccessIterator  _arr1first, _RandomAccessIterator  _arr2first,int s, int t,
                  _Compare _comp) {
            //把arr1[s...t] 并入到 arr2[s...t];
            if (s == t) {
                *(_arr2first + s) =  *(_arr1first + s);
            } else{
                int m =(s + t )/2;
                mergeSort(_arr1first,_arr2first,s,m,_comp);
                mergeSort(_arr1first,_arr2first,m+1,t,_comp);
                merging(_arr2first,_arr1first,s,m,t,_comp);
//                memcpy(_arr2first+s,_arr1first+s,(t-s+1)*sizeof(*_arr2first));
            }

        }

        template<class _RandomAccessIterator>
        static void
        mergeSort(_RandomAccessIterator  _arr1first, _RandomAccessIterator  _arr2first,int s, int t) {
            //把arr1[s...t] 并入到 arr2[s...t];
            if (s == t) {
                *(_arr2first + s) =  *(_arr1first + s);
            } else{
                int m =(s + t )/2;
                mergeSort(_arr1first,_arr2first,s,m);
                mergeSort(_arr1first,_arr2first,m+1,t);
                merging(_arr2first,_arr1first,s,m,t);
//                memcpy(_arr2first+s,_arr1first+s,(t-s+1)*sizeof(*_arr2first));
            }

        }

        template<class _RandomAccessIterator, class _Compare>
        static void singleShellInsert(_RandomAccessIterator  _first, _RandomAccessIterator  _last,
                                      _Compare _comp,Size dk){
            for (_RandomAccessIterator iter = _first+ dk ; iter < _last ;iter++){
                auto temp =  *iter;
                _RandomAccessIterator index = iter -dk;
                //假如前面 iter -1 个都已经排好序 ,现在插第 iter 个
                // 先把iter的值保存起来 到temp
                // 然后从  iter -1 开始 和 temp 开始比较,如果小则 index的值往后移
                // 当比较结果为大时结束循环,把 temp 放入到 当前比较位置的后一个
                for ( ;index >= _first && _comp(temp , *index); index-=dk) {
                    *(index +dk) = *(index);
                }
                *(index +dk) = (temp);
            }
        }
        template<class _RandomAccessIterator >
        static void singleShellInsert(_RandomAccessIterator  _first, _RandomAccessIterator  _last,
                                       Size dk){
            for (_RandomAccessIterator iter = _first+ dk ; iter < _last ;iter++){
                auto temp =  *iter;
                _RandomAccessIterator index = iter -dk;
                //假如前面 iter -1 个都已经排好序 ,现在插第 iter 个
                // 先把iter的值保存起来 到temp
                // 然后从  iter -1 开始 和 temp 开始比较,如果小则 index的值往后移
                // 当比较结果为大时结束循环,把 temp 放入到 当前比较位置的后一个
                for ( ;index >= _first && temp < *index; index-=dk) {
                    *(index +dk) = *(index);
                }
                *(index +dk) = (temp);
            }
        }

    private:
        template<class _RandomAccessIterator>
        static void iteratorSwap(_RandomAccessIterator  _elem1, _RandomAccessIterator  _elem2){
            auto temp = *_elem1;
            *_elem1 = *_elem2;
            *_elem2 = temp;
        }

        template<class _RandomAccessIterator, class _Compare>
        static _RandomAccessIterator singlePartition(_RandomAccessIterator  _first, _RandomAccessIterator  _last,
                               _Compare _comp ){


            auto temp = *_first;
            while(_first -_last <0){
                while (_first<_last && _comp(temp,*_last)){
                    --_last;
                }
                *_first=*_last;
                while (_first<_last && _comp(*_first,temp)){
                    ++_first;
                }
                *_last=*_first;
            }
            *_first=temp;
            return  _first;
        }
        /**
         * <p> 找到指定位置
         * @tparam _RandomAccessIterator
         * @param _first 包含起始位置
         * @param _last  包含结束位置
         * @return
         */
        template<class _RandomAccessIterator >
        static _RandomAccessIterator singlePartition(_RandomAccessIterator  _first, _RandomAccessIterator  _last){
            auto temp = *_first;
            while(_first -_last <0){
                while (_first<_last && temp<=*_last){
                    --_last;
                }
                *_first=*_last;
                while (_first<_last && *_first<=temp){
                    ++_first;
                }
                *_last=*_first;
            }
            *_first=temp;
            return  _first;
        }
        template<class _RandomAccessIterator, class _Compare>
        static void
        merging(_RandomAccessIterator  _arr1first, _RandomAccessIterator  _arr2first, int i, int m, int n,
                _Compare _comp) {
            //把arr1[i...mid]和arr1[mid+1...n]  并入到 arr2[i .... n];
            int j,k; //j属于mid+1...last     i属于i...mid   k属于 i...last
            for (j = m+1 ,k=i; i <=m && j <= n ; ++k) {
                if (_comp(*(_arr1first + i) , *(_arr1first + j))){
                    *(_arr2first + k)  = *(_arr1first+ (i++));
                } else{
                    *(_arr2first + k)  = *(_arr1first+ (j++));
                }
            }
            if ( i <=m){
//                memcpy(_arr2first+k,_arr1first+i,(m-i+1)*sizeof(*_arr2first));
                for (;k<=n && i<=m   ; k++,i++) {
                    *(_arr2first + k)  = *(_arr1first+ i);
                }
            }
            if ( j <=n){
//                memcpy(_arr2first+k,_arr1first+j,(n-j+1)*sizeof(*_arr2first));
                for (;k<=n && i<=n   ; k++,i++) {
                    *(_arr2first + k)  = *(_arr1first+ i);
                }
            }
        }
        template<class _RandomAccessIterator>
        static void
        merging(_RandomAccessIterator  _arr1first, _RandomAccessIterator  _arr2first, int i, int m, int n) {
            //把arr1[i...mid]和arr1[mid+1...n]  并入到 arr2[i .... n];
            int j,k; //j属于mid+1...last     i属于i...mid   k属于 i...last
            for (j = m+1 ,k=i; i <=m && j <= n ; ++k) {
                if (*(_arr1first + i) < *(_arr1first + j)){
                    *(_arr2first + k)  = *(_arr1first+ (i++));
                } else{
                    *(_arr2first + k)  = *(_arr1first+ (j++));
                }
            }
            if ( i <=m){
//                memcpy(_arr2first+k,_arr1first+i,(m-i+1)*sizeof(*_arr2first));
                for (;k<=n && i<=m   ; k++,i++) {
                    *(_arr2first + k)  = *(_arr1first+ i);
                }
            }
            if ( j <=n){
//                memcpy(_arr2first+k,_arr1first+j,(n-j+1)*sizeof(*_arr2first));
                for (;k<=n && i<=n   ; k++,i++) {
                    *(_arr2first + k)  = *(_arr1first+ i);
                }
            }
        }

        /**
        * 堆节点"筛选"函数,堆顶自堆底的调整方式
        * @tparam _RandomAccessIterator
        * @tparam _Compare
        * @param _first
        * @param _last
        * @param _comp
        */
        template<class _RandomAccessIterator, class _Compare>
        static void HeapAdjust(_RandomAccessIterator _first, int nodeIndex,int heapLenth,
                               _Compare _comp ){
            //从小到大排序用大顶堆,父节点比自身节点大,假如按层(从0开始)排列,那么 第n个节点的左孩子是 2n+1  2n+2
            //和自己的孩子进行比较然后交换,接着继续和孩子比较到叶子节点
            auto temp = *(_first + nodeIndex);
            for (int i = 2*nodeIndex +1; i <=heapLenth ; i =2 *i+1 ) {
                if (i < heapLenth && _comp( *(_first+i) , *(_first+i+1)))
                    ++i;
                if (!_comp(temp,*(_first + i)))
                    break;
                *(_first + nodeIndex) = *(_first+i);
                nodeIndex = i;
            }
            *(_first+nodeIndex) = temp;
        }
        /**
        * 堆节点"筛选"函数,堆顶自堆底的调整方式
        * @tparam _RandomAccessIterator
        * @tparam _Compare
        * @param _first
        * @param _last
        * @param _comp
        */
        template<class _RandomAccessIterator >
        static void HeapAdjust(_RandomAccessIterator _first, int nodeIndex,int heapLenth){
            //从小到大排序用大顶堆,父节点比自身节点大,假如按层(从0开始)排列,那么 第n个节点的左孩子是 2n+1  2n+2
            //和自己的孩子进行比较然后交换,接着继续和孩子比较到叶子节点
            auto temp = *(_first + nodeIndex);
            for (int i = 2*nodeIndex +1; i <=heapLenth ; i =2 *i+1 ) {
                if (i < heapLenth &&  *(_first+i)< *(_first+i+1) )
                    ++i;
                if (! temp<*(_first + i) )
                    break;
                *(_first + nodeIndex) = *(_first+i);
                nodeIndex = i;
            }
            *(_first+nodeIndex) = temp;
        }
    };
}
#endif //SMARTDONGLIB_SORT_H

cpp

//
// Created by Administrator on 2020/11/2.
//

#include<iostream>
#include "sdalgorithm/util/sort.h"
#include <ctime>
#include <malloc.h>
#include<algorithm>

using namespace std;
using namespace SmartDongLib;
void print(int a[],int size){
    for (int i = 0; i < size; ++i) {
        cout<< a[i]<<"  ";
    }
    cout<<endl;
};
class C1{
public:
    C1(int a){a_=a;}
    int a_;
};
int main(){

    const int veclen =100000 ;
    int a[veclen] ;
//    a.resize(veclen);
//    int a[10] ={9,34,24,56,31,24,66,3,45,20};
//    Sort::sort(a.begin(),a+veclen,[](int x,int y){return abs(x-30)<=abs(y-30);},SmartDongLib::Sort::QuickSort);
//    print(a,10);
    unsigned  seed = time(0);
    srand(seed);
    for (int i = 0; i < veclen; ++i) {
        a[i]=rand();
    }
    clock_t startSelectionSort,endSelectionSort;
    startSelectionSort = clock();
    Sort::sort<int>(a,a+veclen,SmartDongLib::Sort::SelectionSort);
    endSelectionSort = clock();
    cout<<"1.SelectionSort:"<< (double)(endSelectionSort - startSelectionSort)
    <<"\t isSort:"<<std::is_sorted(a,a+veclen)<<endl;


    for (int i = 0; i < veclen; ++i) {
        a[i]=rand();
    }

    clock_t startBubbleSort,endBubbleSort;
    startBubbleSort = clock();
    Sort::sort<int>(a,a+veclen,SmartDongLib::Sort::BubbleSort);
    endBubbleSort = clock();
    cout<<"2.BubbleSort:"<< (double)(endBubbleSort - startBubbleSort)
            <<"\t isSort:"<<std::is_sorted(a,a+veclen)<<endl;;
    for (int i = 0; i < veclen; ++i) {
        a[i]=rand();
    }

    clock_t startInsertionSort,endInsertionSort;
    startInsertionSort = clock();
    Sort::sort<int>(a,a+veclen,SmartDongLib::Sort::InsertionSort);
    endInsertionSort = clock();
    cout<<"3.InsertionSort:"<< (double)(endInsertionSort - startInsertionSort)
            <<"\t isSort:"<<std::is_sorted(a,a+veclen)<<endl;

    ;
    for (int i = 0; i < veclen; ++i) {
        a[i]=rand();
    }
    clock_t startShellSort,endShellSort;
    startShellSort = clock();
    Sort::sort<int>(a,a+veclen,SmartDongLib::Sort::ShellSort);
    endShellSort = clock();
    cout<<"4.ShellSort:"<< (double)(endShellSort - startShellSort)
            <<"\t isSort:"<<std::is_sorted(a,a+veclen)<<endl;;

    for (int i = 0; i < veclen; ++i) {
        a[i]=rand();
    }
    clock_t startQuickSort,endQuickSort;
    startQuickSort = clock();
//    print(a, veclen);
    Sort::sort<int>(a,a+veclen,SmartDongLib::Sort::QuickSort);
//    print(a, veclen);
    endQuickSort = clock();
    cout<<"5.QuickSort:"<< (double)(endQuickSort - startQuickSort)
            <<"\t isSort:"<<std::is_sorted(a,a+veclen)<<endl;;

    for (int i = 0; i < veclen; ++i) {
        a[i]=rand();
    }
    clock_t startMergingSort,endMergingSort;
    startMergingSort = clock();
    Sort::sort<int>(a, a+veclen,Sort::MergingSort) ;
    endMergingSort = clock();
    cout<<"6.MergingSort:"<< (double)(endMergingSort - startMergingSort)
            <<"\t isSort:"<<std::is_sorted(a,a+veclen)<<endl;

    for (int i = 0; i < veclen; ++i) {
        a[i]=rand();
    }
//    int b[8] = {49,38,65,97,76,13,27,49};
    clock_t startHeapSort,endHeapSort;
    startHeapSort = clock();
    Sort::sort<int>(a, a+veclen,Sort::HeapSort) ;
    endHeapSort = clock();
    cout<<"7.HeapSort:"<< (double)(endHeapSort - startHeapSort)
        <<"\t isSort:"<<std::is_sorted(a,a+veclen)<<endl;
//    print(a,veclen);


    for (int i = 0; i < veclen; ++i) {
        a[i]=rand();
    }
    clock_t startSTLSort,endSTLSort;
    startSTLSort = clock();
    std::sort(a, a+veclen) ;
    endSTLSort = clock();
    cout<<"8.STLSort:"<< (double)(endSTLSort - startSTLSort)
        <<"\t isSort:"<<std::is_sorted(a,a+veclen)<<endl;

}







 

Output result

 

 

 

 

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/109711636