SlideShare a Scribd company logo
4
Most read
5
Most read
8
Most read
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

More Related Content

What's hot (20)

PPT
3.8 quicksort
Krish_ver2
 
PPTX
Quick sort
Jehat Hassan
 
PPTX
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
PPTX
Sorting Algorithms
Pranay Neema
 
PPTX
Quick sort-Data Structure
Jeanie Arnoco
 
PDF
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
PPTX
Quick sort
amar kakde
 
PPTX
Algorithm analysis (All in one)
jehan1987
 
PPT
Algorithm: Quick-Sort
Tareq Hasan
 
PPTX
Insertion sort
almaqboli
 
PPTX
Computer Science-Data Structures :Abstract DataType (ADT)
St Mary's College,Thrissur,Kerala
 
PPT
Time complexity.ppt
YekoyeTigabuYeko
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PPTX
Counting Sort
Faiza Saleem
 
PPTX
Deque and its applications
Jsaddam Hussain
 
PPTX
Merge sort algorithm
Shubham Dwivedi
 
PPTX
Searching
Ashim Lamichhane
 
PPT
3.8 quick sort
Krish_ver2
 
PPT
Merge sort
Soumen Santra
 
3.8 quicksort
Krish_ver2
 
Quick sort
Jehat Hassan
 
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Sorting Algorithms
Pranay Neema
 
Quick sort-Data Structure
Jeanie Arnoco
 
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Quick sort
amar kakde
 
Algorithm analysis (All in one)
jehan1987
 
Algorithm: Quick-Sort
Tareq Hasan
 
Insertion sort
almaqboli
 
Computer Science-Data Structures :Abstract DataType (ADT)
St Mary's College,Thrissur,Kerala
 
Time complexity.ppt
YekoyeTigabuYeko
 
Sorting Algorithms
Mohammed Hussein
 
linear search and binary search
Zia Ush Shamszaman
 
Counting Sort
Faiza Saleem
 
Deque and its applications
Jsaddam Hussain
 
Merge sort algorithm
Shubham Dwivedi
 
Searching
Ashim Lamichhane
 
3.8 quick sort
Krish_ver2
 
Merge sort
Soumen Santra
 

Similar to Quick sort algorithn (20)

PPTX
quick sort by deepak.pptx
DeepakM509554
 
PPTX
CSE680-07QuickSort.pptx
DeepakM509554
 
PPT
lecture 7
sajinsc
 
PDF
Skiena algorithm 2007 lecture08 quicksort
zukun
 
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
PPT
MergesortQuickSort.ppt
AliAhmad38278
 
PPT
Lec35
Nikhil Chilwant
 
PPT
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
PPT
03_sorting and it's types with example .ppt
vanshii9976
 
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
RafishaikIT02044
 
PPTX
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
PPTX
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
 
PDF
lec 03wweweweweweweeweweweewewewewee.pdf
Huma Ayub
 
PDF
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
PPTX
ch16.pptx
lordaragorn2
 
PPTX
ch16 (1).pptx
lordaragorn2
 
PPTX
Lec 03 - Sorting.pptx
Dr. Shaukat Wasi
 
PDF
Sienna 4 divideandconquer
chidabdu
 
quick sort by deepak.pptx
DeepakM509554
 
CSE680-07QuickSort.pptx
DeepakM509554
 
lecture 7
sajinsc
 
Skiena algorithm 2007 lecture08 quicksort
zukun
 
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
MergesortQuickSort.ppt
AliAhmad38278
 
03_sorting123456789454545454545444543.ppt
ssuser7b9bda1
 
03_sorting and it's types with example .ppt
vanshii9976
 
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
RafishaikIT02044
 
09 QUICK SORT Design and Analysis of algorithms
syamalamaganti
 
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
 
lec 03wweweweweweweeweweweewewewewee.pdf
Huma Ayub
 
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
ch16.pptx
lordaragorn2
 
ch16 (1).pptx
lordaragorn2
 
Lec 03 - Sorting.pptx
Dr. Shaukat Wasi
 
Sienna 4 divideandconquer
chidabdu
 
Ad

More from Kumar (20)

PPT
Graphics devices
Kumar
 
PPT
Fill area algorithms
Kumar
 
PDF
region-filling
Kumar
 
PDF
Bresenham derivation
Kumar
 
PPT
Bresenham circles and polygons derication
Kumar
 
PPTX
Introductionto xslt
Kumar
 
PPTX
Extracting data from xml
Kumar
 
PPTX
Xml basics
Kumar
 
PPTX
XML Schema
Kumar
 
PPTX
Publishing xml
Kumar
 
PPTX
DTD
Kumar
 
PPTX
Applying xml
Kumar
 
PPTX
Introduction to XML
Kumar
 
PDF
How to deploy a j2ee application
Kumar
 
PDF
JNDI, JMS, JPA, XML
Kumar
 
PDF
EJB Fundmentals
Kumar
 
PDF
JSP and struts programming
Kumar
 
PDF
java servlet and servlet programming
Kumar
 
PDF
Introduction to JDBC and JDBC Drivers
Kumar
 
PDF
Introduction to J2EE
Kumar
 
Graphics devices
Kumar
 
Fill area algorithms
Kumar
 
region-filling
Kumar
 
Bresenham derivation
Kumar
 
Bresenham circles and polygons derication
Kumar
 
Introductionto xslt
Kumar
 
Extracting data from xml
Kumar
 
Xml basics
Kumar
 
XML Schema
Kumar
 
Publishing xml
Kumar
 
DTD
Kumar
 
Applying xml
Kumar
 
Introduction to XML
Kumar
 
How to deploy a j2ee application
Kumar
 
JNDI, JMS, JPA, XML
Kumar
 
EJB Fundmentals
Kumar
 
JSP and struts programming
Kumar
 
java servlet and servlet programming
Kumar
 
Introduction to JDBC and JDBC Drivers
Kumar
 
Introduction to J2EE
Kumar
 
Ad

Recently uploaded (20)

PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Dimensions of Societal Planning in Commonism
StefanMz
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 

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