基数排序法

基数排序

如果我们有N个整数,范围从1到M,(或从0到M-1),我们可以利用这个信息得到一种快速排序,叫做桶式排序。我们留置一个数组,称之为Count,大小为M,并初始化为0。于是,Count有M个单元,开始都是空的。当被读入时相应的桶加增一。所有输入被读入后扫描数组,打印出排好序的表。该算法花费O(M+N)。对于有p位的数,该排序的时间复杂度为O(P(M+N))。

链表设计

#include <iostream>
using namespace std;

typedef struct Node{
    int data;
    Node *next;
}*PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
int IsEmpty(List L){
    return L->next == nullptr;
}
int IsLast(Position P, List L){
    return P->next== nullptr;
}
Position Find(int x, List L){
    Position p;
    p=L->next;
    while(p!= nullptr&&p->data!=x){
        p=p->next;
    }
    return p;
}
Position FindPrevious(int x, List L){
    Position p;
    p=L;
    while(p->next!= nullptr&&p->next->data!=x){
        p=p->next;
    }
    return p;
}
void Delete(int x, List L){
    Position p;
    p=FindPrevious(x, L);
    if(!IsLast(p,L)){
        Position temp=p->next;
        p->next=temp->next;
        free(temp);
    }
}

void Insert(int x, List L, Position p){
    Position temp;
    temp = new Node;
    temp->data=x;
    temp->next=p->next;
    p->next=temp;
}

void DeleteList(List L){
    Position p;
    p=L->next;
    L->next= nullptr;
    while(p!= nullptr){
        Position temp;
        temp = p->next;
        delete(p);
        p=temp;
    }
}
Position Header(List L){
    return L;
}
Position First(List L){
    return L->next;
}
Position Advance(Position p){
    return p->next;
}
int Retrieve(Position p){
    return p->data;
}
void InsertFront(int x, List L){
    Insert(x, L, L);
}
void Insertback(int x, List L){
    Position last;
    last=L;
    while(last->next!= nullptr){
        last=last->next;
    }
    Insert(x,L,last);
}

List MakeEmpty(List L){
    if(L!= nullptr){
        DeleteList(L);
        return L;
    }
    else{
        L = new Node;
        L->next= nullptr;
        return L;
    }
}

/
//下面为具体实现代码,上面为常用的链表函数。
///
#define BUCKETS 10
#define N 10
#define BITS 3
void radixsort(int arr[]);
int getDigital(int x, int cnt);
void print(int arr[]);

int main(){
    int arr[N];
    int num;
    printf("Please input N nums: \n");
    for(int i=0;i<N;i++){
        cin>>num;
        arr[i]=num;
    }
    printf("The original nums are:");
    print(arr);
    printf("After sorted, the nums are:");
    radixsort(arr);
    print(arr);
}

int getDigital(int x, int cnt){
    for(int i =0; i<cnt; i++){
        x=x/10;
    }
    return x%10;
}

void print(int arr[]){
    for(int i =0;i<N;i++){
        printf("%d  ", arr[i]);
    }
    printf("\n");
}

void radixsort(int arr[]){
    List buckets[BUCKETS];
    int i,j,k;
    for(i=0;i<BUCKETS;i++){
        buckets[i]=MakeEmpty(nullptr);
    }
    for (i=0;i<BITS;i++){
        for (j=0;j<BUCKETS;j++){
            MakeEmpty(buckets[j]);
        }
        for(k=0;k<N;k++){
            Insertback(arr[k],buckets[getDigital(arr[k],i)]);
        }
        for (int i=0,k=0;i<N;i++){
            Position p;
            p=First(buckets[i]);
            while(p!= nullptr){
                arr[k++]=Retrieve(p);
                p=Advance(p);
            }
        }
    }
    for (i=0;i<BUCKETS;i++){
        DeleteList(buckets[i]);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值