Quick Sort and Merge Sort Fei Chen CSCI2100B Data Structures Tutorial 11 1.

Click here to load reader

  • date post

    17-Jan-2016
  • Category

    Documents

  • view

    216
  • download

    0

Embed Size (px)

Transcript of Quick Sort and Merge Sort Fei Chen CSCI2100B Data Structures Tutorial 11 1.

CSC2100B Tutorial 4

Quick Sort and Merge Sort Fei ChenCSCI2100B Data Structures Tutorial 11

1Quick SortEfficient sorting algorithmExample of Divide and Conquer algorithmTwo phasesPartition phaseDivides the work into halfSort phaseConquers the halves!

2QuicksortPartitionChoose a pivotFind the position for the pivot so that all elements to the left are less / equalall elements to the right are equal / greater3< =pivot=> pivotpivot3QuicksortConquerApply the same algorithm to each half< pivot> pivotpivot p< =pp=> p45

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

QuicksortImplementation

22quicksort( void *a, int low, int high ) { int pivot; /* Termination condition! */ if ( high > low ) { pivot = partition( a, low, high ); quicksort( a, low, pivot-1 ); quicksort( a, pivot+1, high ); } }DivideConquer22Quicksort - Partition23int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] pivot */ while( a[right] > pivot_item ) right--; if ( left < right ) { SWAP(a,left,right); left++; right--;} } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; }23Quicksort - AnalysisPartitionCheck every item onceO(n)ConquerDivide data in halfO(log2n)TotalProductO(n log n)Same as Heapsortquicksort is generally fasterFewer comparisons

2424Quicksort vs Heap SortQuicksortGenerally fasterSometimes O(n2)Better pivot selection reduces probabilityUse when you want average good performanceCommercial applications, Information systemsHeap SortGenerally slowerGuaranteed O(n log n) 25Merge Sort - DefinitionDefinition: A sort algorithm that splits the items to be sorted into two groups, recursively sorts each group, and merge them into a final sorted sequence. Run time is O (n log n). 26Merge Sort Divide-and-ConquerDivide-and-conquer is a general algorithm design paradigm:Divide: divide the input data S in two disjoint subsets S1 and S2Conquer: solve the sub-problems associated with S1 and S2 recursivelyCombine: combine the solutions for S1 and S2 into a solution for SThe base case for the recursion are sub-problems of size 0 or 127Merge Sort TreeAn execution of merge-sort is depicted by a binary treeeach node represents a recursive call of merge-sort and stores unsorted sequence before the execution sorted sequence at the end of the executionthe root is the initial callthe leaves are calls on subsequences of size 0 or 128

Merge Sort - example297 2 9 4 | 3 8 6 1Partition307 2 9 4 | 3 8 6 17 2 | 9 4Recursive call, partitionMerge Sort - exampleMerge Sort - example317 2 9 4 | 3 8 6 17 2 | 9 47 | 2Recursive call, partitionMerge Sort - example327 2 9 4 | 3 8 6 17 2 | 9 47 | 277Recursive call, base caseMerge Sort - example337 2 9 4 | 3 8 6 17 2 | 9 47 | 27722Recursive call, base caseMerge Sort - example347 2 9 4 | 3 8 6 17 2 | 9 47|2 2 77722MergeMerge Sort - example357 2 9 4 | 3 8 6 17 2 | 9 47|2 2 79|4 4 97722Recursive call, base case, , base case, merge9944Merge Sort - example367 2 9 4 | 3 8 6 172|94 2 4 7 97|2 2 79|4 4 977229944MergeMerge Sort - example376 1 1 67 2 9 4 | 3 8 6 172|94 2 4 7 93 8 6 11 3 8 67|2 2 79|4 4 9772299443 8 3 833886611Recursive call, , merge, mergeMerge Sort - example386 1 1 67294|3861 1234678972|94 2 4 7 93 8 6 11 3 8 67|2 2 79|4 4 9772299443 8 3 833886611MergeMerge Sort - analysisThe height h of the merge-sort tree is log n + 1 = O(log n) at each recursive call we divide in half the sequenceThe overall amount or work done at the nodes of depth i is O(n)we partition and merge 2i sequences of size n/2iwe make 2i+1 recursive callsThus, the total running time of merge-sort is O(n log n)

39

Merge Sort sample codevoid mergesort(int low, int high){ if (low