SlideShare a Scribd company logo
1 of 38
INFORMATION AND TECHNOLOGY Branch Code : 016 
Data Structures Subject code : 2130702 
Presentation on 
Quick Sort and Binary Search 
By Divya S. Modi
Data Structure 
Quick Sort and Binary Search
INDEX 
Quick Sort 
Binary Search 
Summary 
References
Quick Sort 
Graphical Representation
Quick Sort 
• Basic Concept: divide and conquer 
• Select a pivot and split the data into two groups: (< pivot) and (> piv 
ot): 
Quicksort Concept 
(<pivot) 
LEFT group 
(> pivot) 
RIGHT group 
• Recursively apply Quicksort to the subgroups
Quick Sort 
Start with all data 
in an array, and 
consider it unsorted 
Quicksort Start 
Unsorted Array
Quick Sort 
Quicksort Step 1 
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. 
pivot 
26 33 35 29 19 
12 22
Quick Sort 
Quicksort Step 2 
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 
pivot 
26 33 35 29 19 
left 
12 22 
right
Quick Sort 
Step 3, 
If left element belongs 
to LEFT group, then 
increment left index. 
Quicksort Step 3 
If right index element belongs 
to RIGHT, then decrement right. 
Exchange when you find 
elements that belong to the other 
group. 
pivot 
26 33 35 29 19 
left 
12 22 
right
Quick Sort 
Step 4: 
Element 33 belongs 
to RIGHT group. 
Element 22 belongs 
to LEFT group. 
Exchange the two 
elements. 
Quicksort Step 4 
pivot 
26 33 35 29 19 
left 
12 22 
right 
pivot 
26 22 35 29 19 
left 
12 33 
right
Quick Sort 
Step 5: 
Quicksort Step 5 
After the exchange, 
increment left marker, 
decrement right marker. 
pivot 
26 22 35 29 19 
left 
left 
12 33 
right
Quick Sort 
Step 6: 
Element 35 belongs 
to RIGHT group. 
Element 12 belongs 
to LEFT group. 
Exchange, 
increment left, and 
decrement right. 
Quicksort Step 6 
26 22 35 29 19 
left 
12 33 
right 
pivot 
26 22 12 29 19 
35 33 
left right 
pivot
Quick Sort 
Step 7: 
Element 29 belongs 
to RIGHT. 
Element 19 belongs 
to LEFT. 
Exchange, 
increment left, 
decrement right. 
Quicksort Step 7 
26 22 12 29 19 
left 
pivot 
35 33 
right 
26 22 12 19 29 
left 
pivot 
35 33 
right
Quick Sort 
Quicksort Step 8 
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 
left 
pivot 
35 33 
right 
pivot 
26 
19 22 12 29 
35 
LEFT RIGHT
Quick Sort 
Step 9: 
Apply Quicksort 
to the LEFT and 
RIGHT groups, 
recursively. 
Quicksort Step 9 
Assemble parts when done 
previous pivot 
26 
Quicksort Quicksort 
19 22 12 29 
pivot 
26 
35 33 
pivot 
12 19 22 29 33 35 
12 19 22 26 29 33 35
Quick Sort 
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.
Quick Sort 
Best Case 
 We cut the array size in half each time 
 So the depth of the recursion in log2n 
 At each level of the recursion, all the partitions at that level do work th 
at is linear in n 
 O(log2n) * O(n) = O(n log2n) 
 Hence in the average case, quicksort has time complexity O(n log2n) 
 What about the worst case?
Quick Sort 
 In the worst case, partitioning always divides the size n array into th 
ese three parts: 
- A length one part, containing the pivot itself 
- A length zero part, and 
- A length n-1 part, containing everything else 
 We don’t recur on the zero-length part 
 Recurring on the length n-1 part requires (in the worst case) recurrin 
g to depth n-1 
Worst case
Quick Sort 
Worst case for quicksort 
 In the worst case, recursion may be n levels deep (for an array of 
size n) 
 But the partitioning work done at each level is still n 
 O(n) * O(n) = O(n2) 
 So worst case for Quicksort is O(n2) 
 When does this happen? 
- When the array is sorted to begin with!
Binary Search 
Problem: Search 
 We are given a list of records. 
 Each record has an associated key. 
 Give efficient algorithm for searching for a record containing a 
particular key. 
 Efficiency is quantified in terms of average time analysis (number of 
comparisons) to retrieve an item.
Binary Search 
Search 
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 700 ] 
Number 281942902 Number 701466868 Number 233667136 
Number 580625685 Number 506643548 
… 
Number 155778322 Number 580625685 
Each record in list has an associated key. 
In this example, the keys are ID numbers. 
Given a particular key, how can we efficiently 
retrieve the record from the list?
Binary Search 
Binary Search 
 Binary search is based on the “divide-and-conquer” strategy w 
hich works as follows: 
 Start by looking at the middle element of the array 
1. If the value it holds is lower than the search element, elimi 
nate the first half of the array from further consideratio 
n. 
2. If the value it holds is higher than the search element, 
eliminate the second half of the array from further 
consideration. 
 Repeat this process until the element is found, or until the enti 
re 
array has been eliminated.
Binary Search 
Binary Search 
 Perhaps we can do better than O(n) in the average case? 
 Assume that we are give an array of records that is sorted. For 
instance: 
 an array of records with integer keys sorted from smallest to 
largest (e.g., ID numbers), or 
 an array of records with string keys sorted in alphabetical order 
(e.g., names).
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Find approximate midpoint
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Is 7 = midpoint key? NO.
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Is 7 < midpoint key? YES.
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Search for the target in the area before midpoint.
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Find approximate midpoint
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Target = key of midpoint? NO.
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Target < key of midpoint? NO.
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Target > key of midpoint? YES.
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Search for the target in the area after midpoint.
Binary Search 
Example 
 Example: sorted array of integer keys. Target=7. 
[ 0 ] [ 1 ] 
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 
3 6 7 11 32 33 53 
Find approximate midpoint. 
Is target = midpoint key? YES.
Binary Search 
Efficiency of binary search 
 The maximum number of searches x necessary to find a name is the 
smallest integer that satisfies the inequality 2x > 10 or x = 4. 
 If n represents the number of names, the maximum number of searc 
hes x necessary to find a name is the smallest integer that satisfies th 
e inequality 2x > n. 
The maximum number of 
searches is the smallest integer 
greater than log n/log 2 
2x > n 
log (2x) > log n 
x log 2>log n
Binary Search 
Efficiency of binary search 
# of names 
Maximum sequential 
searches necessary 
Maximum binary 
searches necessary 
10 10 4 
100 100 7 
1,000 1,000 10 
5,000 5,000 13 
10,000 10,000 14 
50,000 50,000 16 
100,000 100,000 17 
1,000,000 1,000,000 20 
10,000,000 10,000,000 24 
1,000,000,000 1,000,000,000 30 
With the incredible speed of today’s computers, a binary 
search becomes necessary only when the number of 
names is large.
Binary Search
References 
 https://www.cs.auckland.ac.nz/~jmor159/PLDS210/qsort.html 
 http://en.wikipedia.org/wiki/Quicksort 
 http://www.cprogramming.com/tutorial/computersciencetheory/qui 
cksort.html
Quick sort and binary search

More Related Content

Recently uploaded

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 

Recently uploaded (20)

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Quick sort and binary search

  • 1. INFORMATION AND TECHNOLOGY Branch Code : 016 Data Structures Subject code : 2130702 Presentation on Quick Sort and Binary Search By Divya S. Modi
  • 2. Data Structure Quick Sort and Binary Search
  • 3. INDEX Quick Sort Binary Search Summary References
  • 4. Quick Sort Graphical Representation
  • 5. Quick Sort • Basic Concept: divide and conquer • Select a pivot and split the data into two groups: (< pivot) and (> piv ot): Quicksort Concept (<pivot) LEFT group (> pivot) RIGHT group • Recursively apply Quicksort to the subgroups
  • 6. Quick Sort Start with all data in an array, and consider it unsorted Quicksort Start Unsorted Array
  • 7. Quick Sort Quicksort Step 1 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. pivot 26 33 35 29 19 12 22
  • 8. Quick Sort Quicksort Step 2 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 pivot 26 33 35 29 19 left 12 22 right
  • 9. Quick Sort Step 3, If left element belongs to LEFT group, then increment left index. Quicksort Step 3 If right index element belongs to RIGHT, then decrement right. Exchange when you find elements that belong to the other group. pivot 26 33 35 29 19 left 12 22 right
  • 10. Quick Sort Step 4: Element 33 belongs to RIGHT group. Element 22 belongs to LEFT group. Exchange the two elements. Quicksort Step 4 pivot 26 33 35 29 19 left 12 22 right pivot 26 22 35 29 19 left 12 33 right
  • 11. Quick Sort Step 5: Quicksort Step 5 After the exchange, increment left marker, decrement right marker. pivot 26 22 35 29 19 left left 12 33 right
  • 12. Quick Sort Step 6: Element 35 belongs to RIGHT group. Element 12 belongs to LEFT group. Exchange, increment left, and decrement right. Quicksort Step 6 26 22 35 29 19 left 12 33 right pivot 26 22 12 29 19 35 33 left right pivot
  • 13. Quick Sort Step 7: Element 29 belongs to RIGHT. Element 19 belongs to LEFT. Exchange, increment left, decrement right. Quicksort Step 7 26 22 12 29 19 left pivot 35 33 right 26 22 12 19 29 left pivot 35 33 right
  • 14. Quick Sort Quicksort Step 8 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 left pivot 35 33 right pivot 26 19 22 12 29 35 LEFT RIGHT
  • 15. Quick Sort Step 9: Apply Quicksort to the LEFT and RIGHT groups, recursively. Quicksort Step 9 Assemble parts when done previous pivot 26 Quicksort Quicksort 19 22 12 29 pivot 26 35 33 pivot 12 19 22 29 33 35 12 19 22 26 29 33 35
  • 16. Quick Sort 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.
  • 17. Quick Sort Best Case  We cut the array size in half each time  So the depth of the recursion in log2n  At each level of the recursion, all the partitions at that level do work th at is linear in n  O(log2n) * O(n) = O(n log2n)  Hence in the average case, quicksort has time complexity O(n log2n)  What about the worst case?
  • 18. Quick Sort  In the worst case, partitioning always divides the size n array into th ese three parts: - A length one part, containing the pivot itself - A length zero part, and - A length n-1 part, containing everything else  We don’t recur on the zero-length part  Recurring on the length n-1 part requires (in the worst case) recurrin g to depth n-1 Worst case
  • 19. Quick Sort Worst case for quicksort  In the worst case, recursion may be n levels deep (for an array of size n)  But the partitioning work done at each level is still n  O(n) * O(n) = O(n2)  So worst case for Quicksort is O(n2)  When does this happen? - When the array is sorted to begin with!
  • 20. Binary Search Problem: Search  We are given a list of records.  Each record has an associated key.  Give efficient algorithm for searching for a record containing a particular key.  Efficiency is quantified in terms of average time analysis (number of comparisons) to retrieve an item.
  • 21. Binary Search Search [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 700 ] Number 281942902 Number 701466868 Number 233667136 Number 580625685 Number 506643548 … Number 155778322 Number 580625685 Each record in list has an associated key. In this example, the keys are ID numbers. Given a particular key, how can we efficiently retrieve the record from the list?
  • 22. Binary Search Binary Search  Binary search is based on the “divide-and-conquer” strategy w hich works as follows:  Start by looking at the middle element of the array 1. If the value it holds is lower than the search element, elimi nate the first half of the array from further consideratio n. 2. If the value it holds is higher than the search element, eliminate the second half of the array from further consideration.  Repeat this process until the element is found, or until the enti re array has been eliminated.
  • 23. Binary Search Binary Search  Perhaps we can do better than O(n) in the average case?  Assume that we are give an array of records that is sorted. For instance:  an array of records with integer keys sorted from smallest to largest (e.g., ID numbers), or  an array of records with string keys sorted in alphabetical order (e.g., names).
  • 24. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Find approximate midpoint
  • 25. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Is 7 = midpoint key? NO.
  • 26. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Is 7 < midpoint key? YES.
  • 27. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Search for the target in the area before midpoint.
  • 28. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Find approximate midpoint
  • 29. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Target = key of midpoint? NO.
  • 30. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Target < key of midpoint? NO.
  • 31. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Target > key of midpoint? YES.
  • 32. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Search for the target in the area after midpoint.
  • 33. Binary Search Example  Example: sorted array of integer keys. Target=7. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 3 6 7 11 32 33 53 Find approximate midpoint. Is target = midpoint key? YES.
  • 34. Binary Search Efficiency of binary search  The maximum number of searches x necessary to find a name is the smallest integer that satisfies the inequality 2x > 10 or x = 4.  If n represents the number of names, the maximum number of searc hes x necessary to find a name is the smallest integer that satisfies th e inequality 2x > n. The maximum number of searches is the smallest integer greater than log n/log 2 2x > n log (2x) > log n x log 2>log n
  • 35. Binary Search Efficiency of binary search # of names Maximum sequential searches necessary Maximum binary searches necessary 10 10 4 100 100 7 1,000 1,000 10 5,000 5,000 13 10,000 10,000 14 50,000 50,000 16 100,000 100,000 17 1,000,000 1,000,000 20 10,000,000 10,000,000 24 1,000,000,000 1,000,000,000 30 With the incredible speed of today’s computers, a binary search becomes necessary only when the number of names is large.
  • 37. References  https://www.cs.auckland.ac.nz/~jmor159/PLDS210/qsort.html  http://en.wikipedia.org/wiki/Quicksort  http://www.cprogramming.com/tutorial/computersciencetheory/qui cksort.html