Base sorting (using a linked list)
Based on the comparison between two algorithms, the lower bound of the algorithm is Ω (nlogN). To reduce the time complexity, you can only use other non-comparison-based methods. Base sorting is a method in which the time complexity is O (N)
In the process of base sorting, if there are four numbers, we use a linked list to connect them. These four numbers are
321 892 538 439
Step 1: first create 10 linked lists, L0 ~ Then, according to the number of four digits, connect them to the back of the corresponding linked list.
L0
L1 321
L2. 892
L3
L4
L5
L6
L7
L8 538
439
Step 2: connect to the linked list from L0 ~ The numbers of '9', we re-generate a new linked list in order.
321 892 538 439
Then we re-connect them to the back of each chain table based on the dozens of numbers.
L0
L1
L2. 321
L3 538 439
L4
L5
L6
L7
L8
892
Step 3: connect to the linked list from L0 ~ The numbers of '9', we re-generate a new linked list in order.
321 538 439 892
Then, based on the hundreds of digits, we re-connect them to the back of each chain table.
L0
L1
L2
L3 321
L4 439
L5 538
L6
L7
L8 892
Maid
Summary step: connect to the linked list from L0 ~ The numbers of '9', we re-generate a new linked list in order.
321 439 538 892
Now, the sorting result is complete.
The complete code is as follows:
(Implemented using a single-chain table)
# Include
Using namespace std; typedef struct Node {int data; struct Node * next;} LNode; void Initiate (LNode ** head) {(* head) = (LNode *) malloc (sizeof (LNode); (* head)-> next = NULL;} void Insert (LNode * head, int num) {if (head = NULL) return; else {LNode * p, * q; q = head; p = (LNode *) malloc (sizeof (LNode); p-> data = num; p-> next = NULL; while (q-> next! = NULL) {q = q-> next;} q-> next = p;} LNode * GetFirstNode (LNode * head) {if (head-> next = NULL) return NULL; else {LNode * p; p = head-> next; head-> next = p-> next; return p ;}} void AppendNode (LNode * head, LNode * node) {if (head = NULL) return; else {LNode * p; p = head; while (p-> next! = NULL) {p = p-> next;} p-> next = node; node-> next = NULL;} void Total (LNode * L, LNode * head) {LNode * p; p = L; while (p-> next! = NULL) {p = p-> next;} p-> next = head-> next;} int Power (int a, int n) {int y; if (n = 0) return 1; else {y = Power (a, n/2); y = y * y; if (n % 2 = 1) y = y * a;} return y;} int GetNum (LNode * p, int I) {int data = p-> data; int a; I --; a = data/Power (10, I); return a % 10 ;} // The second parameter indicates the maximum number of digits in the sorting integer. A total of count digits void radix_sort (LNode * head, int count) {LNode * p [10], * q; int I, j, k; for (j = 1; j <= count; j ++) {// initialize the for (I = 0; I <10; I ++) {Initiate (& p [I]) ;}// the linked list is scanned from start to end and To the node from the linked list. While (head-> next! = NULL) {q = GetFirstNode (head); k = GetNum (q, j); // obtain the element value kAppendNode (p [k], q); // connect the node to the corresponding position of the 10 linked lists} // connect the 10 linked lists from 0-9 to the back of the head node for (I = 0; I <10; I ++) {Total (head, p [I]) ;}} for (I = 0; I <10; I ++) {delete (p [I]) ;}} void printSL (LNode * head) {LNode * p; p = head-> next; while (p! = NULL) {cout <
Data <"; p = p-> next ;}} void main () {LNode * head; Initiate (& head); Insert (head, 1113 ); insert (head, 4212); Insert (head, 1232); Insert (head, 6533); Insert (head, 2332); Insert (head, 123); Insert (head,); Insert (head, 23); radix_sort (head, 4); // indicates the maximum number of digits of the integer to be sorted. A total of four digits: printSL (head); system ("pause ");}
The code for using a two-way linked list is attached:
# Include
# Define deusing namespace std; int power (int a, int n) // use recursion, O (logn), evaluate the Npower OF a {int y; if (n = 0) return 1; else {y = power (a, n/2); y = y * y; if (n % 2! = 0) y = a * y;} return y;} typedef struct Node {int key; struct Node * prior; struct Node * next;} SLnode; SLnode * create (int n) {SLnode * head, * p, * q; int I; if (head = (SLnode *) malloc (sizeof (SLnode ))) = NULL) {cout <"error"; exit (0); // exit if creation fails} head-> prior = head; head-> next = head; q = head; for (I = 0; I
> P-> key; p-> prior = q; q-> next = p; q = p;} head-> prior = q; q-> next = head; return head;} void printSL (SLnode * head) {SLnode * p; p = head-> next; while (p! = Head) {cout <
Key <"; p = p-> next ;}} SLnode * GetFirstNode (SLnode * head) {SLnode * p; p = head-> next; if (p! = Head) {p-> next-> prior = p-> prior; p-> prior-> next = p-> next;} else p = NULL; return p ;} int get_num (SLnode * p, int I) // obtain the number of I {int key = p-> key, num; num = key/power (10, I ); return num % 10;} void addNode (SLnode * head, SLnode * p) {p-> prior = head-> prior; p-> next = head; head-> prior-> next = p; head-> prior = p;} void append (SLnode * head, SLnode * Lhead) {if (Lhead-> next! = Lhead) // This judgment condition must be written, {Lhead-> next-> prior = head-> prior; Lhead-> prior-> next = head; head-> prior-> next = Lhead-> next; head-> prior = Lhead-> prior;} void radix_sort (SLnode * head, int n) // sort the internship base {SLnode * Lhead [10], * p; int I, j, k, f; for (I = 0; I <10; I ++) {Lhead [I] = (SLnode *) malloc (sizeof (SLnode); assert (Lhead [I]! = NULL) ;}for (I = 0; I
Next = Lhead [k]; Lhead [k]-> prior = Lhead [k];} while (head-> next! = Head) {p = GetFirstNode (head); // get first Node, and delete itj = get_num (p, I ); // obtain the number of I addNode (Lhead [j], p); // connect the obtained node to the end of Lhead [j]} for (f = 0; f <10; f ++) append (head, Lhead [f]) ;}for (I = 0; I <10; I ++) // delete a node delete (Lhead [I]);} void main () {int a, B; SLnode * head; cin> a; head = create (); printSL (head); cout <
> B; radix_sort (head, B); printSL (head); system ("pause ");}