1 / 13

Quicksort Concept

Quicksort Concept. Basic Concept: divide and conquer Select a pivot and split the data into two groups: (< pivot) and (> pivot):. pivot. (< pivot) LEFT group. (> pivot) RIGHT group. Recursively apply Quicksort to the subgroups. Quicksort Start. Start with all data in an array, and

Download Presentation

Quicksort Concept

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. Quicksort Concept • Basic Concept: divide and conquer • Select a pivot and split the data into two groups: (< pivot) and (> pivot): pivot (<pivot) LEFT group (> pivot) RIGHT group • Recursively apply Quicksort to the subgroups

  2. Quicksort Start Start with all data in an array, and consider it unsorted Unsorted Array

  3. Quicksort Step 1 pivot Step 1, select a pivot (it is arbitrary) We will select the first element, as presented in the original algorithm by C.A.R. Hoare in 1962. 26 33 35 29 19 12 22

  4. Quicksort Step 2 pivot Step 2, start process of dividing data into LEFT and RIGHT groups: The LEFT group will have elements less than the pivot. The RIGHT group will have elements greater that the pivot. Use markers left and right 26 33 35 29 19 12 22 left right

  5. Quicksort Step 3 pivot Step 3, If left element belongs to LEFT group, then increment left index. If right index element belongs to RIGHT, then decrement right. Exchange when you find elements that belong to the other group. 26 33 35 29 19 12 22 left right

  6. Quicksort Step 4 pivot Step 4: Element 33 belongs to RIGHT group. Element 22 belongs to LEFT group. Exchange the two elements. 26 33 35 29 19 12 22 left right pivot 26 22 35 29 19 12 33 right left

  7. Quicksort Step 5 pivot Step 5: After the exchange, increment left marker, decrement right marker. 26 22 35 29 19 12 33 left right

  8. Quicksort Step 6 pivot Step 6: Element 35 belongs to RIGHT group. Element 12 belongs to LEFT group. Exchange, increment left, and decrement right. 26 22 35 29 19 12 33 left right pivot 26 22 12 29 19 35 33 left right

  9. Quicksort Step 7 pivot Step 7: Element 29 belongs to RIGHT. Element 19 belongs to LEFT. Exchange, increment left, decrement right. 26 22 12 29 19 35 33 right left pivot 26 22 12 19 29 35 33 right left

  10. Quicksort Step 8 pivot Step 8: When the left and right markers pass each other, we are done with the partition task. Swap the right with pivot. 26 22 12 19 29 35 33 right left pivot 26 19 22 12 29 35 33 LEFT RIGHT

  11. Quicksort Step 9 previous pivot Step 9: Apply Quicksort to the LEFT and RIGHT groups, recursively. Assemble parts when done 26 Quicksort Quicksort 19 22 12 29 35 33 pivot pivot 12 19 22 26 29 33 35 12 19 22 26 29 33 35

  12. Quicksort Efficiency The partitioning of an array into two parts is O(n) The number of recursive calls to Quicksort depends on how many times we can split the array into two groups. On average this is O (log2 n) The overall Quicksort efficiency is O(n) = n log2n What is the worst-case efficiency? Compare this to the worst case for the heapsort.

  13. Sort Algorithms • Quadratic in-place sort algorithms: • Insertion Sort O(n2) efficiency • Selection Sort O(n2) efficiency • Bubble Sort O(n2) efficiency • Recursive sort algorithms: • Mergesort O(n log2n) • Quicksort: avg: O(n log2n); worst-case: O(n2)) • Sort using a heap: • Heapsort O(n log2n)

More Related