箱子排序(桶排序),基數排序

#include <iostream>
#include <list>
#include <time.h>
//#include "circularListWithHeader.h"
#include "extendedChain.h"
#include <math.h>
/*本算法爲箱子排序的算法
 * Author:  zailushang
 * Data:    2020-06-27
 *
 *目的爲了熟悉C++的雙向鏈表
 *
 *
 */
#define BUFFSIZE 1000000
#define RADIX   100
struct studentRecord{//默認爲公有的成員
    int sort;
    int number;
    std::string *name;
    int operator!=(const studentRecord &rht)const{
        return this->sort != rht.sort;
    }
    operator int()const{//賦給一個int的時候,賦予的是sort值
        return sort;
    }
    studentRecord():sort(0),name(NULL){}
    studentRecord(int sorts,std::string *names):sort(sorts),name(names){}
};
void binSort(std::list<studentRecord> &theList,int range){
    std::list<studentRecord> *p = new std::list<studentRecord>[range+1];//建立一個數組空間,用來保存各個鏈表
    clock_t t = clock();
    for(auto it = theList.begin();it != theList.end();it = theList.begin()){//使用迭代器去訪問元素
        p[it->sort].push_back(*it);//將對應的分數報錯到相應的鏈表中;時間複雜度
        theList.erase(it);
    }
    for(int i = 0; i <= range;++i){
        for(auto it = p[i].begin(); it != p[i].end();it=p[i].begin()){
            theList.push_back(*it);
            p[i].erase(it);
        }
    }
    delete []p;
    std::cout<<"the bin sort time is "<<float(clock()-t)/CLOCKS_PER_SEC;
}
void binSortByList(extendedChain<studentRecord> &theList,int range){
    extendedChain<studentRecord> *p = new extendedChain<studentRecord>[range+1];
    auto listSize = theList.mychain<studentRecord>::size();
    for(int i = 0; i != listSize;++i){
        studentRecord x = theList.get(0);
        theList.erase(0);
        p[x.sort].insert(0,x);
    }
    for(int j = range;j >= 0;--j){
        while(!p[j].empty()){
            studentRecord x = p[j].get(0);
            p[j].erase(0);
            theList.insert(0,x);
        }
    }
    delete []p;
}
void binSortUsedByRadix(extendedChain<studentRecord> &theList,int c){
    int listSize = theList.mychain<studentRecord>::size();
    extendedChain<studentRecord> *p = new extendedChain<studentRecord>[listSize+1];
    int number = 1,anotherNUmber=1;
    for(int i = 0; i != c;++i)
        number*=RADIX;
    anotherNUmber = number/RADIX;
    for(int i = 0; i != listSize;++i){
        studentRecord x = theList.get(0);
        theList.erase(0);
        p[((x.sort)%number)/anotherNUmber].insert(0,x);//這個是比較數
    }
    for(int j = listSize;j >= 0;--j){
        while(!p[j].empty()){
            studentRecord x = p[j].get(0);
            p[j].erase(0);
            theList.insert(0,x);
        }
    }
    delete []p;
}

void radixSort(extendedChain<studentRecord> &theList,int range){
    int listSize  = theList.mychain<studentRecord>::size();
    std::cout<<"\nthe radix is "<<RADIX<<std::endl;
    int c = 1;
    extendedChain<studentRecord> *p = new extendedChain<studentRecord>[listSize+1];
    while(pow(RADIX,c) < range)
        c++;//獲得指數的次方
    std::cout<<"\nthe radix is "<<c<<std::endl;
    for(int i = 0;i != c;i++){
        int number = 1,anotherNUmber=1;
        for(int j = 0;j != c;++j)
            number*=RADIX;
        anotherNUmber = number/RADIX;
        for(int k = 0; k != listSize;++k){
            studentRecord x = theList.get(0);
            theList.erase(0);
            p[((x.sort)%number)/anotherNUmber].insert(0,x);//這個是比較數
        }
        for(int j = listSize;j>= 0;--j){
            while(!p[j].empty()){
                studentRecord x = p[j].get(0);
                p[j].erase(0);
                theList.insert(0,x);
            }
        }
         //binSortUsedByRadix(theList,i+1);
    }
    delete []p;
}
int main(){
    std::list<studentRecord> listStduent;
    extendedChain<studentRecord> test1,test2,test3;    
    std::string s("test");
        clock_t t = clock();
    auto it = listStduent.begin();
    for(int i = 0;i != BUFFSIZE;++i,it = listStduent.begin())
        listStduent.insert(it,studentRecord(i,&s));
        //listStduent.push_front(studentRecord(i,&s));
    std::cout<<"use STL push_front insert "<<BUFFSIZE<<" numbers need time is "<<float(clock()-t)/CLOCKS_PER_SEC<<"\n";
        std::cout<<"\n********************************\n";
    clock_t t3 = clock();
    for(int i = 0;i != BUFFSIZE/1000;++i)
        test1.insert(0,studentRecord(i,&s));
    std::cout<<"use myself push_front insert "<<BUFFSIZE<<" numbers need time is "<<float(clock()-t3)/CLOCKS_PER_SEC<<"\n";
    
    std::cout<<"\n*********************\n";
    binSort(listStduent,BUFFSIZE);
    std::cout<<"\n*******************";
    /*
    for(auto it = listStduent.begin();it != listStduent.end();++it)
        std::cout<<(*it).sort<<" ";
    */
    /*
     * 冒泡排序
    clock_t t4 = clock();
    test2.bubbleSort();
    std::cout<<"\nuse myself bubbleSort "<<BUFFSIZE<<" numbers need time is "<<float(clock()-t4)/CLOCKS_PER_SEC<<"\n";
    */
    for(int i = 0; i != BUFFSIZE/1000;++i)
        test2.insert(0,studentRecord(i,&s));

    std::cout<<"\n*****************************\n";
    clock_t t5 = clock();
    binSortByList(test1,BUFFSIZE);
    std::cout<<"\nuse the list By the function that the time is "<<float(clock()-t5)/CLOCKS_PER_SEC;
    std::cout<<"\n*****************************";
    std::cout<<"\n******************************\n";
    clock_t t6 = clock();
    test2.binSort(BUFFSIZE);
    std::cout<<"\nuse the binSort of the member in class that  the time is "<<float(clock()-t6)/CLOCKS_PER_SEC;
    /*
     * for(auto it = test2.begin();it != test2.end();++it)
        std::cout<<(*it).sort<<" ";
    */
    
    for(int i = 0; i != BUFFSIZE/1000;++i)
        test3.insert(0,studentRecord(i,&s));

    
    std::cout<<"\n******************************";
    clock_t t7 = clock();
    radixSort(test3,BUFFSIZE);
    std::cout<<"\nuse the binSort of the member in class that  the time is "<<float(clock()-t7)/CLOCKS_PER_SEC;
    std::cout<<"\n******************************\n";
   
    return 0;
}
/*
 經過上述的測試比較,發現STL官方給的雙向鏈表不如自己寫的單向鏈表的插入和箱子排序速度快,因爲主要是插入的時候需要兩個
 指針進行要轉換,肯定會比單鏈錶慢。
 冒泡排序比箱子排序法慢了不止一點兩點(在大數據的情況下)
 */

這裏說明一點,基數排序桶排序的一個優化。但是如果輸入範圍和你的桶的個數一樣的時候,基數排序的優勢就沒有那麼大了。
記住:雖然基數排序的時間複雜度雖然有O(n),桶排序有O(n+range)。但是在實際中,在某種情況下,基數排序不一定比桶排序快。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章