Question: Does the choice of the pivot affect the running time of quick sort? Why or why not? It would help if you could provide examples or illustrations.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Big-O Solving (PYTHON)

Question: Does the choice of the pivot affect the running time of quick sort? Why or why not? It would help if you could provide examples or illustrations.

Given ONLY:

Quick Sort is another sorting algorithm that follows a divide-and-conquer approach. The algorithm can be summarized in 3 steps:

  1. pivot element is chosen, usually the first element.
  2. All elements smaller than the pivot are placed to the left of the pivot. This creates 2 partitions, elements greater than the pivot and elements less than the pivot.
  3. The 2 partitions are sorted using Quick Sort.

Sample code in python3:

   def quick_sort(arr):
     def quick_sort_r(arr, start, end):
       if end - start < 2:
         # single element base case
         return

       # choose a pivot
       pivot = start # you may choose other elements
       store = pivot+1 # index to store less than elements

       # for all elements after the pivot
       for i in range(pivot+1, end):
         if arr[i] < arr[pivot]:
           # if element is less than pivot
           arr[i], arr[store] = arr[store], arr[i] # swap
           store += 1 # increment store index

       # swap pivot with last element in less than partition
       arr[pivot], arr[store-1] = arr[store-1], arr[pivot]
       pivot = store-1 # new pivot index

       # Recursive Calls
       quick_sort_r(arr, start, pivot) # sort less than partition
       quick_sort_r(arr, pivot+1, end) # sort greater than partition
       return
   quick_sort_r(arr, 0, len(arr))

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Mergesort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education