1 / 11

CSE 246 Data Structures and Algorithms

CSE 246 Data Structures and Algorithms. Spring2011 Lecture#22. Sorting. Quick Sort Heap Sort. Quick Sort. Each iteration of the quick sort selects an element, known as the pivot , and divides the list into 3 groups: Elements whose keys are less than (or equal to) the pivot’s key.

kira
Download Presentation

CSE 246 Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 246Data Structures and Algorithms Spring2011 Lecture#22

  2. Sorting • Quick Sort • Heap Sort Quratulain

  3. Quick Sort • Each iteration of the quick sort selects an element, known as the pivot, and divides the list into 3 groups: • Elements whose keys are less than (or equal to) the pivot’s key. • The pivot element • Elements whose keys are greater than the pivot’s key.

  4. Quick Sort • The sorting then continues by quick sorting the left partition followed by quick sorting the right partition. • The basic algorithm is as follows:

  5. Quick Sort • Partitioning Step: Take an element in the unsorted array and determine its final location in the sorted array. This occurs when all values to the left of the element in the array are less than (or equal to) the element, and all values to the right of the element are greater than (or equal to) the element. We now have 1 element in its proper location and two unsorted subarrays. • Recursive Step: Perform step 1 on each unsorted subarray.

  6. Quick Sort • Each time step 1 is performed on a subarray, another element is placed in its final location of the sorted array, and two unsorted subarrays are created. • When a subarray consists of one element, that subarray is sorted. • Therefore, that element is in its final location.

  7. Quick Sort • There are several partitioning strategies used in practice (i.e., several “versions” of quick sort), but the one we are about to describe is known to work well. • For simplicity we will select the firstelement as the pivot element. • We could also chose a different pivot element and swap it with the last element in the array.

  8. Efficiency of Quick Sort • List have n items every time split into two i-e n=2m • (n-1) comparison on first pass. • List split in to half n/2 approximately. • Same way continue to split as follows n+2*(n/2)+4*(n/4)+8*(n/8)+…+n*(n/n) n+n+n+n+…+n ------ [List split m times] The total number of comparison in average case is O(n*m) where m= log n. Thus, O(n log n) In worst case is O(n2)

  9. Heap Sort • Idea: take the items that need to be sorted and insert them into a heap. • By calling deleteHeap, we remove the smallest (or largest) element, depending on whether or not we are working with a min- or max-heap, respectively. • Hence, the elements are removed in ascending or descending order. • Efficiency: O(nlog2n)

  10. Efficiency Summary

  11. Read book for further reading Quratulain

More Related