Look at the data structure write code (65) Cardinal sort

Source: Internet
Author: User

Please note that the code is insufficient

Reference book: MIN data structure. C language Edition "

RadixSort.cpp: Defines the entry point of the console application. #include "stdafx.h" #define MAX_SIZE 1000//Maximum space # define RADIX 10//keyword cardinality # key_num 3//keyword number struct slnode{// Static linked list node int key;int next;}; struct sllist{//static linked list Slnode base[max_size];int keynum;//keyword number int len;}; typedef int ARRTYPE [radix];//pointer array void initlist (Sllist * list,int * array,int num) {for (int i = 0; i < num; i++) {list-& Gt;base[i].next = i + 1;list->base[i+1].key = array[i];} List->base[num].next = 0;list->len = Num;list->keynum = 3;} int ord (int key,int i) {//Get address (0~9) if (i = = 0) {return key% 10;} else if (i = = 1) {return key% 100/10;} Else{return key/100;}}  Allocation function//i: number of keywords//f: Team head pointer array//e: tail pointer array void Distribute (Sllist * list,int i,arrtype * F,arrtype * e) {for (int j = 0; J < RADIX; J + +) (*f) [j] = 0;//Empty team Header data int next = List->base[0].next;for (; next = = 0; next = list->base[next].next) {int index = O RD (List->base[next].key,i), if ((*f) [index] = = 0) {(*f) [index] = next;} else{list->base[(*e) [Index]].next = Next;} (*e) [Index] = next;} printf ("\ n");} int GetNext (Arrtype f,int i) {for (; i < RADIX; i++) {if (f[i]! = 0) {return i;}} return-1;}  The collection function (the allocation function already blocks the array that needs to be sorted and now needs to link the blocks) void Collect (Sllist * List,arrtype f,arrtype e) {int first = GetNext (f,0); if (First! =-1) {List->base[0].next = f[first];//sets the successor int of the head pointer next = GetNext (f,first+1); int pre = FIRST;FOR (; next! =-1;) {list->base[e[pre]].next = F[next];p re = Next;next = GetNext (f,next+1);} List->base[e[pre]].next = 0;//Set the successor of the last node to 0}}void printlist (sllist list) {printf ("------------------ Print static list--------------------\ n "), int next = List.base[0].next;while (next! = 0) {Slnode nextnode = List.base[next];p rintf ( "%d\t", nextnode.key); next = Nextnode.next;} printf ("\ n");} void Radixsort (Sllist * list) {Arrtype F; Arrtype e;for (int i = 0; i < list->keynum; i++) {Distribute (list,i,&f,&e);//assignment, time complexity O (n), n is the size of the sorted array collect (list,f,e);//Collect ... printlist (*list);}} static int testarray[] = {656,301,305,998,999,875,832,423,432,565};int _tmain (int argc, _tchar* argv[]) {sllist list;initlist (&list,testarray,10); Radixsort (&list); return 0;} 
Run:

Printed 3 static linked lists, respectively, for the "single digit", "10 bits", "hundred" allocation, after the collection of static linked list situation.



Look at the data structure write code (65) Cardinal sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.