Presentation is loading. Please wait.

Presentation is loading. Please wait.

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-1 Chapter 7 Sorting Introduction to Data Structure CHAPTER 7 SORTING 7.1 Searching and List Verification.

Similar presentations


Presentation on theme: "Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-1 Chapter 7 Sorting Introduction to Data Structure CHAPTER 7 SORTING 7.1 Searching and List Verification."— Presentation transcript:

1 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-1 Chapter 7 Sorting Introduction to Data Structure CHAPTER 7 SORTING 7.1 Searching and List Verification 7.2 Definitions 7.3 Insertion Sort 7.4 Quick Sort 7.5 Optimal Sorting Time 7.6 Merge Sort 7.7 Heap Sort 7.8 Radix Sort

2 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-2 Chapter 7 Sorting Chapter 1 Basic Concepts Chapter 2 Arrays Chapter 3 Stacks and Queues Chapter 4 Linked Lists Chapter 5 Trees Chapter 6 Graph Chapter 7 Sorting Chapter 8 Hashing Chapter 9 Heap Structures Chapter 10 Search Structures Contents

3 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-3 Chapter 7 Sorting 7.1 Searching and List Verification Motivation of Sorting The term list here is a collection of records. Each record has one or more fields. Each record has a key to distinguish one record with another. For example, the phone directory is a list. Name, phone number, and even address can be the key, depending on the application or need. Key (Student_ID)Field (Dept.)

4 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-4 Chapter 7 Sorting Sequential Searching Two ways to store a collection of records Sequential Non-sequential Assume a sequential list f. To retrieve a record with key f[i].key from such a list, we can do search in the following order: f[n].key, f[n-1].key, …, f[1].key => sequential search

5 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-5 Chapter 7 Sorting Sequential Searching (cont.) int SeqSearch (int list[], int searchnum, int n) { int i; list[n] = searchnum; for (i = 0; list[i] != searchnum; i++); return (i < n) ? i: -1); } Program 7.1, p. 321 The average number of comparisons for a successful search is

6 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-6 Chapter 7 Sorting  Basic concept Compare searchnum and list[middle].key  searchnum < list[middle].key  search list[0] ~ list[middle-1]  searchnum = list[middle].key  return TRUE  searchnum > list[middle].key  search list[middle+1] ~ list[n-1]  Program 7.2, p. 322  Complexity  A binary search only takes O(log n) time to search a sequential list with n records. Binary Search

7 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-7 Chapter 7 Sorting List Verification Definition Given two lists, verification if both are the same. list1 = list2? Example: Tax verification The IRS gets salary reports from employers. IRS also gets tax filing reports from employees about their salary. Need to verify the two numbers match for each individual employee. Two methods Method 1: Random verification Method 2: Ordered verficiation

8 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-8 Chapter 7 Sorting Random Verification: Unsorted Lists void verify1(element list1[], element list2[], int n, int m) /* Compare two unordered lists list1 and list2 */ { int i, j; int marked [MAX_SIZE]; for (i = 0; i < m; i++) marked[i] = FALSE; for (i = 0; i< n; i++) if ((j == seqsearch(list2, m, list1[i].key()) < 0) printf(“%d is not in list 2\n”, list1[i].key); else /* check each of the other fields from list1[i] and list2[j], and print out any discrepancies */ marked[j] = TRUE; for (i = 1; i < m; i++) if (!marked[i]) printf(“%d is not in list 1\n”, list2[i].key); } Complexity: O(mn) Why?

9 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-9 Chapter 7 Sorting Sorted Verifying: Sorted Lists void verify2(element list1[], element list2[], int n, int m) { int i, j; sort(list1, n); sort(list2, m); i = j = 0; while (i < n && j < m) if (list1[i].key < list2[j].key) { printf(“%d is not in list 2\n”, list1[i].key); i++; } else if (list1[i].key == list2[j].key) { /* compare list1[i] and list2[j] on each of the other fields and report any discrepancies */ i++; j++; } else { printf(“%d is not in list 1\n”, list2[j].key); j++; } for (; i < n; i++) printf(“%d is not in list 2\n”, list1[i].key; for (; j < m; i++) printf(“%d is not in list 2\n”, list1[i].key; } Complexity: O(max{n log n, m log m})

10 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-10 Chapter 7 Sorting 7.2 Definition Formal definition Given a list of records (R 0, R 1, …, R n-1 ), each with a key K i. The sorting problem is to find permutation, σ, such that K σ(i) ≤ K σ(i+1), 1 ≤ i ≤ n – 1. The desired ordering is (R σ(1), R σ(2), R σ(n) ). If a list has several key values that are identical, the permutation, σ s, is not unique. Let σ s be the permutation of the following properties: (1) [sorted] K σ(i) ≤ K σ(i+1), 1 ≤ i ≤ n – 1 (2) [stable] If i < j and K i == K j in the input list, then R i precedes R j in the sorted list. The above sorting method that generates σ s is stable.

11 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-11 Chapter 7 Sorting Stable Sorting Example Stable permutation Unstable permutation 3 2 4 1 7 4 2 5 0 1 2 3 4 5 Key a b c d e f Record 0 1 2 3 4 5 1 0 2 5 3 4 2 3 4 1 4 2 5 7 0 1 2 3 4 5 Key b a c e f d Record 2 3 4 2 4 1 5 7 0 1 2 3 4 5 Key b a e c f d Record  0 1 2 3 4 5 1 0 3 5 2 4 

12 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-12 Chapter 7 Sorting Category of Sorting Methods Internal method Methods to be used when the list to be sorted is small enough so that the entire sort list can be carried out in the main memory. Example Insertion sort Quick sort Merge sort Heap sort Radix sort External method Methods to be used on larger lists

13 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-13 Chapter 7 Sorting Basic concept 7.3 Insertion Sort a 1 a 2 a 3 … a k aiai sorted insert a 1 a 2 a i a 3 … a k sorted 1 3 5 7 9 4 1 3 4 5 7 9

14 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-14 Chapter 7 Sorting Insertion Sort: Program Program 7.5, p 327 void insertion_sort(element list[], int n) { int i, j; element next; for (i = 1; i < n; i++) { next = list[i]; for (j = i – 1; j >= 0 && next.key < list[j].key; j--) list[j+1] = list[j]; list[j+1] = next; }

15 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-15 Chapter 7 Sorting Insertion Sort: Example Record R i is left out of order (LOO) iff R i < Example 1 Assume n = 5 and the input key sequence is 5, 4, 3, 2, 1 j[1][2][3][4][5] -54321 245321 334521 423451 512345

16 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-16 Chapter 7 Sorting Insertion Sort: Example Example 2 Assume n = 5 and the input key sequence is 2, 3, 4, 5, 1 j[1][2][3][4][5] -23451 223451 323451 423451 512345 O(1) O(n)

17 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-17 Chapter 7 Sorting Insertion Sort Analysis If there are k LOO records in a list, the computing time for sorting the list via insertion sort is O((k+1)n) = O(kn) Therefore, if k << n, then insertion sort might be a good sorting choice.

18 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-18 Chapter 7 Sorting 7.4 Quick Sort Quick sort is developed by C. A. R. Hoare. Quick sort has the best average behavior among the sorting methods. Basic concept Based on the divide and conquer paradigm Choose an element (pivot) p, i.e., p = K 0 Place p into a proper position j such that K 0 ~ K j  1  p K j  1 ~ K n  1 > p K 0 K 1 … K j-1 K j = p K j+1 K j+2 … K n-1

19 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-19 Chapter 7 Sorting Quick Sort (cont.) Example 25 57 48 37 12 92 86 33 (12) 25 (57 48 37 92 86 33) 12 25 (48 37 33) 57 (92 86) 12 25 (37 33) 48 57 (92 86) 12 25 33 37 48 57 (92 86) 12 25 33 37 48 57 86 92

20 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-20 Chapter 7 Sorting Quick Sort (cont.) Key mechanism: a partition method Algorithm quicksort(list, left, right) { if (left < right) { partition(list, left, right, j); quicksort(list, left, j  1); quicksort(list, j+1, right); }

21 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-21 Chapter 7 Sorting Quick Sort: Partition Concept of partition method Let pivot = list[left] be the pivot Use two pointers, i and j i  until list[i]  pivot; j  until list[j]  pivot; list[i]  list[j] if i < j

22 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-22 Chapter 7 Sorting Quick Sort: Partition (cont.) Example of partition method 25 57 48 37 12 92 86 33 25 12 48 37 57 92 86 33 (12) 25 (48 37 57 92 86 33) i j i j i j

23 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-23 Chapter 7 Sorting Quick Sort: Program Codes void quicksort(element list[], int left, int right) { int pivot, i, j; element temp; if (left < right) { i = left; j = right + 1; pivot = list[left].key; do { do i++; while (list[i].key < pivot); do j--; while (list[j].key > pivot); if (i < j) SWAP(list[i], list[j], temp); } while (i < j); SWAP(list[left], list[j], temp); quicksort(list, left, j–1); quicksort(list, j+1, right); } partition

24 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-24 Chapter 7 Sorting Quick Sort: Example Example Input list: 10 records with keys (26, 5, 37, 1, 61, 11, 59, 15, 48, 19). K0K0 K1K1 K2K2 K3K3 K4K4 K5K5 K6K6 K7K7 K8K8 K9K9 LeftRight [265371611159154819110 [11519115]26[59614837]15 [15]11[1915]26[59614837]12 1511[1915]26[59614837]45 1511151926[59614837]710 1511151926[4837]59[61]78 1511151926374859[61]10 151115192637485961

25 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-25 Chapter 7 Sorting Quick Sort: Analysis Analysis of QuickSort Worse case: O(n 2 ) Average case: Assume each time a record is correctly positioned  |left sublist| = |right sublist| Let T(n) be the time taken to sort a list of size n T(n) ≤ cn + 2T(n/2), for some constant c ≤ cn + 2(cn/2 +2T(n/4)) ≤ 2cn + 4T(n/4) : ≤ cn log 2 n + T(1) = O(n logn)

26 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-26 Chapter 7 Sorting Quick Sort Variant Quick sort using a median of three Pick the median of the first, middle, and last keys in the current sublist as the pivot. Thus, pivot = median{K left, K (left+right)/2, K right }.

27 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-27 Chapter 7 Sorting 7.5 Optimal Sorting Time Question How quickly can we sort a list of n objects? Answer If only operations permitted on keys are comparisons and interchanges, then O(n logn) is the best possible time. Method This is done by using a tree called decision tree that describes the sorting process. Each vertex of the tree represents a key comparison, and the branches indicate the result.

28 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-28 Chapter 7 Sorting Decision Tree for Insertion Sort K 1 ≤ K 2 stopK 1 ≤ K 3 K 2 ≤ K 3 K 1 ≤ K 3 stopK 2 ≤ K 2 stop Yes No Yes No I II III IV V VI [0, 1, 2] [1, 0, 2] [1, 2, 0] [2, 1, 0] [0, 1, 2] [0, 2, 1] [2, 0, 1] [0, 2, 1] [1, 0, 2]

29 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-29 Chapter 7 Sorting Decision Tree (cont.) Theorem 7.1: Any decision tree that sorts n distinct elements has a height of at least log 2 (n!) + 1 Corollary: Any algorithm that sorts only by comparisons must have a worst-case computing time of Ω(n log n)

30 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-30 Chapter 7 Sorting 7.6 Merge Sort Kernel operation of Merge Sort: Merging Given two sorted list, merge them into a single sorted list Example 25 37 48 57 12 33 86 92 => 12 25 33 37 48 57 86 92 Methods for merging Simple merge O(1) space merge

31 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-31 Chapter 7 Sorting Simple Merge void merge(element list[], element sorted[], int i, int m, int n) /* merge list[i],…,list[m], and list[m+1],…,list[n] */ { int j, k, t; j = m+1; k = i; while (i <= m && j <= n) { if (list[i].key <= list[j].key) sorted[k++] = list[i++]; else sorted[k++] = list[j++]; } if (i > m) for (t = j; t <= n; t++) sorted[k+t-j] = list[t]; else for (t = i; t <= m; t++) sorted[k+t-i] = list[t]; } Time & space complexity: O(n  i + 1)

32 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-32 Chapter 7 Sorting O(1) Space Merge A merge algorithm only requires O(1) additional space Assumption The total number of records n is a perfect square The numbers of records in the left sublist and the right sublist are multiple of

33 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-33 Chapter 7 Sorting O(1) Space Merge Algorithm Step 1: Identify the records with largest keys. This is done by following right to left along the two lists to be merged. Step 2: Exchange records of the second list identified in Step 1 with those just to the left of those identified from the first list. Step 3: Swap the block of largest with the leftmost block (unless it is already the leftmost block). Sort the rightmost block. Step 4: Reorder the blocks, excluding the block of largest records, into nondecreasing order of the last key in the blocks. Step 5: Perform as many merge substeps as needed to merge the blocks, other than the block with the largest keys. Step 6: Sort the block with the largest keys.

34 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-34 Chapter 7 Sorting O(1) Space Merge: Example 0 2 4 6 8 a c e g i j k l m n t w z|1 3 5 7 9 b d f h o p q r s u v x y 0 2 4 6 8 a c e g i j k l m n t w z 1 3 5 7 9 b d f h o p q r s u v x y 0 2 4 6 8 a|c e g i j k|u v x y w z|1 3 5 7 9 b|d f h o p q|r s l m n t u v x y w z|c e g i j k|0 2 4 6 8 a|1 3 5 7 9 b|d f h o p q|l m n r s t u v x y w z 0 2 4 6 8 a|1 3 5 7 9 b|c e g i j k|d f h o p q|l m n r s t 0 v x y w z u 2 4 6 8 a|1 3 5 7 9 b|c e g i j k|d f h o p q|l m n r s t 0 1 x y w z u 2 4 6 8 a|v 3 5 7 9 b|c e g i j k|d f h o p q|l m n r s t 0 1 2 y w z u x 4 6 8 a|v 3 5 7 9 b|c e g i j k|d f h o p q|l m n r s t

35 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-35 Chapter 7 Sorting O(1) Space Merge: Example (cont.) 0 1 2 3 4 5 u x w 6 8 a|v y z 7 9 b|c e g i j k|d f h o p q|l m n r s t 0 1 2 3 4 5 6 7 8 u w a|v y z x 9 b|c e g i j k|d f h o p q|l m n r s t 0 1 2 3 4 5 6 7 8 9 a w|v y z x u b|c e g i j k|d f h o p q|l m n r s t 0 1 2 3 4 5 6 7 8 9 a w v y z x u b c e g i j k|d f h o p q|l m n r s t 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k v z u|y x w o p q|l m n r s t 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k v z u y x w o p q|l m n r s t 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q y x w|v z u r s t 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t|v z u y x w

36 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-36 Chapter 7 Sorting O(1) Space Merge: Analysis Steps 1 and 2 O( ) time and O(1) space Step 3 Swapping: O( ) time and O(1) space Sorting: O(n) time and O(1) space (via insertion sort) Step 4 O(n) time and O(1) space (via selection sort) Selection sort sorts m records using O(m 2 ) key comparisons and O(m) record moves. O(n 1.5 ) time and O(1) space (via insertion sort) Insertion sort needs O(m 2 ) record moves ( records per block * n record moves).

37 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-37 Chapter 7 Sorting O(1) Space Merge: Analysis (cont.) Step 5 Merge substeps: The total number of is at most. The total time for is O(n). Step 6 The sort of can be done in O(n) by using either a selection sort or an insertion sort. In total O(n) time and O(1) space

38 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-38 Chapter 7 Sorting Iterative Merge Sort Concept Treat the input as n sorted lists, each of length 1. Lists are merged by pairs to obtain n/2 lists, each of size 2 (if n is odd, the one list is of length 1). The n/2 lists are then merged by pairs, and so on until we are left with only one list.

39 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-39 Chapter 7 Sorting Iterative Merge Sort: Example 265771611159154819 5 261 7711 6115 5919 48 1 5 26 7711 15 59 6119 48 1 5 11 15 26 59 61 7719 48 1 5 11 15 19 26 48 59 61 77

40 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-40 Chapter 7 Sorting Iterative Merge Sort: Analysis Program code Program 7.9 and 7.10 Time complexity Total of passes are made over the data Each pass of merge sort takes O(n) time The total of computing time is O(n log n)

41 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-41 Chapter 7 Sorting Recursive Merge Sort Concept Divide the list to be sorted into two roughly equal parts: left sublist [left : (left+right)/2] right sublist [(left+right)/2 +1 : right] Sort each sublist recursively, and merge the sorted sublists To avoid copying, the use of a linked list (integer instead of real link) for sublist is desirable. Program code Program 7.11 and 7.12, complexity: O(n log n)

42 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-42 Chapter 7 Sorting Recursive Merge Sort: Example 265771611159154819 5 2611 5919 48 5 26 7711 15 5919 48 1 5 26 61 7711 15 19 48 59 1 5 11 15 19 26 48 59 61 77 1 61

43 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-43 Chapter 7 Sorting Natural Merge Sort Concept It takes advantage of the prevailing order within the list before performing merge sort It runs an initial pass over the data to determine the sublists of records that are in order Then it uses the sublists for the merge sort

44 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-44 Chapter 7 Sorting Natural Merge Sort: Example 265 771 6111 5915 4819 1 11 59 6115 19 48 5 26 77 1 5 11 26 59 61 77 15 19 48 1 5 11 15 19 26 48 59 61 77

45 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-45 Chapter 7 Sorting 7.7 Heap Sort Preliminary Merge sort needs O(n) additional storage space, even though its computing time is O(n log n) Merge sort using O(1) merge only needs O(1) space but the sorting algorithm is much slower Heap sort only requires a fixed amount of additional storage achieves worst-case and average computing time O(n log n)

46 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-46 Chapter 7 Sorting Heap Sort (cont.) Concept Adopt the max-heap structure Consists of two phases Phase 1: create the heap Insert the n records into an empty heap Phase 2: adjust the heap Exchange the max element with the current last element and perform adjustment

47 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-47 Chapter 7 Sorting Heap Sort: Program Code void heapsort (element list[], int n) { int i, j; element temp; for (i = n/2; i > 0; i--) /* Phase 1 */ adjust(list, i, n); for (i = n-1; i > 0; i--) { /* Phase 2 */ SWAP(list[1], list[i+1], temp); adjust(list, 1, i); } Complexity: O(n log n)

48 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-48 Chapter 7 Sorting Heap Sort: Program Code (cont.) void adjust (element list[], int root, int n) { int child, rootkey; element temp = list[root]; rootkey = list[root].key; child = 2*root; while (child <= n) { if (child < n) && (list[child].key < list[child+1].key)) child++; if (rootkey > list[child].key) break; else { list[child/2] = list[child]; child *= 2; } list[child/2] = temp; }

49 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-49 Chapter 7 Sorting Heap Sort: Example 26 154819 5 161 77 1159 77 1515 61 4819 59 1126 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [2] [3] [4] [5] [6] [7] [8] [9] [10] (a) Input array (b) Initial heap [1]

50 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-50 Chapter 7 Sorting Heap Sort: Example (cont.) 61 51 48 1519 59 1126 [2] [3] [4] [5] [6] [7] [8] [9] [1] Heap size = 9, Sorted = [77] 59 5 48 1519 26 111 [2] [3] [5] [6] [7] [8] [1] Heap size = 8, Sorted = [61, 77]

51 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-51 Chapter 7 Sorting Heap Sort: Example (cont.) 61 51 48 1519 59 1126 [2] [3] [4] [5] [6] [7] [8] [9] [1] Heap size = 9, Sorted = [77] 59 5 48 1519 26 111 [2] [3] [5] [6] [7] [8] [1] Heap size = 8, Sorted = [61, 77]

52 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-52 Chapter 7 Sorting 7.8 Radix Sort Sorting on multiple keys A list of records are said to be sorted with respect to the keys K 0, K 2, …, K r-1 iff for every pair of records i and j, i < j and (K 0 i, K 1 i, …, K r-1 i ) ≤ (K 0 j, K 1 j, …, K r-1 j ). (x 0, x 2, …, x r-1 ) ≤ (y 0, y 2, …, y r-1 ) iff either x i = y i, 0 ≤ i ≤ j, and x j+1 < y j+1 for some j < r-1 or x i = y i, 0 ≤ i < r Example, sorting a deck of cards: suite and face value. K 0 [suit]: ♣ < ♦ < ♥ < ♠ K 1 [Face value]: 2 < 3 < 4 … < 10 < J < Q < K < A A possible ordering 2 ♣, … A ♣, 2 ♦, …, A ♦, 2 ♥, …, A ♥, 2 ♠, …, A ♠ Most significant

53 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-53 Chapter 7 Sorting Radix Sort (cont.) Two popular ways to sort on multiple keys MSD: sort on the most significant key into multiple piles LSD: sort on the least significant digit first LSD and MSD only defines the order in which the keys are to be sorted LSD and MSD can be used even when there is only one key E.g., if the keys are numeric, then each decimal digit may be regarded as a subkey => Radix sort

54 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-54 Chapter 7 Sorting Radix Sort: Example 1792083069385998455927133 list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] e[0]e[1]e[2]e[3]e[4]e[5]e[6]e[7]e[8]e[9] f[0]f[1]f[2]f[3]f[4]f[5]f[6]f[7]f[8]f[9] 179 859 9 2083065598493 33 271 9333984553062081798599 list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9]

55 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-55 Chapter 7 Sorting Radix Sort: Example (cont.) e[0]e[1]e[2]e[3]e[4]e[5]e[6]e[7]e[8]e[9] f[0]f[1]f[2]f[3]f[4]f[5]f[6]f[7]f[8]f[9] 179859 9 208 306559849333271 9333984553062081798599 list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] 3062089335585927117998493 list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9]

56 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-56 Chapter 7 Sorting Radix Sort: Example (cont.) e[0]e[1]e[2]e[3]e[4]e[5]e[6]e[7]e[8]e[9] f[0]f[1]f[2]f[3]f[4]f[5]f[6]f[7]f[8]f[9] 179 55 33 9 859984 306 271 9335593179208271306859948 list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] 3062089335585927117998493 list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9] 93 208

57 Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-57 Chapter 7 Sorting Radix Sort: Analysis Let d: the number of digits r: radix size n: number of records Time complexity O(d(n+r)) Usually r << n, so O(dn)


Download ppt "Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 7-1 Chapter 7 Sorting Introduction to Data Structure CHAPTER 7 SORTING 7.1 Searching and List Verification."

Similar presentations


Ads by Google