SlideServe
Menu
Préférences
© SlideServe · v2026

CSE 246 Data Structures and Algorithms

kira avatar
11 diapositives · 253 Views · Sep 16, 2014

This lecture explores two essential sorting algorithms: Quick Sort and Heap Sort. Quick Sort operates by selecting a pivot and partitioning the list into three groups—elements less than the pivot, the pivot itself, and elements greater than the pivot—continuing recursively on the partitions. In contrast, Heap Sort builds a heap from the list and utilizes it to extract elements in sorted order. Learn about their efficiencies, with Quick Sort averaging O(n log n) and Heap Sort achieving the same efficiency, offering insights into their workings and practical applications.

CSE 246 Data Structures and Algorithms - slide 1
01 / 11CSE 246 Data Structures and Algorithms
1 / 17

Télécharger la présentation

CSE 246 Data Structures and Algorithms
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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