120 likes | 289 Views
CS2006 - Data Structures I. Chapter 10 Algorithm Efficiency & Sorting IV. Topics. Sorting Radix Sort. Radix (Bucket) Sort. Idea: Form groups and then combine them to sort a collection of data Arrange the data according to their rightmost (least significant) letter (value)
E N D
CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting IV
Topics • Sorting • Radix Sort
Radix (Bucket) Sort • Idea: • Form groups and then combine them to sort a collection of data • Arrange the data according to their rightmost (least significant) letter (value) • Combine the groups into one • Arrange again according to the next least significant value • Repeat until you reach the most significant value • Restriction • Can work only on certain key types
Radix Sort • Example: Original Digits 0123 2154 0222 0004 0283 1560 1061 2150
Radix Sort • Pseudocode radixSort(ItemArray the array, integer n, integer d) // Sorts n d-digit integers in the array theArray for (j = d down to 1) { Initialize 10 groups to empty Initialize a counter for each group to 0 for (i = 0 through n-1) { k = jth digit of theArray[i] Place theArray[i] at the end of group k Increase kth counter by 1 } // end for i Replace the items in theArray with all the items in group 0, followed by all items in group 1, …etc. } // end for j
Radix Sort • Say that the number of digits in our integers is ‘k’ • The maximum size of an integer is constant • If an unsigned long is 32 bits, then the largest integer is 4 294 967 296 (k=10) • Often even more restricted • Eg. Student number k=7 • F(n) = ? • Why?
Radix Sort • Say that the number of digits in our integers is ‘k’ • The maximum size of an integer is constant • If an unsigned long is 32 bits, then the largest integer is 4 294 967 296 (k=10) • Often even more restricted • Eg. Student number k=7 • F(n) = kn • We are not comparing the items with each other • Just looking at the digits
Radix Sort • Analysis: • O(n) • Key size is a factor, but will still be a linear relationship • Constant proportionality(k) may be high • Why not use it all the time?
Comparison of Sorting Algorithms Sorting Worse case Average Case ------------------------------------------------------------------- Bubble Sort n2 n2 Selection Sort n2 n2 Insertion Sort n2 n2 Mergesort nlogn nlogn Quicksort n2 nlogn Radix Sort n n Treesort n2 nlogn Heap Sort nlogn nlogn
True or False 1 The analysis of an algorithm must take into consideration the computer that will be used to run a program that implements the algorithm. 2. The values of the growth-rate function O(log2n) grow faster than the values of the growth-rate function O(n).
True or False • Low-order terms can be ignored in an algorithm’s growth-rate function. • The efficiency of the selection sort depends on the initial arrangement of the data.
True or False • For large arrays, the insertion sort is prohibitively inefficient. • The mergesort is not a recursive sorting algorithm.