C++ programming program design including data structure 7th advanced sorting

20 102 0
C++  programming program design including data structure 7th advanced sorting

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Data Structures & Algorithms © John Urrutia 2016, All Rights Reserved Advanced Sorting The Shell Sort Created by Donald Shell Assuming a random distribution of keys try to position this key reasonably close to its correct position by sorting elements across widely disbursed intervals Trade off between the number of swaps vs number of comparisons  Each pass through the data will reduce the width of the of the interval The Shell Sort (cont.) The last pass will have an interval of which will a standard insertion sort Very easy to code Difficult to select an appropriate interval to provide efficiency The Shell Sort Big ‘O’ ranges from: N2 – Normal insertion sort N7/6 – Best Efficiency for Shell Sort Example 13 items to sort  Insertion Sort  Max Copies for insertion = 67  Max Comparisons = 55  Shell Sort  Max Copies for insertion = 67  Max Comparisons = 55 Gap Sequence     Original 81 94 11 96 12 35 17 95 28 58 41 75 15 Before 81 94 11 96 12 35 17 95 28 58 41 75 15 (1,6,11) 35         41         81     (2,7,12)   17         75         94   (3,8,13)     11         15         95 (4,9)       28         96         (5,10)         12         58       After 35 17 11 28 12 41 75 15 96 58 81 94 95                             Before 35 17 11 28 12 41 75 15 96 58 81 94 95 (1,4,7,10,13) 28     35     58     75     95 (2,5,8,11)   12     15     17     81     (3,6,9,12)     11     41     94     96   After 28 12 11 35 15 41 58 17 94 75 81 96 95                             Before 28 12 11 35 15 41 58 17 94 75 81 96 95 After 11 12 15 17 28 35 41 58 75 81 94 95 96 Shell Sort Code Step – Find an interval for the array void shellSort() { int inner, outer; long temp; int h = 1; while (h 0) {for (outer = h; outer < nElems; outer++) {temp = theArray[outer]; inner = outer; while (inner > h-1 && theArray[inner-h] >= temp) { theArray[inner] = theArray[inner-h]; inner-=h; } theArray[inner] = temp; } h=(h-1)/3; } The QuickSort One of the most popular advanced sorting methods Created by C A R Hoare in 1962 Uses a Divide and Conquer approach Partitions the data then subdivides the partitions using recursion Perhaps the fastest O(N * log2 N2) for random data sorted in-place Degenerates to O(N2) for data that is already in order The QuickSort The algorithm Select a “pivot” value to use for partitioning the data Create the Partitions Place all values less than the pivot in the left partition  Place all values Greater than the pivot in the right partition  Return the position of the partition boundary  Call Recursive with the sub-partitions  The QuickSort Un-partitioned Array 94 78 Partitioned Array 50 Pivot  Sorted  42 The QuickSort Left Partition  has elements (insertion Left Partition sort)  has elements (insertion sort) 42 3 3 3 3 3 89 89 27 27 27 27 12 12 12 12 12 63 63 63 12 12 12 27 27 27 27 27 12 94 27 12 94 27 12 94 89 63 94 89 36 94 89 36 94 89 36 50 89 36 50 42 36 50 42 3642 50 3642 50 78 78 42 78 42 78 42 78 42 78 42 78 42 78 89 63 89 63 89 6378 5036 pivot 50 36 50 36 50 36 50 63 pivot 5063 Right Partition 9463 has elements (QuickSort) 9463 94 78 pivot 9478  Right Partition has elements 89 94 (insertion sort) The QuickSort The void recQuickSort(int left, int right) Recursive Code {   if (right - left = rightPtr)      // if pointers cross,           break;                    //    partition done else                          // not crossed, so            swap(leftPtr, rightPtr);  //    swap elements       }  // end while(true)        swap(leftPtr, right);         // restore pivot       return leftPtr;               // return pivot location }  // end partitionIt() The QuickSort Finding a good pivot value Median-Of-Three Sorts the Three elements at the beginning, ending & center into their correct partitions and uses the center as the pivot  Avoids O(N2)  Slightly improves number of items that must be partitioned  Increases the speed of the inner partitioning loop  The Radix Sort Implementation of a radix sort using Queues Radix sort passes once for each digit in the input At the conclusion of each pass the digit is in order ie radix sort of two digit numbers 91, 46, 85, 15, 92, 35, 31, 22 Queue application – sort of first pass orders by the one’s digit Before 91, 46, 85, 15, 92, 35, 31, 22 After 91, 31, 22, 92, 85, 15, 35, 46 second pass orders by the ten’s digit Before 91, 31, 22, 92, 85, 15, 35, 46 After 15, 22, 31, 35, 46, 85, 91, 92 Queues in order Pass -9 ,4 , , 9, 3, 3, , Bin Bin Bin Bin Bin Bin Bin Bin Bin Bin Pass - , , , , , , , Queues in order Pass - , , 2 , , , , , Bin Bin Bin Bin Bin Bin Bin Bin Bin Bin  Pass - , , , , , , , Radix Sort The Code: Pass – One’s digit  Each element is enqueued onto the correct queue and counted Pass – Ten’s digit  Each queue is dequeued into a temp array & reenqueued in the correct order Each queue is dequeued to the console ... } The QuickSort One of the most popular advanced sorting methods Created by C A R Hoare in 1962 Uses a Divide and Conquer approach Partitions the data then subdivides the partitions using... log2 N2) for random data sorted in-place Degenerates to O(N2) for data that is already in order The QuickSort The algorithm Select a “pivot” value to use for partitioning the data Create the... to its correct position by sorting elements across widely disbursed intervals Trade off between the number of swaps vs number of comparisons  Each pass through the data will reduce the width

Ngày đăng: 06/02/2018, 09:14

Từ khóa liên quan

Mục lục

  • Slide 1

  • The Shell Sort

  • The Shell Sort (cont.)

  • The Shell Sort

  • Slide 5

  • Shell Sort Code

  • Shell Sort Code

  • The QuickSort

  • The QuickSort

  • The QuickSort

  • The QuickSort

  • The QuickSort

  • The QuickSort

  • The QuickSort

  • The QuickSort

  • The Radix Sort

  • Queue application – sort of

  • Queues in order

  • Queues in order

  • Radix Sort

Tài liệu cùng người dùng

Tài liệu liên quan