九大经典排序算法整理及其源代码分享(纯C++版)

本文整理了九大经典排序算法的C++实现,包括头文件9sortEx.h、类实现文件9sortEx.cpp和测试程序test.cpp。测试数据随机生成,范围在0到500之间,规模从10到10000不等。测试在Visual Studio 2015 Enterprise的Debug模式下进行,结果显示快速排序在平均情况下表现最优,而直接插入排序在最坏情况下表现最佳。
摘要由CSDN通过智能技术生成

九大经典排序算法整理及其源代码分享(纯C++版)
[TOC]

头文件–9sortEx.h

#ifndef SORTEX_H_
#define SORTEX_H_

typedef int Comparable;

#include <vector>
using std::vector;

class sortEx
{
public:
    //static bool lessThan(Comparable v, Comparable w);
    void exch(int i, int j);
    bool isSorted()const;
    void show()const;
    virtual void sort() = 0;
protected:
    sortEx(int n = 0);
    vector<Comparable> a;
};

class insertSortStraight :public sortEx
{
public:
    insertSortStraight(int n):sortEx(n){ }
    void sort();
};

class insertSortShell :public sortEx
{
public:
    insertSortShell(int n) :sortEx(n)
    {
    }
    void sort();
};

class swapSortBubble :public sortEx
{
public:
    swapSortBubble(int n) :sortEx(n)
    {
    }
    void sort();
};

class swapSortQuick :public sortEx
{
public:
    swapSortQuick(int n) :sortEx(n)
    {
    }
    void sort();
private:
    int Partition(int st, int ed);   //[st, ed]
    void sort(int st, int ed);       //[st, ed)
};


class selectionSortSimple :public sortEx
{
public:
    selectionSortSimple(int n) :sortEx(n)
    {
    }
    void sort();
};

class selectionSortHeap :public sortEx
{
public:
    selectionSortHeap(int n) :sortEx(n)
    {
    }
    void sort();
private:
    void createHeap();
    void heapAdjust(int totalN, int pos);
};

class mergeSort :public sortEx
{
public:
    mergeSort(int n) :sortEx(n)
    {
        aux.resize(n);
    }
    //自顶向下的归并
    void sort();
    //自底向上的归并
    void sortAnther();
private:
    void sort(int st, int ed);            //[st, ed]
    void merge(int st, int mid, int ed);  //[st, ed]
    vector<Comparable> aux;
    int minNum(int a, int b)
    {
        return a < b ? a : b;
    }
};

class countSort :public sortEx
{
public:
    countSort(int n) :sortEx(n)
    {
    }
    void sort();
};

class radixSort :public sortEx
{
public:
    radixSort(int n) :sortEx(n), RADIX(10)
    {
    }
    void sort();
private:
    int getRadix(int num, int sq);
    void Distribute(int sq, vector<int>& cnt);
    void Collect(int sq, vector<int>& cnt, vector<int>& tmp);
    const int RADIX;
};

#endif

类实现文件–9sortEx.cpp

/*
 * ---------------------------------------------------
 *  Copyright (c) 2017 josan All rights reserved.
 * ---------------------------------------------------
 *
 *               创建者: Josan
 *             创建时间: 2017/9/3 15:28:42
 */
#include <iostream>
#include <vector>
#include <cassert>
#include <iomanip>
#include <cmath>
#include <queue>
#include <functional>
#include <cassert>
#include "9sortEx.h"
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值