Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concepts of Algorithms CSC-244 Unit 11 & 12 Sorting (Radix Sort) Shahid Iqbal Lone Computer College Qassim University K.S.A.

Similar presentations


Presentation on theme: "Concepts of Algorithms CSC-244 Unit 11 & 12 Sorting (Radix Sort) Shahid Iqbal Lone Computer College Qassim University K.S.A."— Presentation transcript:

1 Concepts of Algorithms CSC-244 Unit 11 & 12 Sorting (Radix Sort) Shahid Iqbal Lone Computer College Qassim University K.S.A.

2 CSC 244 Concepts of Algorithms2 Radix Sort Radix Sort is a clever and little sorting algorithm. Radix Sort puts the elements in order by using the digits of the numbers/values. This sort is unusual because it does not directly compare any of the elements We instead create a set of buckets and repeatedly separate the elements into the buckets On each pass, we look at a different part of the elements

3 Radix Sort Assuming decimal elements and 10 buckets, we would put the elements into the bucket associated with its units digit The buckets are actually queues so the elements are added at the end of the bucket At the end of the pass, the buckets are combined in increasing order I will explain it with an example on next slid. CENG 213 Data Structures3

4 Radix Sort (Continued) 4CSC 244 Concepts of Algorithms Consider the following 9 numbers/values: 493 812 715 710 195 437 582 340 385 We should start sorting by comparing and ordering the one's digits:

5 Radix Sort (Continued) CSC 244 Concepts of Algorithms 5 Notice that the numbers were added onto the list in the order that they were found, which is why the numbers appear to be unsorted in each of the bucket above. Now, we gather the buckets (in order from the 0 bucket to the 9 bucket) into the main list again: 340 710 812 582 493 715 195 385 437 Note: The order in which we divide and reassemble the list is extremely important, as this is one of the foundations of this algorithm.

6 Radix Sort (Continued) 6CSC 244 Concepts of Algorithms Now, the buckets are created again, this time based on the ten's digit: 493 812 715 710 195 437 582 340 385

7 Radix Sort (Continued) 7 Now the sublists are gathered in order from 0 to 9: 710 812 715 437 340 582 385 493 195 Finally, the sublists are created according to the hundred's digit: CSC 244 Concepts of Algorithms

8 Radix Sort (Continued) At last, the list is gathered up again: 195 340 385 437 493 582 710 715 812 And now we have a fully sorted array! Radix Sort is very simple, and a computer can do it fast. When it is programmed properly, Radix Sort is in fact one of the fastest sorting algorithms for numbers or strings of letters. 8 CSC 244 Concepts of Algorithms

9 Radix Sort Algorithm Let ‘A’ is Linear Array and N is the number of values stored in A and R is the Radix/Base (10) of Number System. For the processing requirement, I am using a two-dimensional array i.e. C[100][10], where 100 are the pockets in every bucket and 10 are the buckets. Here a recursive function is going to use for RADIX SORT. Here A, N and C will be global entities. 9 CSC 244 Concepts of Algorithms

10 Radix Sort Algorithm (Continued) 10 CSC 244 Concepts of Algorithms

11 C-Language Code for Recursive Radix Sort 11 CSC 244 Concepts of Algorithms // C++ Code (RADIX SORT): #include int const MAX = 100; int A[MAX]; // Two dimensional array, where each row has ten columns int C[MAX][10]; int N; // Function Prototyping void get_Values(); void RadixSort (int); void display( ); int main() { get_Values(); if(N>1) RadixSort (10); // Here 10 is the Radix of Decimal Numbers display(); cout<<endl<<endl; return 0; }

12 C-Language Code for Recursive Radix Sort void get_Values() { cout "; cin>>N; cout<<"\n Put "<<N<<" Values..\n"; for(int I=0 ;I >A[I]; } void display() { cout<<"\n\n\t\t\t Sorted List\n\n"; for(int I =0;I < N ; I++) cout<<A[I]<<"\t"; } 12 CSC 244 Concepts of Algorithms void RadixSort (int R) { int I, J, X, D, Flag=0, p = -1; D=R/10; for(I=0;I<N;I++) {for(J=0 ;J < 10 ;J++) C[I][J] = -1; } for(I=0;I<N ; I++) { X=A[I]%R; int M=X/D; if( M > 0) Flag = 1; C[I][M]=A[I]; } for(J=0 ; J< 10 ; J++) { for( I=0 ; I < N ; I++) { if(C[I][J] != -1) { p++; A[p]=C[I][J]; } if(Flag == 1) { RadixSort(R*10); // recursive call } } // end of RadixSort( ) function

13 Radix sort: another way Using o Queue ADT Sorting methods which utilize digital properties of the numbers (keys) in the sorting process are called radix sorts. Example. Consider the list 459 254 472 534 649 239 432 654 477 Step 1: ordering wrt ones Step 2: ordering wrt tens Step 3: ordering wrt hundreds 0 0 0 1 1 1 2 472 432 2 2 239 254 3 3 432 534 239 3 4 254 534 654 4 649 4 432 459 472 477 5 5 254 654 459 5 534 6 6 6 649 654 7 477 7 472 477 7 8 8 8 9 459 649 239 9 9 After step 1: 472 432 254 534 654 477 459 649 239 After step 2: 432 534 239 649 254 654 459 472 477 After step 3: 239 254 432 459 472 477 534 649 654 13 CSC 244 Concepts of Algorithms

14 Radix sort: using Queue ADT the algorithm Consider the following data structures: – a queue for storing the original list and lists resulting from collecting piles at the end of each step (call these master lists); – ten queues for storing piles 0 to 9; Pseudo code description of radix sort at the “idea” level: start with the one’s digit; while there is still a digit on which to classify data do { for each number in the master list do { add that number to the appropriate sublist } for each sublist do { for each number from the sublist do { remove the number from the sublist and append it to a newly arranged master list } advance the current digit one place to the left } 14 CSC 244 Concepts of Algorithms

15 Radix Sort: using Queue the algorithm (cont.) Here is a more detailed pseudo code description of the radix sort: Input: A queue Q of N items Output: Q sorted in ascending order Algorithm RadixSort (Q, N): digit := 1 while StillNotZero (digit) do { for (i := 1 to 10) do { create (sublist[i]) } while (! empty Q) do { dequeue (Q, item) pile := getPile (item, digit) + 1 O(N) enqueue (sublist[pile], item) swaps this outer loop will execute } “digit” times reinitialize (Q) for (j :=1 to 10) do { while (! empty sublist(j)) do { dequeue (sublist[j], item) O(N) enqueue (Q, item) } swaps } digit := digit * 10 } 15 CSC 244 Concepts of Algorithms

16 Efficiency of the Radix Sort Operations that affects the efficiency of radix sort the most are “dequeue- enqueue” swaps from and to the master list. Because the outer while-loop executes C times, and each of the inner loops is O(N), the total efficiency of radix sort is O(C*N). Notes: 1. If no duplicates are allowed in the list, we have log 10 N <= C for non-negative integers. 2. If there is a limit on the number of digits in the integers being sorted, we have C <= H * log 10 N. Therefore, radix sort is O(N * log N) algorithm if unique values are sorted; otherwise it is O(N) algorithm with a constant of proportionality, C, which can be large enough to make C * N > N * logN even for large N. A disadvantage of radix sort is that it required a large amount of memory to keep all of the sub-lists and the master list at the same time. 16 CSC 244 Concepts of Algorithms

17 About the getPile method The getPile method must return an appropriate isolated digit from the number currently considered. That digit + 1 yields the sublist, where the number is to be enqueued. A possible implementation of the getPile method is the following: int getPile (int number, int digit) { return (number % (10 * digit) / digit); } Examples: number = 1234 digit = 100 (1234 % 1000) / 100 = 2 number = 12345 digit = 1 (12345 % 10) / 1 = 5 17 CSC 244 Concepts of Algorithms

18 END 18 CSC 244 Concepts of Algorithms


Download ppt "Concepts of Algorithms CSC-244 Unit 11 & 12 Sorting (Radix Sort) Shahid Iqbal Lone Computer College Qassim University K.S.A."

Similar presentations


Ads by Google