几种排序算法的实现与比较

编译环境:vs2013

/*

**排序算法比较** 
利用随机函数产生N个随机整数,利用直接插入排序、折半插入排序,起泡排序、快速排序、选择排序、堆排序,基数排序七种排序方法进行排序(结果为由小到大的顺序),并比较次数和交换次数。
这里是源代码
 */

#include<iostream>
using namespace std;
#include<stdlib.h>
#include<time.h>
#include<fstream>

#define LOW 1
#define HIGH 1000000
#define SUCCESS 1
#define UNSUCESS 0
#define DUPLICATE -1
#define OK 1
#define ERROR 0

#define LIST_INIT_SIZE 30002       //  线性表存储空间的初始分配量
#define LISTINCREAMENT 200          //  线性表存储空间的分配增量

#define MEMBER 30
#define MAX_SIZE 200

typedef int Status;

typedef struct
{
    int exchange;
    int compare;
}SortO;

typedef int Status;
typedef int ElemType;
typedef struct
{
    ElemType *elem;         //  存储空间基址
    int Length;             //  当前长度
    int ListSize;           //  当前分配的存储容量(以sizeof(ElemType)为单位)
}SqList;

Status InitList_Sq(SqList &L)       //  构造一个空的线性表
{
    L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if (!L.elem)  return OVERFLOW;         //  申请空间,如果失败,返回OVERFLOW
    L.Length = 0;                          //  初始化长度为0
    L.ListSize = LIST_INIT_SIZE;           //  当前空间为初始分配量
    return OK;
}

Status CreatList(SqList &L,int len)
{
    ifstream file;
    int num;
    InitList_Sq(L);
    file.open("随机数.txt", ios::in);
    if (!file)
    {
        cout << "打开文件失败" << endl;
        exit(0);
    }
    for (int i = 1; i <= len; i++)
    {
        file >> num;
        if (!num)
        {
            i = i - 1;
            continue;
        }
        L.elem[i] = num;
        num = 0;
        L.Length++;
        if (L.Length + 1>L.ListSize)   //  如果超出了线性表已申请的空间
        {
            L.elem = (ElemType*)realloc(L.elem, (L.ListSize + LISTINCREAMENT)*sizeof(ElemType));
            if (!L.elem)  return ERROR;     //  申请新的空间,如果失败,返回ERROR
        }
    }
    file.close();
    return OK;
}

Status RandomNum()
{
    ofstream file;
    int num;
    file.open("随机数.txt", ios::out);
    if (!file)
        exit(OVERFLOW);
    srand((unsigned)time(NULL));
    for (int i = 0; i < 100000; i++)
    {
        num = LOW + rand() % (HIGH - LOW + 1);
        file << num << endl;
    }
    file.close();
    return OK;
}

Status InserSort(SqList &L, SortO &O)
{
    for (int i = 2; i <= L.Length; i++)
    {
        O.compare++;
        if (L.elem[i] < L.elem[i - 1])
        {
            L.elem[0] = L.elem[i];
            L.elem[i] = L.elem[i - 1];
            O.exchange = O.exchange + 2;
            int j = i - 2;
            O.compare++;
            while (L.elem[0]<L.elem[j])
            {
                O.exchange++;
                L.elem[j+1] = L.elem[j];
                j--;
            }
            L.elem[j + 1] = L.elem[0];
            O.exchange++;
        }
    }
    return OK;
}

Status BInsertSort(SqList &L, SortO &O)
{
    int low, high,m;
    for (int i = 2; i <= L.Length; i++)
    {
        L.elem[0] = L.elem[i];
        O.exchange++;
        low = 1, high = i - 1;
        O.compare++;
        while (low <= high)
        {
            m = (low + high) / 2;
            O.compare++;
            if (L.elem[0] < L.elem[m])
                high = m - 1;
            else
                low = m + 1;
        }
        for (int j = i - 1; j >= high + 1; j--)
        {
            L.elem[j + 1] = L.elem[j];
            O.exchange++;
        }
        L.elem[high + 1] = L.elem[0];
        O.exchange++;
    }
    return OK;
}

Status BubbleSort(SqList &L, SortO &O)
{
    for (int i = 1; i <= L.Length; i++)
    {
        for (int j = 1; j <= L.Length - i;j++)
        {
            O.compare = O.compare + 1;
            if (L.elem[j] > L.elem[j + 1])
            {
                L.elem[0] = L.elem[j];
                L.elem[j] = L.elem[j + 1];
                L.elem[j + 1] = L.elem[0];
                O.exchange += 3;
            }
        }
    }
    return OK;
}

int Partition(SqList &L, int low, int high, SortO &O)
{
    int privotkey=0;

    L.elem[0] = L.elem[low];
    privotkey = L.elem[low];
    O.exchange = O.exchange + 2;
    O.compare++;
    while (low < high)
    {
        O.compare += 4;
        O.exchange += 2;
        while (low < high&&L.elem[high] >= L.elem[0])
            high--;
        if (high == low)
            continue;
        L.elem[low] = L.elem[high];

        while (low < high&&L.elem[low] <= L.elem[0])
            low++;
        if (high == low)
            continue;
        L.elem[high] = L.elem[low];
    }
    L.elem[low] = L.elem[0];
    L.elem[0] = 0; 
    O.exchange++;

    return low;
}

Status QSort(SqList &L, int l, int h, SortO &O)
{
    int Partition(SqList &L, int low, int high, SortO &O);
    if (l > h)
        return OK;
    int privotloc;
    int m, n;
    if (l < h)
    {
        privotloc = Partition(L, l, h, O);
        m = privotloc - 1;
        QSort(L, l, m,O);
        n = privotloc + 1;
        QSort(L, n, h,O);
    }
    return OK;
}

void QuickSort(SqList &L, SortO &O)
{
    QSort(L, 1, L.Length,O);
}

Status SelectSort(SqList &L, SortO &O)
{
    int k;
    for (int i = 1; i < L.Length; i++)
    {
        k = i;
        L.elem[0] = L.elem[i];
        O.exchange++;
        for (int j = i+1; j <= L.Length; j++)
        {
            O.compare++;
            if (L.elem[0] > L.elem[j])
            {
                O.exchange++;
                L.elem[0] = L.elem[j];
                k = j;
            }
        }
        if(k!=i)
        {
            O.exchange += 2;
            L.elem[k] = L.elem[i];
            L.elem[i] = L.elem[0];
        }
    }
    return OK;
}

Status HeapAdjust(SqList &L, int s, int m, SortO &O)
{
    int rc;
    rc = L.elem[s];
    O.exchange++;
    for (int i = 2 * s; i <= m; i = i * 2)
    {
        O.compare += 2;
        if (i < m && L.elem[i] < L.elem[i + 1])
            i++;
        if (rc >= L.elem[i])
            break;
        L.elem[s] = L.elem[i];
        s = i;
        O.exchange++;
    }
    L.elem[s] = rc;
    O.exchange++;
    return OK;
}

Status HeapSort(SqList &L, SortO &O)
{
    int rc;
    for (int i = L.Length / 2; i > 0; i--)
        HeapAdjust(L, i, L.Length,O);
    for (int i = L.Length; i > 1; --i)
    {
        O.exchange += 3;
        rc = L.elem[1];
        L.elem[1] = L.elem[i];
        L.elem[i] = rc;
        HeapAdjust(L, 1, i - 1,O);
    }
    return OK;
}

Status RadixSort(SqList &L, SortO &O)
{

    int temp[11][23000];
    int order[10]={0};
    int k;
    int n;
    int p;
    n = 1;
    while (n <= 100000)
    {
        for (int i = 1; i <= L.Length; i++)
        {
            k = (L.elem[i] / n) % 10;
            if (k < 0)
            {
                i = i - 1;
                continue;
            }
            O.exchange+=2;
            temp[k][order[k]] = L.elem[i];
            order[k] = 1+order[k];
        }
        p = 1;
        for (int i = 0; i < 10; i++)
        {
            O.compare++;
            if (order[i] != 0)
            {
                for (int j = 0; j < order[i]; j++)
                {
                    O.exchange++;
                    L.elem[p] = temp[i][j];

                    p++;
                }
                order[i] = 0;
            }
        }
        n =n*10;
    }
    return OK;
}

void Print(SqList L)
{
    for (int i = 1; i <= L.Length; i++)
    {
        cout << L.elem[i] << "\t";
    }
    cout << endl;
}

int main()
{
    char ch;
    int len;

    SqList L,LBIS, LSIS, LBS, LQS, LSS, LHS, LRS;
    SortO BIS, SIS, BS, QS, SS, HS, RS;
    SIS.compare = SIS.exchange = BIS.compare = BIS.exchange = BS.compare
        =BS.exchange = QS.compare = QS.exchange = SS.compare = SS.exchange
        = HS.compare = HS.exchange = RS.compare = RS.exchange = 0;
    cout << "\t\t\t\twelcome!!!" << endl;
    cout << "是否要重新输入随机数(是Y,否N):" << endl;
    cin >> ch;
    if (ch == 'Y' || ch == 'y')
        RandomNum();
    cout << "请输入随机数的长度" << endl;
    cin >> len;

    CreatList(L, len);
    CreatList(LBIS, len);
    CreatList(LSIS, len);
    CreatList(LBS, len);
    CreatList(LQS, len);
    CreatList(LSS, len);
    CreatList(LHS, len);
    CreatList(LRS, len);
    cout << "排序数:" << endl;
    Print( L);

    cout << "插入排序:" << endl;
    InserSort(LSIS, SIS);
    Print(LSIS);
    cout << "折半插入排序:" << endl;
    BInsertSort(LBIS, BIS);
    Print(LBIS);
    cout << "起泡排序:" << endl;
    BubbleSort(LBS, BS);
    Print(LBS);
    cout << "快速排序:" << endl;
    QuickSort(LQS, QS);
    Print(LQS);
    cout << "选择排序:" << endl;
    SelectSort(LSS, SS);
    Print(LSS);
    cout << "堆排序:" << endl;
    HeapSort(LHS, HS);
    Print(LHS);
    cout << "基数排序:" << endl;
    RadixSort(LRS, RS);
    Print(LRS);
    cout << endl;
    cout << "插入排序的比较次数和交换次数为:" << SIS.compare << " " << SIS.exchange << endl;
    cout << "折半插入的比较次数和交换次数为:" << BIS.compare << " " << BIS.exchange << endl;
    cout << "起泡排序的比较次数和交换次数为:" << BS.compare << " " << BS.exchange << endl;
    cout << "快速排序的比较次数和交换次数为:" << QS.compare << " " << QS.exchange << endl;
    cout << "选择排序的比较次数和交换次数为:" << SS.compare << " " << SS.exchange << endl;
    cout << "用堆排序的比较次数和交换次数为:" << HS.compare << " " << HS.exchange << endl;
    cout << "基数排序的比较次数和交换次数为:" << RS.compare << " " << RS.exchange << endl;
    cout << endl;

    cout << "******************************特殊情况*******************************\n"<<endl;
    SIS.compare = SIS.exchange = BIS.compare = BIS.exchange = BS.compare
        = BS.exchange = QS.compare = QS.exchange = SS.compare = SS.exchange
        = HS.compare = HS.exchange = RS.compare = RS.exchange = 0;
    InserSort(LSIS, SIS);
    BInsertSort(LBIS, BIS);
    BubbleSort(LBS, BS);
    QuickSort(LQS, QS);
    SelectSort(LSS, SS);
    HeapSort(LHS, HS);
    RadixSort(LRS, RS);
    cout << "插入排序的比较次数和交换次数为:" << SIS.compare << " " << SIS.exchange << endl;
    cout << "折半插入的比较次数和交换次数为:" << BIS.compare << " " << BIS.exchange << endl;
    cout << "起泡排序的比较次数和交换次数为:" << BS.compare << " " << BS.exchange << endl;
    cout << "快速排序的比较次数和交换次数为:" << QS.compare << " " << QS.exchange << endl;
    cout << "选择排序的比较次数和交换次数为:" << SS.compare << " " << SS.exchange << endl;
    cout << "用堆排序的比较次数和交换次数为:" << HS.compare << " " << HS.exchange << endl;
    cout << "基数排序的比较次数和交换次数为:" << RS.compare << " " << RS.exchange << endl;

    system("pause");
    return 0;
}

源码下载地址

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值