SlideShare a Scribd company logo
12
Most read
13
Most read
15
Most read
By- Sumita Das
Presentation On Binary Search, Quick
Sort, Merge Sort And Problems
Created by Sumita Das
Binary Search
• If we have an ordered list and we know how
many things are in the list (i.e., number of records
in a file), we can use a different strategy.
• The binary search gets its name because the
algorithm continually divides the list into two
parts.
Created by Sumita Das
How a Binary Search Works
Always look at the center
value. Each time you get to
discard half of the remaining
list.
Created by Sumita Das
Initialize
FailureGet Midpoint
High < Low
< >Compare
Adjust High Adjust Low
Success=
Created by Sumita Das
Algorithm:
Low= 1; High = n;
while Low < High {
m = floor( (Low+High)/2 );
if k <= A[m]
then High = m - 1
else Low = m + 1
}
if A[Low] = k then j = Low else j = 0
Created by Sumita Das
•Similar to merge sort - divide-and-
conquer recursive algorithm
•One of the fastest sorting algorithms
•Average running time O(NlogN)
•Worst-case running time O(N2
)
Quick sort
Created by Sumita Das
Basic idea
1.Pick one element in the array, which will be
the pivot.
2.Make one pass through the array, called a
partition step, re-arranging the entries so that:
•the pivot is in its proper place.
•entries smaller than the pivot are to the left
of the pivot.
•entries larger than the pivot are to its right.
Created by Sumita Das
3.Recursively apply quick sort to the part
of the array that is to the left of the pivot,
and to the right part of the array.
Note:
Here we don't have the merge step, at
the end all the elements are in the proper
order.
Created by Sumita Das
Algorithm:
• Input: Array E and indices first, and last, s.t. elements
E[i] are defined for first ≤ i ≤ last
• Ouput: E[first], …, E[last] is a sorted rearrangement of
the array
• Void quickSort(Element[] E, int first, int last)
if (first < last)
Element pivotElement = E[first];
int splitPoint = partition(E, pivotElement, first,
last);
quickSort (E, first, splitPoint –1 );
quickSort (E, splitPoint +1, last );
return;
Created by Sumita Das
Merge Sort
• Uses recursion. Slightly faster than heap, but uses
twice as much memory from the 2nd
array.
• Sometimes called “divide and conquer” sort.
• Works by recursively splitting an array into two
equal halves, sorting the items, then re-merging
them back into a new array.
Created by Sumita Das
Algorithm:
• Split array A[1..n] in two and make copies of each half in
arrays B[1.. n/2 ] and C[1.. n/2 ]
• sort arrays B and C
• Merge sorted arrays B and C into array A
Created by Sumita Das
Using Divide and Conquer: Merge Sort
• Merge Sort Strategy
Sorted
Merge
Sorted Sorted
Sort recursively
by Mergesort
Sort recursively
by Mergesort
first last
(first + last)/2
Created by Sumita Das
Example : Binary Searching
14?
14?
Created by Sumita Das
Binary Searching
14
Created by Sumita Das
Example : Quick sort
Created by Sumita Das
Example : Merge Sort
– n Power of 2
1 2 3 4 5 6 7 8
q = 462317425
1 2 3 4
7425
5 6 7 8
6231
1 2
25
3 4
74
5 6
31
7 8
62
1
5
2
2
3
4
4
7 1
6
3
7
2
8
6
5
Divide
Example – n Power of 2
1
5
2
2
3
4
4
7 1
6
3
7
2
8
6
5
1 2 3 4 5 6 7 8
76543221
1 2 3 4
7542
5 6 7 8
6321
1 2
52
3 4
74
5 6
31
7 8
62
Conquer
and
Merge
Example – n Not a Power of 2
62537416274
1 2 3 4 5 6 7 8 9 10 11
q = 6
416274
1 2 3 4 5 6
62537
7 8 9 10 11
q = 9q = 3
274
1 2 3
416
4 5 6
537
7 8 9
62
10 11
74
1 2
2
3
16
4 5
4
6
37
7 8
5
9
2
10
6
11
4
1
7
2
6
4
1
5
7
7
3
8
Divide
Example – n Not a Power of 2
77665443221
1 2 3 4 5 6 7 8 9 10 11
764421
1 2 3 4 5 6
76532
7 8 9 10 11
742
1 2 3
641
4 5 6
753
7 8 9
62
10 11
2
3
4
6
5
9
2
10
6
11
4
1
7
2
6
4
1
5
7
7
3
8
74
1 2
61
4 5
73
7 8
Conquer
and
Merge
References
[1]Introduction to Algorithms 2nd ,Cormen, Leiserson, Rivest and
Stein, The MIT Press, 2001.
Thank you….
Created by Sumita Das

More Related Content

What's hot (20)

PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PDF
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
PPTX
Knapsack Problem
Jenny Galino
 
PPT
Algorithm: Quick-Sort
Tareq Hasan
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PDF
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
PPTX
Quick sort
amar kakde
 
PPTX
Topological Sorting
ShahDhruv21
 
PPTX
Data structures and algorithms
Julie Iskander
 
PPTX
Quick sort
Afaq Mansoor Khan
 
PDF
sparse matrix in data structure
MAHALAKSHMI P
 
PPTX
Breadth First Search & Depth First Search
Kevin Jadiya
 
PPTX
Merge sort algorithm
Shubham Dwivedi
 
PPTX
Stressen's matrix multiplication
Kumar
 
PPT
Selection sort
stella D
 
PPT
Shell sorting
TUC
 
PPTX
Selection sort 1
asmhemu
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PDF
Master theorem
fika sweety
 
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Knapsack Problem
Jenny Galino
 
Algorithm: Quick-Sort
Tareq Hasan
 
Sparse matrix and its representation data structure
Vardhil Patel
 
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Quick sort
amar kakde
 
Topological Sorting
ShahDhruv21
 
Data structures and algorithms
Julie Iskander
 
Quick sort
Afaq Mansoor Khan
 
sparse matrix in data structure
MAHALAKSHMI P
 
Breadth First Search & Depth First Search
Kevin Jadiya
 
Merge sort algorithm
Shubham Dwivedi
 
Stressen's matrix multiplication
Kumar
 
Selection sort
stella D
 
Shell sorting
TUC
 
Selection sort 1
asmhemu
 
Merge sort and quick sort
Shakila Mahjabin
 
Master theorem
fika sweety
 

Viewers also liked (20)

PDF
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
PPT
Meaghan technology report
Marq2014
 
PPT
Im260 presentation
mojo0402
 
PDF
eCommerce Report 2016
eCommerce Institute
 
PPTX
Difference b/w LED and LCD
Infant Gopinath
 
PPTX
Quicksort Presentation
irdginfo
 
PPTX
Digital marketing overview bl
Manish Chaudhary
 
PPTX
Quick sort-Data Structure
Jeanie Arnoco
 
DOCX
Ecommerce
Syed Ahmed
 
PPTX
Binary search
Gaurav Solanki
 
PDF
Linear search algorithm
NeoClassical
 
PPTX
Top 10 inventions that changed the world (elenka maam)
sheldon pereira
 
PPTX
Internet banking - College Project
Sheril Daniel
 
PPT
Quick Sort
priyankanaidu6
 
DOCX
Newspaper vs internet
Thinesh Pergas
 
PPTX
Co education
Shahzad Gul
 
PPTX
Coeducation
skmaken
 
PPT
Social Network Analysis
Giorgos Cheliotis
 
PPTX
Ecommerce final ppt
reemalmarri
 
PPT
Newspapers vs internet news
greatnewsupdate
 
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Meaghan technology report
Marq2014
 
Im260 presentation
mojo0402
 
eCommerce Report 2016
eCommerce Institute
 
Difference b/w LED and LCD
Infant Gopinath
 
Quicksort Presentation
irdginfo
 
Digital marketing overview bl
Manish Chaudhary
 
Quick sort-Data Structure
Jeanie Arnoco
 
Ecommerce
Syed Ahmed
 
Binary search
Gaurav Solanki
 
Linear search algorithm
NeoClassical
 
Top 10 inventions that changed the world (elenka maam)
sheldon pereira
 
Internet banking - College Project
Sheril Daniel
 
Quick Sort
priyankanaidu6
 
Newspaper vs internet
Thinesh Pergas
 
Co education
Shahzad Gul
 
Coeducation
skmaken
 
Social Network Analysis
Giorgos Cheliotis
 
Ecommerce final ppt
reemalmarri
 
Newspapers vs internet news
greatnewsupdate
 
Ad

Similar to Presentation on binary search, quick sort, merge sort and problems (20)

PPTX
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
LakshmiSamivel
 
PPTX
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
PDF
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
 
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
PPT
Lec 6 Divide and conquer of Data Structures & Algortihms
haseebanjum2611
 
PDF
Bs,qs,divide and conquer 1
subhashchandra197
 
PPTX
Quick Sort in data structure.pptx
ujjwalmatoliya
 
PPTX
Quick and Heap Sort with examples
Bst Ali
 
PPT
Designing Algorithms using Divide and Conquer Approach
Zain Ul Abiden
 
PPTX
Algorithm - Mergesort & Quicksort
Varendra University Rajshahi-bangladesh
 
PPTX
Chapter 4.2 - ADTree_Divide_n_Conquer 2021
g46179042
 
PDF
Analysis and design of algorithms part2
Deepak John
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PPTX
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
madhu614742
 
PDF
Unit ii divide and conquer -1
subhashchandra197
 
PPTX
data structures and algorithms Unit 3
infanciaj
 
PPT
mergesSort/mnt/data/Merge_Sort_Seminary_Presentation.pptx.ppt
545nithi
 
PPT
lecture14.pptJH5TVHY6B5TH JNR TN U5YNB JYM
denveramoson
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
LakshmiSamivel
 
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
 
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
Lec 6 Divide and conquer of Data Structures & Algortihms
haseebanjum2611
 
Bs,qs,divide and conquer 1
subhashchandra197
 
Quick Sort in data structure.pptx
ujjwalmatoliya
 
Quick and Heap Sort with examples
Bst Ali
 
Designing Algorithms using Divide and Conquer Approach
Zain Ul Abiden
 
Algorithm - Mergesort & Quicksort
Varendra University Rajshahi-bangladesh
 
Chapter 4.2 - ADTree_Divide_n_Conquer 2021
g46179042
 
Analysis and design of algorithms part2
Deepak John
 
Divide and conquer
Dr Shashikant Athawale
 
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
madhu614742
 
Unit ii divide and conquer -1
subhashchandra197
 
data structures and algorithms Unit 3
infanciaj
 
mergesSort/mnt/data/Merge_Sort_Seminary_Presentation.pptx.ppt
545nithi
 
lecture14.pptJH5TVHY6B5TH JNR TN U5YNB JYM
denveramoson
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
Ad

More from Sumita Das (10)

PPTX
Numerical on bisection method
Sumita Das
 
PPTX
Numerical on dichotomous search
Sumita Das
 
PPTX
Loop alignment
Sumita Das
 
PPTX
Maps in android
Sumita Das
 
PPTX
Hardware and software parallelism
Sumita Das
 
PPTX
Trusted systems1
Sumita Das
 
PPTX
Activity selection problem
Sumita Das
 
PPTX
Multiprotocol label switching
Sumita Das
 
PPTX
Asymptotic analysis of parallel programs
Sumita Das
 
PPTX
Distributed systems1
Sumita Das
 
Numerical on bisection method
Sumita Das
 
Numerical on dichotomous search
Sumita Das
 
Loop alignment
Sumita Das
 
Maps in android
Sumita Das
 
Hardware and software parallelism
Sumita Das
 
Trusted systems1
Sumita Das
 
Activity selection problem
Sumita Das
 
Multiprotocol label switching
Sumita Das
 
Asymptotic analysis of parallel programs
Sumita Das
 
Distributed systems1
Sumita Das
 

Recently uploaded (20)

PDF
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
PPTX
Seminar Description: YOLO v1 (You Only Look Once).pptx
abhijithpramod20002
 
PDF
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PPTX
Alan Turing - life and importance for all of us now
Pedro Concejero
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PPTX
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
PPTX
L300 Technical Slide Library_Feb 2025 microsoft purview
macarenabenitez6
 
PDF
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
PPTX
darshai cross section and river section analysis
muk7971
 
PDF
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
PPTX
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
Seminar Description: YOLO v1 (You Only Look Once).pptx
abhijithpramod20002
 
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
Alan Turing - life and importance for all of us now
Pedro Concejero
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
L300 Technical Slide Library_Feb 2025 microsoft purview
macarenabenitez6
 
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
darshai cross section and river section analysis
muk7971
 
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Functions in Python Programming Language
BeulahS2
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 

Presentation on binary search, quick sort, merge sort and problems

  • 1. By- Sumita Das Presentation On Binary Search, Quick Sort, Merge Sort And Problems Created by Sumita Das
  • 2. Binary Search • If we have an ordered list and we know how many things are in the list (i.e., number of records in a file), we can use a different strategy. • The binary search gets its name because the algorithm continually divides the list into two parts. Created by Sumita Das
  • 3. How a Binary Search Works Always look at the center value. Each time you get to discard half of the remaining list. Created by Sumita Das
  • 4. Initialize FailureGet Midpoint High < Low < >Compare Adjust High Adjust Low Success= Created by Sumita Das
  • 5. Algorithm: Low= 1; High = n; while Low < High { m = floor( (Low+High)/2 ); if k <= A[m] then High = m - 1 else Low = m + 1 } if A[Low] = k then j = Low else j = 0 Created by Sumita Das
  • 6. •Similar to merge sort - divide-and- conquer recursive algorithm •One of the fastest sorting algorithms •Average running time O(NlogN) •Worst-case running time O(N2 ) Quick sort Created by Sumita Das
  • 7. Basic idea 1.Pick one element in the array, which will be the pivot. 2.Make one pass through the array, called a partition step, re-arranging the entries so that: •the pivot is in its proper place. •entries smaller than the pivot are to the left of the pivot. •entries larger than the pivot are to its right. Created by Sumita Das
  • 8. 3.Recursively apply quick sort to the part of the array that is to the left of the pivot, and to the right part of the array. Note: Here we don't have the merge step, at the end all the elements are in the proper order. Created by Sumita Das
  • 9. Algorithm: • Input: Array E and indices first, and last, s.t. elements E[i] are defined for first ≤ i ≤ last • Ouput: E[first], …, E[last] is a sorted rearrangement of the array • Void quickSort(Element[] E, int first, int last) if (first < last) Element pivotElement = E[first]; int splitPoint = partition(E, pivotElement, first, last); quickSort (E, first, splitPoint –1 ); quickSort (E, splitPoint +1, last ); return; Created by Sumita Das
  • 10. Merge Sort • Uses recursion. Slightly faster than heap, but uses twice as much memory from the 2nd array. • Sometimes called “divide and conquer” sort. • Works by recursively splitting an array into two equal halves, sorting the items, then re-merging them back into a new array. Created by Sumita Das
  • 11. Algorithm: • Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] • sort arrays B and C • Merge sorted arrays B and C into array A Created by Sumita Das
  • 12. Using Divide and Conquer: Merge Sort • Merge Sort Strategy Sorted Merge Sorted Sorted Sort recursively by Mergesort Sort recursively by Mergesort first last (first + last)/2 Created by Sumita Das
  • 13. Example : Binary Searching 14? 14? Created by Sumita Das
  • 15. Example : Quick sort Created by Sumita Das
  • 16. Example : Merge Sort – n Power of 2 1 2 3 4 5 6 7 8 q = 462317425 1 2 3 4 7425 5 6 7 8 6231 1 2 25 3 4 74 5 6 31 7 8 62 1 5 2 2 3 4 4 7 1 6 3 7 2 8 6 5 Divide
  • 17. Example – n Power of 2 1 5 2 2 3 4 4 7 1 6 3 7 2 8 6 5 1 2 3 4 5 6 7 8 76543221 1 2 3 4 7542 5 6 7 8 6321 1 2 52 3 4 74 5 6 31 7 8 62 Conquer and Merge
  • 18. Example – n Not a Power of 2 62537416274 1 2 3 4 5 6 7 8 9 10 11 q = 6 416274 1 2 3 4 5 6 62537 7 8 9 10 11 q = 9q = 3 274 1 2 3 416 4 5 6 537 7 8 9 62 10 11 74 1 2 2 3 16 4 5 4 6 37 7 8 5 9 2 10 6 11 4 1 7 2 6 4 1 5 7 7 3 8 Divide
  • 19. Example – n Not a Power of 2 77665443221 1 2 3 4 5 6 7 8 9 10 11 764421 1 2 3 4 5 6 76532 7 8 9 10 11 742 1 2 3 641 4 5 6 753 7 8 9 62 10 11 2 3 4 6 5 9 2 10 6 11 4 1 7 2 6 4 1 5 7 7 3 8 74 1 2 61 4 5 73 7 8 Conquer and Merge
  • 20. References [1]Introduction to Algorithms 2nd ,Cormen, Leiserson, Rivest and Stein, The MIT Press, 2001.