SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...Umesh Kumar
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentationirdginfo
 
Insertion sort
Insertion sortInsertion sort
Insertion sortalmaqboli
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sortKrish_ver2
 

Was ist angesagt? (20)

Quick sort
Quick sortQuick sort
Quick sort
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Quick sort
Quick sortQuick sort
Quick sort
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 

Ähnlich wie 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
 
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
 

Ähnlich wie 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
 
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
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Kürzlich hochgeladen (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

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