SlideShare une entreprise Scribd logo
1  sur  8
Télécharger pour lire hors ligne
Quick Sort Algorithm
• Simple, fast, widely used in practice.
• Can be done “in place;” no extra space.
• General Form:
1. Partition: Divide into two subarrays, L and R;
elements in L are all smaller than those in R.
2. Recurse: Sort L and R recursively.
3. Combine: Append R to the end of L.
• Partition (A, p, q, i) partitions A with pivot A[i].

Subhash Suri

UC Santa Barbara
Partition
• Partition returns the index of the cell containing
the pivot in the reorganized array.
11

4

9

7

3

10

2

6

13

21

8

• Example: Partition (A, 0, 10, 3).
• 4, 3, 2, 6, 7, 11, 9, 10, 13, 21, 8

Subhash Suri

UC Santa Barbara
Quick Sort Algorithm
• QuickSort (A, p, q) sorts the subarray A[p · · · q].
• Initial call with p = 0 and q = n − 1.
QuickSort(A, p, q)
if p ≥ q then return
i ← random(p, q)
r ← Partition(A, p, q, i)
Quicksort (A, p, r − 1)
Quicksort (A, r + 1, q)

Subhash Suri

UC Santa Barbara
Analysis of QuickSort
• Lucky Case: Each Partition splits array in halves.
We get T (n) = 2T (n/2) + Θ(n) = Θ(n log n).
• Unlucky Case: Each partition gives unbalanced
split. We get T (n) = T (n − 1) + Θ(n) = Θ(n2).
• In worst case, Quick Sort as bad as BubbleSort.
The worst-case occurs when the list is already
sorted, and the last element chosen as pivot.
• But, while BubbleSort always performs poorly on
certain inputs, because of random pivot,
QuickSort has a chance of doing much better.
Subhash Suri

UC Santa Barbara
Analyzing QuickSort
• T (n): runtime of randomized QuickSort.
• Assume all elements are distinct.
• Recurrence for T (n) depends on two subproblem
sizes, which depend on random partition element.
• If pivot is i smallest element, then exactly (i − 1)
items in L and (n − i) in R. Call it an i-split.
• What’s the probability of i-split?
• Each element equally likely to be chosen as pivot,
1
so the answer is n .
Subhash Suri

UC Santa Barbara
Solving the Recurrence
n

1
T (n) =
(runtime with i-split) + n + 1
n
i=1
n

1
=
(T (i − 1) + T (n − i)) + n + 1
n i=1
n

2
T (i − 1) + n + 1
=
n i=1
2
=
n

Subhash Suri

n−1

T (i) + n + 1
i=0

UC Santa Barbara
Solving the Recurrence
• Multiply both sides by n. Subtract the same
formula for n − 1.
n−1

T (i) + n2 + n

nT (n) = 2
i=0
n−2

T (i) + (n − 1)2 + (n − 1)

(n − 1)T (n − 1) = 2
i=0

Subhash Suri

UC Santa Barbara
Solving the Recurrence
nT (n) = (n + 1)T (n − 1) + 2n
T (n − 1)
2
T (n)
=
+
n+1
n
n+1
T (n − 2) 2
2
=
+ +
n−1
n n+1
.
.
n
2
T (2)
+
=
3
i
i=3
= Θ(1) + 2 ln n
• Thus, T (n) ≤ 2(n + 1) ln n.
Subhash Suri

UC Santa Barbara

Contenu connexe

Tendances (20)

Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Quick sort
Quick sortQuick sort
Quick sort
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Binary search
Binary searchBinary search
Binary search
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Queues
QueuesQueues
Queues
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Quick sort
Quick sortQuick sort
Quick sort
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Recursion
RecursionRecursion
Recursion
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 

Similaire à Quick sort algorithn

quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
lecture 7
lecture 7lecture 7
lecture 7sajinsc
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
presentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptpresentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptajiths82
 
MergesortQuickSort.ppt
MergesortQuickSort.pptMergesortQuickSort.ppt
MergesortQuickSort.pptAliAhmad38278
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modifiedInbok Lee
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2Deepak John
 

Similaire à Quick sort algorithn (20)

quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
sorting
sortingsorting
sorting
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
lecture 7
lecture 7lecture 7
lecture 7
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
presentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.pptpresentation_mergesortquicksort_1458716068_193111.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
 
MergesortQuickSort.ppt
MergesortQuickSort.pptMergesortQuickSort.ppt
MergesortQuickSort.ppt
 
Lec35
Lec35Lec35
Lec35
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
ch16.pptx
ch16.pptxch16.pptx
ch16.pptx
 
ch16 (1).pptx
ch16 (1).pptxch16 (1).pptx
ch16 (1).pptx
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Lca seminar modified
Lca seminar modifiedLca seminar modified
Lca seminar modified
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 

Plus de Kumar

Graphics devices
Graphics devicesGraphics devices
Graphics devicesKumar
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithmsKumar
 
region-filling
region-fillingregion-filling
region-fillingKumar
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivationKumar
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons dericationKumar
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xsltKumar
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xmlKumar
 
Xml basics
Xml basicsXml basics
Xml basicsKumar
 
XML Schema
XML SchemaXML Schema
XML SchemaKumar
 
Publishing xml
Publishing xmlPublishing xml
Publishing xmlKumar
 
Applying xml
Applying xmlApplying xml
Applying xmlKumar
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XMLKumar
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee applicationKumar
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLKumar
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB FundmentalsKumar
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programmingKumar
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programmingKumar
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversKumar
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EEKumar
 

Plus de Kumar (20)

Graphics devices
Graphics devicesGraphics devices
Graphics devices
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
 
region-filling
region-fillingregion-filling
region-filling
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Xml basics
Xml basicsXml basics
Xml basics
 
XML Schema
XML SchemaXML Schema
XML Schema
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
 
DTD
DTDDTD
DTD
 
Applying xml
Applying xmlApplying xml
Applying xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee application
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programming
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EE
 

Dernier

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 

Dernier (20)

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 

Quick sort algorithn

  • 1. Quick Sort Algorithm • Simple, fast, widely used in practice. • Can be done “in place;” no extra space. • General Form: 1. Partition: Divide into two subarrays, L and R; elements in L are all smaller than those in R. 2. Recurse: Sort L and R recursively. 3. Combine: Append R to the end of L. • Partition (A, p, q, i) partitions A with pivot A[i]. Subhash Suri UC Santa Barbara
  • 2. Partition • Partition returns the index of the cell containing the pivot in the reorganized array. 11 4 9 7 3 10 2 6 13 21 8 • Example: Partition (A, 0, 10, 3). • 4, 3, 2, 6, 7, 11, 9, 10, 13, 21, 8 Subhash Suri UC Santa Barbara
  • 3. Quick Sort Algorithm • QuickSort (A, p, q) sorts the subarray A[p · · · q]. • Initial call with p = 0 and q = n − 1. QuickSort(A, p, q) if p ≥ q then return i ← random(p, q) r ← Partition(A, p, q, i) Quicksort (A, p, r − 1) Quicksort (A, r + 1, q) Subhash Suri UC Santa Barbara
  • 4. Analysis of QuickSort • Lucky Case: Each Partition splits array in halves. We get T (n) = 2T (n/2) + Θ(n) = Θ(n log n). • Unlucky Case: Each partition gives unbalanced split. We get T (n) = T (n − 1) + Θ(n) = Θ(n2). • In worst case, Quick Sort as bad as BubbleSort. The worst-case occurs when the list is already sorted, and the last element chosen as pivot. • But, while BubbleSort always performs poorly on certain inputs, because of random pivot, QuickSort has a chance of doing much better. Subhash Suri UC Santa Barbara
  • 5. Analyzing QuickSort • T (n): runtime of randomized QuickSort. • Assume all elements are distinct. • Recurrence for T (n) depends on two subproblem sizes, which depend on random partition element. • If pivot is i smallest element, then exactly (i − 1) items in L and (n − i) in R. Call it an i-split. • What’s the probability of i-split? • Each element equally likely to be chosen as pivot, 1 so the answer is n . Subhash Suri UC Santa Barbara
  • 6. Solving the Recurrence n 1 T (n) = (runtime with i-split) + n + 1 n i=1 n 1 = (T (i − 1) + T (n − i)) + n + 1 n i=1 n 2 T (i − 1) + n + 1 = n i=1 2 = n Subhash Suri n−1 T (i) + n + 1 i=0 UC Santa Barbara
  • 7. Solving the Recurrence • Multiply both sides by n. Subtract the same formula for n − 1. n−1 T (i) + n2 + n nT (n) = 2 i=0 n−2 T (i) + (n − 1)2 + (n − 1) (n − 1)T (n − 1) = 2 i=0 Subhash Suri UC Santa Barbara
  • 8. Solving the Recurrence nT (n) = (n + 1)T (n − 1) + 2n T (n − 1) 2 T (n) = + n+1 n n+1 T (n − 2) 2 2 = + + n−1 n n+1 . . n 2 T (2) + = 3 i i=3 = Θ(1) + 2 ln n • Thus, T (n) ≤ 2(n + 1) ln n. Subhash Suri UC Santa Barbara