Sorting Algorithm-base sorting (RadixSort (L, max) single-chain table version, radixsort single-chain
Reprint http://blog.csdn.net/shayabean_/article/details/44885917blog
Let's talk about the base sortingThoughts:
Base sorting is a non-Comparative sorting algorithm. The principle is to cut integers into different digits by the number of digits, and then compare them by each digit.
Unify all the values to be compared (positive integers) into the same length of digits, and add zero before the number of shorter digits. Then, sort the data by bit. In each sort, the array elements are placed in the corresponding
In the bucket, and then put the elements from the bucket 0 to the bucket 9 into the array in the first-in-first-out mode. In this way, the sequence is changed to an ordered sequence after the ranking is completed until the sorting is completed by the highest bit.
In this version, the base sorting RadixSort (L, max) is different from RadixSort (L) in that the maximum number of digits in the column to be sorted needs to be entered. Because the maximum number of RadixSort (L) digits has been calculated in the program, because the maximum number needs to be calculated, it is necessary to take the first cycle of the chain table, so RadixSort (L, max) faster than RadixSort (L.
This blog includes four files, two header files RadixSort. h and fatal. h, one library function RadixSort. c, and one test file Test_Radix_Sort.c.
Header fileFatal. h:
1 #include<stdio.h>2 #include<stdlib.h>3 #define Error(Str) FatalError(Str)4 #define FatalError(Str) fprintf(stderr, "%s\n", Str), exit(1);
Header fileRadixSort. h:
1 typedef int ElementType; 2 # ifndef RADIX_SORT_H 3 # define RADIX_SORT_H 4 5 # include <stdbool. h> 6 # define ListEmpty-2 7 8 struct Node; 9 typedef struct Node * PtrToNode; 10 typedef PtrToNode List; 11 typedef PtrToNode Position; 12 13 List MakeEmpty (List L ); 14 bool IsEmpty (List L); 15 bool IsLast (Position P, List L); 16 Position Header (List L); 17 Position Advance (Position P); 18 ElementType Retrieve (Posi Tion P); 19 void DeleteList (List L); 20 void PrintList (const List L); 21 void Insert (ElementType X, List L, Position P ); 22 void MoveNode (List L1, List L2); // move the header node in Table L2 to the End Node 23 void RadixSort (List L, int max) of L1 ); // The final base sorting function, input the linked list L, and sort the L to get the new sorting linked list L. max is the maximum number of digits of the elements to be sorted 24 # endif //! RADIX_SORT_H
Here, RadixSort is the final sorting function, which can be sorted by calling it.
Library functionsRadixSort. c
1 # include "RadixSort. h "2 # include <stdio. h> 3 # include <stdlib. h> 4 # include <malloc. h> 5 # include <math. h> 6 # include "fatal. h "7 struct Node 8 {9 ElementType Element; 10 Position Next; 11}; 12 13 // initialize the linked List 14 List MakeEmpty (List L) 15 {16 if (L! = NULL) 17 DeleteList (L); // if the linked list is not empty, delete the linked list 18 L = malloc (sizeof (struct Node); 19 if (L = NULL) 20 FatalError ("Out of memory! "); 21 L-> Next = NULL; 22 return L; 23} 24 // determines whether the linked List is empty. 25 bool IsEmpty (List L) 26 {27 return L-> Next = NULL; 28} 29 30 // determine whether the current pointer P points to the last element of the linked List 31 bool IsLast (Position P, List L) 32 {33 return P-> Next = NULL; 34} 35 36 // get the head of the linked List 37 Position Header (List L) 38 {39 return L; 40} 41 42 // obtain the Next Position of Position P 43 Position Advance (Position P) 44 {45 return P-> Next; 46} 47 48 // extract the value of position P in the structure 49 ElementType Retr Ieve (Position P) 50 {51 return P-> Element; 52} 53 54 // Delete the linked List 55 void DeleteList (List L) 56 {57 Position P, Temp; 58 P = L-> Next; 59 L-> Next = NULL; 60 while (P! = NULL) 61 {62 Temp = P-> Next; 63 free (P); 64 P = Temp; 65} 66} 67 68 // print the linked List 69 void PrintList (const List L) 70 {71 Position P = Header (L); 72 if (IsEmpty (L )) 73 printf ("Empty list \ n"); 74 else 75 {76 do 77 {78 P = Advance (P); 79 printf ("% d ", retrieve (P); 80} while (! IsLast (P, L); 81 printf ("\ n"); 82} 83} 84 85 // Insert element X to 86 void Insert (ElementType X, list L, Position P) 87 {88 Position TmpCell; 89 TmpCell = malloc (sizeof (struct Node); 90 if (TmpCell = NULL) 91 FatalError ("Out of Space !!! "); 92 TmpCell-> Element = X; 93 TmpCell-> Next = P-> Next; 94 P-> Next = TmpCell; 95} 96 97 void MoveNode (List L1, list L2) 98 {99 // move the header node in Table L2 to the End Node of L1 100 Position Tmp1 = L1; 101 Position Tmp2; 102 if (IsEmpty (L2 )) exit (ListEmpty); 103 while (! IsLast (Tmp1, L1) 104 Tmp1 = Tmp1-> Next; // point Tmp1 to the end of L1 table 105 Tmp2 = L2-> Next; 106 L2-> Next = Tmp2-> Next; 107 Tmp1-> Next = Tmp2; 108 Tmp2-> Next = NULL; 109} 110 111 void RadixSort (List L, int max) 112 {113 // if (IsEmpty (L) return L; // if the linked list L to be sorted is an empty table, 114 int I, j, TmpSub is not sorted; // Tmpsub stores a few digits, ten digits, and hundreds of digits. 115 ElementType FirstElement; // the first element of the storage chain table is 116 117 List Bucket [10]; // It opens up 10 buckets, 0 ~ 9118 for (I = 0; I <10; I ++) Bucket [I] = MakeEmpty (NULL); // initialize 10 buckets, each array is a chain table 119 for (I = 0; I <max; I ++) // start to extract the single, ten, and hundreds of digits of each digit 120 {121 while (! IsEmpty (L) // when the elements in L are stripped, the loop ends at 122 {123 FirstElement = L-> Next-> Element; // retrieve the data of the first node 124 TmpSub = (int) (FirstElement/pow (10, I) % 10; // extract the 100th digit 125 MoveNode (Bucket [TmpSub], L) in sequence; // move the nodes in L to the corresponding Bucket 126} 127 for (j = 0; j <10; j ++) // move the number in the bucket to 128 {129 while (! IsEmpty (Bucket [j]) MoveNode (L, Bucket [j]); 130} 131} 132 for (I = 0; I <10; I ++) free (Bucket [I]); // release 10 buckets 133}
Test function Test_Radix_Sort.c
1 # include <stdio. h> 2 # include "RadixSort. h "3 # include" fatal. h "4 # include <time. h> 5 6 int main () 7 {8 int amount; 10 List L; Position P; 11 L = MakeEmpty (NULL); // initialize the Linked List 12 P = L; 13 if (L = NULL) Error ("Out of Space !!! "); 14 printf (" randomly generated digits: "); 15 scanf_s (" % d ", & amount); 16 srand (unsigned) time (NULL )); 17 for (int I = 0; I <amount; I ++) 18 {19 Insert (rand () % 10000, L, P); 20 P = Advance (P ); 21} 22 printf ("results before sorting:"); 23 PrintList (L); 24 RadixSort (L, 4 ); // call the sorting function to sort 25 printf ("result after base sorting:"); 26 PrintList (L); 27}