8大排序

3 篇文章 0 订阅
3 篇文章 0 订阅

代码:

ArrayDefine.h
#ifndef _array_define_h_v_1_0_
#define _array_define_h_v_1_0_

#include <stdio.h>

#define ARRAYLEN    1000000

int InitArray(int* arrayname)
{
    FILE* f = fopen("data.dat","rb");//文件中有1000000个整数
    int b = fread(arrayname,sizeof(int),ARRAYLEN,f);
    fclose(f);
    return 1;
}

int UninitArray(int* const arrayname)
{
    delete[] arrayname;
    return 1;
}

#define INIT_ARRAY(name) int* name = new int[ARRAYLEN]; int n_##name##_init = InitArray(name);

#define UNINIT_ARRAY(name)	int n_##name##_uninit = UninitArray(name);

#endif//_array_define_h_v_1_0_
sortfunction.h
#ifndef _sort_function_h_v_1_0_
#define _sort_function_h_v_1_0_

class sortfunction
{
public:
    static bool check_is_sorted(const int* const checkarray, int count);
    //冒泡排序
    static void sort_bubble (int* const sortarray, int count);
    //快速排序
    static void sort_quick  (int* const sortarray, int count);
    //插入排序
    static void sort_insert (int* const sortarray, int count);
    //希尔排序
    static void sort_shell  (int* const sortarray, int count);
    //选择排序
    static void sort_select (int* const sortarray, int count);
    //堆排序
    static void sort_heap   (int* const sortarray, int count);
    //归并排序
    static void sort_merge  (int* const sortarray, int count);
    //基数排序
    static void sort_radix  (int* const sortarray, int count);

private:

    static int  sort_quick_getdevision_l      (int* const sortarray, int l, int r);

    static int  sort_quick_getdevision_r      (int* const sortarray, int l, int r);

    static void sort_quick_recursion          (int* const sortarray, int l, int r);

    static void sort_heap_adjust_heap         (int* const sortarray, int count, int hnode);

    static void sort_merge_merge_array        (int* const sortarray,int l, int m, int r);

    static void sort_merge_recursion          (int* const sortarray, int l, int r);

    static int  sort_radix_get_max_digit_count(int* const sortarray, int count);

    static int  sort_radix_get_digit          (int num, int digit);

};

#endif//_sort_function_h_v_1_0_
sortfunction.cpp
#include "sortfunction.h"

#include <string.h>
#include <math.h>

bool sortfunction::check_is_sorted(const int* const checkarray, int count)
{
    for(int i = 0; i < count-1; ++i)
    {
        if(checkarray[i] > checkarray[i+1])
        {
            return false;
        }
    }
    return true;
}

void sortfunction::sort_bubble  (int* const sortarray, int count)
{
    for(int i = 0; i < count; ++i)
    {
        for(int j = 0; j < count - i -1; ++j)
        {
            if(sortarray[j]>sortarray[j+1])
            {
                int tmp = sortarray[j];
                sortarray[j] = sortarray[j+1];
                sortarray[j+1] = tmp;
            }
        }
    }
}

void sortfunction::sort_quick   (int* const sortarray, int count)
{
    sort_quick_recursion(sortarray,0,count-1);
}

void sortfunction::sort_insert  (int* const sortarray, int count)
{
    for(int i = 0; i < count; ++i)
    {
        for(int j = 0; j < i; ++j)
        {
            if(sortarray[i]<sortarray[j])
            {
                int tmp = sortarray[i];
                memmove(&sortarray[j+1],&sortarray[j],sizeof(int)*(i-j));
                sortarray[j] = tmp;
            }
        }
    }
}

void sortfunction::sort_shell   (int* const sortarray, int count)
{
    for(int G = count / 2; G >0; G /= 2)
    {
        for(int i = G; i < count; ++i)
        {
            int j = 0;
            int tmp = sortarray[i];
            for(j = i - G; j >=0 && tmp < sortarray[j]; j = j - G)
            {
                sortarray[j + G] = sortarray[j];
            }
            sortarray[j + G] = tmp;
        }
    }
}

void sortfunction::sort_select  (int* const sortarray, int count)
{
    for(int i = 0; i < count; ++i)
    {
        int minidx = 0;
        for(int j = 1; j < count - i; ++j)
        {
            if(sortarray[j] > sortarray[minidx])
            {
                minidx = j;
            }
        }
        if((count - i -1) != minidx)
        {
            int tmp = sortarray[minidx];
            sortarray[minidx] = sortarray[count - i -1];
            sortarray[count - i -1] = tmp;
        }
    }
}

void sortfunction::sort_heap    (int* const sortarray, int count)
{
    for(int i = 0; i < count; ++i)
    {
        sort_heap_adjust_heap(sortarray,count-i,0);
        int tmp = sortarray[0];
        sortarray[0] = sortarray[count - i -1];
        sortarray[count - i -1] = tmp;
    }
}

void sortfunction::sort_merge   (int* const sortarray, int count)
{
    sort_merge_recursion(sortarray, 0, count);
}

void sortfunction::sort_radix   (int* const sortarray, int count)
{
    int radix = 10;
    int max_digit_count = sort_radix_get_max_digit_count(sortarray,count);
    int *sortbucket  = new int[count];
    int *bucketcount = new int[radix+1];
    for(int i = 0; i < max_digit_count; ++i)
    {
        memset(bucketcount,0,sizeof(int)*(radix+1));
        for(int j = 0; j < count; ++j)
        {
            ++bucketcount[sort_radix_get_digit(sortarray[j],i)+1];
        }
        for(int j = 1; j < radix+1; ++j)
        {
            bucketcount[j] += bucketcount[j-1];
        }
        for(int j = 0; j < count; ++j)
        {
            sortbucket[bucketcount[sort_radix_get_digit(sortarray[j],i)]++] = sortarray[j];
        }
        memmove(sortarray,sortbucket,sizeof(int)*count);
    }
    delete[] sortbucket;
    delete[] bucketcount;
}

int sortfunction::sort_quick_getdevision_l      (int* const sortarray, int l, int r)
{
    int devisionnum = sortarray[l];
    while(l < r)
    {
        while(l < r && sortarray[r] >= devisionnum)
        {
            --r;
        }
        sortarray[l] = sortarray[r];
        while(l < r && sortarray[l] <= devisionnum)
        {
            ++l;
        }
        sortarray[r] = sortarray[l];
    }
    sortarray[l] = devisionnum;
    return l;
}

int sortfunction::sort_quick_getdevision_r      (int* const sortarray, int l, int r)
{
    int devisionnum = sortarray[r];
    while(l < r)
    {
        while(l < r && sortarray[l] <= devisionnum)
        {
            ++l;
        }
        sortarray[r] = sortarray[l];
        while(l < r && sortarray[r] >= devisionnum)
        {
            --r;
        }
        sortarray[l] = sortarray[r];
    }
    sortarray[r] = devisionnum;
    return r;
}

void sortfunction::sort_quick_recursion         (int* const sortarray, int l, int r)
{
    if(l < r)
    {
        //以下3行两种写法需要对应,不然会出现死循环
        int devision = sort_quick_getdevision_l(sortarray,l,r);    //int devision = getdevision_r(sortarray,l,r); 
        sort_quick_recursion(sortarray,l,devision);                 //sort_quick_recursion(sortarray,l,devision-1);  
        sort_quick_recursion(sortarray,devision+1,r);               //sort_quick_recursion(sortarray,devision,r);
    }
}

void sortfunction::sort_heap_adjust_heap        (int* const sortarray, int count, int hnode)
{
    int inodechild_l = 2*hnode+1, inodechild_r = inodechild_l+1;
    if(inodechild_l < count)
    {
        sort_heap_adjust_heap(sortarray,count,inodechild_l);
        if(sortarray[inodechild_l] > sortarray[hnode])
        {
            int tmp = sortarray[inodechild_l];
            sortarray[inodechild_l] = sortarray[hnode];
            sortarray[hnode] = tmp;
        }
    }
    if(inodechild_r < count)
    {
        sort_heap_adjust_heap(sortarray,count,inodechild_r);
        if(sortarray[inodechild_r] > sortarray[hnode])
        {
            int tmp = sortarray[inodechild_r];
            sortarray[inodechild_r] = sortarray[hnode];
            sortarray[hnode] = tmp;
        }
    }
}

void sortfunction::sort_merge_merge_array       (int* const sortarray,int l, int m, int r)
{
    int* temparray = new int[r-l];
    int i = l, j = m, tmpi = 0;
    while(tmpi < r-l)
    {
        while(i < m && tmpi < r-l && (j>=r?1:sortarray[i] <= sortarray[j]))
        {
            temparray[tmpi++] = sortarray[i++];
        }
        while(j < r && tmpi < r-l && (i>=m?1:sortarray[j] <= sortarray[i]))
        {
            temparray[tmpi++] = sortarray[j++];
        }
    }
    memmove(sortarray+l,temparray,sizeof(int)*(r-l));
    delete[] temparray;
}

void sortfunction::sort_merge_recursion         (int* const sortarray, int l, int r)
{
    if(r-l>1)
    {
        sort_merge_recursion(sortarray,l,(l+r)/2);
        sort_merge_recursion(sortarray,(l+r)/2,r);
        sort_merge_merge_array(sortarray,l,(l+r)/2,r);
    }
}

int sortfunction::sort_radix_get_max_digit_count(int* const sortarray, int count)
{
    int dight = 0;
    int maxnum = sortarray[0];
    for(int i = 1; i < count; ++i)
    {
        if(sortarray[i]>maxnum)
        {
            maxnum = sortarray[i];
        }
    }
    for(; maxnum>0; ++dight, maxnum /= 10);
    return dight;
}

int sortfunction::sort_radix_get_digit          (int num, int digit)
{
    return num/int(pow(10.0f,digit))%10;
}
main.cpp
#include "sortfunction.h"
#include "ArrayDefine.h"

#include <assert.h>

int main()
{
    INIT_ARRAY(array_1);
    INIT_ARRAY(array_2);
    INIT_ARRAY(array_3);
    INIT_ARRAY(array_4);
    INIT_ARRAY(array_5);
    INIT_ARRAY(array_6);
    INIT_ARRAY(array_7);
    INIT_ARRAY(array_8);

    sortfunction::sort_bubble(array_1,ARRAYLEN);
    assert(sortfunction::check_is_sorted(array_1,ARRAYLEN));
    sortfunction::sort_quick(array_2,ARRAYLEN);
    assert(sortfunction::check_is_sorted(array_2,ARRAYLEN));
    sortfunction::sort_insert(array_3,ARRAYLEN);
    assert(sortfunction::check_is_sorted(array_3,ARRAYLEN));
    sortfunction::sort_shell(array_4,ARRAYLEN);
    assert(sortfunction::check_is_sorted(array_4,ARRAYLEN));
    sortfunction::sort_select(array_5,ARRAYLEN);
    assert(sortfunction::check_is_sorted(array_5,ARRAYLEN));
    sortfunction::sort_heap(array_6,ARRAYLEN);
    assert(sortfunction::check_is_sorted(array_6,ARRAYLEN));
    sortfunction::sort_merge(array_7,ARRAYLEN);
    assert(sortfunction::check_is_sorted(array_7,ARRAYLEN));
    sortfunction::sort_radix(array_8,ARRAYLEN);
    assert(sortfunction::check_is_sorted(array_8,ARRAYLEN));

    UNINIT_ARRAY(array_1);
    UNINIT_ARRAY(array_2);
    UNINIT_ARRAY(array_3);
    UNINIT_ARRAY(array_4);
    UNINIT_ARRAY(array_5);
    UNINIT_ARRAY(array_6);
    UNINIT_ARRAY(array_7);
    UNINIT_ARRAY(array_8);

    return 1;
}
说明:
堆排序中是否每次调整都需要将堆调整成一个最大(小)堆?其实在以上的代码中对于堆排序,并没有每次调整都构建出一个最大堆,只是保证了堆结构中的根节点数值最大,一样可以达到排序正确的目的。
附:
测试程序中的data.dat文件是自己生成用以测试的,生成文件代码如下:
generate_integer_array_file.cpp
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char** argv)
{
    if(argc != 3)
    {
        printf("usage: cmd is %s number outputfile.\n    eg. \"%s 10000 data.dat\" will generate 10000 random integers and write them to file \'data.dat\'.",*(argv+0),*(argv+0));
        return 0;
    }
    srand(time(NULL));
    int numcount = atoi(*(argv+1));
    FILE* f = fopen(*(argv+2),"wb");
    if(f)
    {
        for(int i = 0; i < numcount; ++i)
        {
            int randnum = rand()%(numcount*10);
            fwrite(&randnum,sizeof(int),1,f);
        }
        fclose(f);
    }
    return 1;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值