zoukankan      html  css  js  c++  java
  • 王道数据结构 (23) 基数排序

     

     示例代码 :

    /**
     * 基数排序-单链表实现
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define Error(Str) FatalError(Str)
    #define FatalError(Str) fprintf(stderr, "%s
    ", Str), exit(1)
    
    #define N 10      //数个数
    #define RADIX 10  //基数
    #define POS_LEN 3 //位长
    
    typedef struct Node
    {
      int data;
      struct Node *next;
    } * pNode;
    typedef pNode LNode;
    
    pNode create();
    int get_num_pos(int num, int pos, int radix);
    void radix_sort(pNode collect, int radix, int pos_len);
    void append_node(pNode L, int num);
    void insert(pNode P, int num);
    void delete_list(pNode L);
    void test();
    void print(pNode L);
    
    int main(void)
    {
      test();
      return 0;
    }
    
    void radix_sort(pNode collect, int radix, int pos_len)
    {
      // collect assign
      LNode assign[radix - 1], P, tmp, p;
      int i, num;
      for (i = 0; i < radix; i++)
      {
        assign[i] = create();
      }
      for (i = 1; i <= pos_len; i++)
      {
        P = collect;
        while (NULL != P->next)
        {
          p = P->next;
          P->next = p->next;
          p->next = NULL;
          int num = get_num_pos(p->data, i, radix);
          tmp = assign[num];
          while (NULL != tmp->next)
          {
            tmp = tmp->next;
          }
          tmp->next = p;
        }
        printf("第%d次分配
    ", i);
        for (int j = 0; j < radix; j++)
        {
          printf("N-%d	", j);
          print(assign[j]);
        }
    
        //assign
        P = collect;
        for (int j = 0; j < radix; j++)
        {
          LNode phead;
          phead = assign[j];
          while (NULL != phead->next)
          {
            p = phead->next;
            phead->next = p->next;
            p->next = NULL;
            P->next = p;
            P = P->next;
          }
        }
        printf("第%d次收集
    ", i);
        print(collect);
        // 置空
        // for (int j = 0; j < radix; j++) {
        //  delete_list(assign[j]);
        // }
      }
      // free space
      for (i = 0; i < radix; i++)
      {
        delete_list(assign[i]);
      }
    }
    
    int get_num_pos(int num, int pos, int radix)
    {
      int temp = 1, i;
      for (i = 0; i < pos - 1; i++)
        temp *= radix;
      return (num / temp) % radix;
    }
    void insert(pNode P, int num)
    {
      pNode temp;
      temp = (pNode)malloc(sizeof(struct Node));
      if (NULL == temp)
        FatalError("out of space");
      temp->data = num;
      temp->next = NULL;
      P->next = temp;
    }
    
    void append_node(pNode L, int num)
    {
      pNode temp, P;
      P = L;
      temp = (pNode)malloc(sizeof(struct Node));
      if (NULL == temp)
        FatalError("out of space");
      temp->next = NULL;
    
      while (NULL != P->next)
      {
        P = P->next;
      }
      P->next = temp;
    }
    
    pNode create()
    {
      pNode L;
      L = (pNode)malloc(sizeof(struct Node));
      if (NULL == L)
        FatalError("out of space");
      L->next = NULL;
      return L;
    }
    
    void delete_list(pNode L)
    {
      pNode P, temp;
      P = L->next;
      L->next = NULL;
      while (NULL != P)
      {
        temp = P->next;
        free(P);
        P = temp;
      }
    }
    
    void print(pNode L)
    {
      pNode P;
      P = L->next;
      while (NULL != P)
      {
        printf("%d	", P->data);
        P = P->next;
      }
      printf("
    ");
    }
    
    void test()
    {
      pNode source, tmp, P;
      int arr[N] = {
          278,
          109,
          63,
          930,
          589,
          184,
          505,
          269,
          8,
          83};
      int i;
      int max = 1;
      for (i = 0; i <= POS_LEN - 1; i++)
        max *= RADIX;
      source = create();
      P = source;
      srand((unsigned)time(NULL));
      for (i = 0; i < N; i++)
      {
        insert(P, arr[i]);
        P = P->next;
      }
      print(source);
      radix_sort(source, RADIX, POS_LEN);
      printf("结果
    ");
      print(source);
    }

    运行结果 :

    越努力越幸运
  • 相关阅读:
    小总结
    使用Bind提供域名解析服务(正向解析)
    在虚拟机上的NFS网络文件系统
    在虚拟机上的FTP FTP访问模式(虚拟用户模式)
    在虚拟机上的FTP FTP访问模式(本地用户模式)
    在虚拟机上的FTP,FTP访问模式(匿名)
    在虚拟机上的关于Apache(阿帕奇)(5)基于端口访问网站
    在虚拟机上的关于Apache(阿帕奇)(4)基于域名访问网站
    在虚拟机上的关于Apache(阿帕奇)(3)基于IP访问网站
    在虚拟机上的关于Apache(阿帕奇)(2)开启个人用户主页功能
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/13541511.html
Copyright © 2011-2022 走看看