SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
8/31/2020
1
• Applications: Binary Search, Quick sort
• Material prepared by
• Dr. N. Subhash Chandra
• Source: Web resources , Computer Algorithms by
Sahni
1
What is Divide-and-Conquer?
 Divide: Divide the problem into a number of
smaller sub-problems ideally of about the same size.
 Conquer: The smaller sub-problems are solved,
typically recursively. If the sub-problem sizes are
small enough, just solve the sub-problems in a
straight forward manner.
 Combine: If necessary, the solution obtained the
smaller problems are connected to get the solution to
the originalproblem.
2
1
2
8/31/2020
2
Contd ……
Divide and Conquer typical case
3
Control abstraction for DandC
Algorithm DandC(p)
{
if small (p)then
return S(p)
else
{
Divide P into small instances P1, P2, P3……..Pk,k≥1;
Apply DandC to each of thesesub-problems;
return combine (DandC(P1), DandC(P1),…. (DandC(Pk);
}
}
4
3
4
8/31/2020
3
5
Application - Binary Search
Procedure
• The method starts with looking at the middle
element of the list. If it matches with the key
element, then search iscomplete.
• If the key element is less than the middle
element, then the search continues with the
first half of the list.
• If the key element is greater than the middle
element, then the search continues with the
second half of the list.
• This process continues until the key element is
found or the search fails indicating that the key
is not there in thelist.
6
5
6
8/31/2020
4
Contd ...
Example: -4, -1, 0, 5, 10, 18, 32, 33, 98, 147, 154, 198, 250, 500.
-4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500
Sol: The given list of elements are:
3 4 5 6 7 8 9 10 11
Low
0 1 2
High
12 13 14
Searching key '-1': Here the key to search is '-1'
First calculate mid;
Mid = (low + high)/2
= (0 +14) /2 =7
-4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500
3 4 5
Low
0 1 2
High
12 13 14
Mid
6 7 8 9 10 11
< First Half > < Second Half >
7
Contd …
-4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500
3 12 13 14
Low
0 1 2
-1 < 32 Now mid = (0+6)/2 =3
High
4 5 6 7 8 9 10 11
Low
0 1 2 Mid 3 High
4 5 6 7 8 9 10 1112 13 14
-4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500
< First Half > < Second Half >
-1 < 5 Now mid = (0+2)/2 =1
-4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500
4 5 6 7 8 9 10 11 12 13 14
L ow High
0 1 2 3
Low
0 Mid
1
High
2 3 4 5 6 7 8 9 10 11 12 13 14
-4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500
Here, the search key -1 is found at position1
8
7
8
8/31/2020
5
9
10
9
10
8/31/2020
6
Time complexity of Binary Search
11
Quick Sort
• Application - Quick Sort
• The entire list is partitioned into two sub-list one
of which holds values smaller than the specified
value, say pivot, based on which the partition is
made and another sub-list holds values greater
than the pivot value.
• Quick sort partitions an array and then calls
itself recursively twice to sort the two resulting
subarrays.
12
11
12
8/31/2020
7
Example
Eg: 12 6 18 4 9 8 2 15
• In this example, we use the first number, say 12, which is called
the pivot (rotate) element.
Procedure
[1] [2] [3] [4] [5] [6] [7] [8] [9]
12 6 18 4 9 8 2 15 
• Let ‘i’ be the position of the second element and ‘j’ be the
position of the last element. i.e. i =2 and j =8, in this
example.
• Assume that a [n+1] =, where ‘a’ is an array of size n.
i j
2 8
13
Contd …
• First scan the list from left to right (from i to j) can
compare each and every element with the pivot. This
process continues until an element found which is
greater than or equal to pivot element. If such an
element found, then that element position becomes the
value of ‘i’.
• Now scan the list from right to left (from j to i) and
compare each and every element with the pivot. This
process continues until an element found which is less
than or equal to pivot element. If such an element finds
then thatelement’s position become ‘j’ value.
• Now compare ‘i’ and ‘j’. If i <j, then swap a[i] and a[j].
Otherwiseswap pivotelementand a[j].
14
13
14
8/31/2020
8
Contd …
[1] [2] [3] [4] [5] [6] [7] [8] [9] i j
12 6 18 4 9 8 2 15  2 8
12 6 18 4 9 8 2 15  3 7
12 6 2 4 9 8 18 15  7 6
Since i = 7 j =6, then swap pivot element and 6th element ( jth element), weget
8 6 2 4 9 12 18 15
Thus pivot reaches its original position. The elements on left to the right pivot
are smaller than pivot (12) and right to pivot are greater pivot (12).
8 6 2 4
Sublist 1
9 12 18 15
Sublist 2
Now take sub-list1 and sub-list2 and apply the above process recursively,
at last we get sorted list. 15
16
15
16
8/31/2020
9
17
18
17
18
8/31/2020
10
Randomized Quick sort
19
• Pros and Cons
• Advantage: It is the fastest sorting method
among all the sorting methods.
• Disadvantage: It is somewhat complex and
little difficult to implement than other sorting.
20
19
20
8/31/2020
11
21
22
21
22
8/31/2020
12
Time complexity of Quick sort
23
24
23
24
8/31/2020
13
25
26
25
26
8/31/2020
14
TimeComplexity
Best Case: In best case, consider the following twoassumptions-
1. The pivot, which we choose, will always be swapped into the exactly the
middle of the list. And also consider pivot will have an equal number of
elements both to its left and right.
2. The number of elements in the list is a powerof 2 i.e. n= 2y
.
27
Contd…..
Worst Case: Assume that the pivot partition the list into two parts, so that
one of the partition has no elements while the other has all the other
elements.
28
27
28
8/31/2020
15
Contd…..
Average Case: Assume that the pivot partition the list into two parts, so that
one of the partition has no elements while the other has all the other
elements.
Quick Sort
Best Case O(n log n)
Average Case O(n log n)
Worst Case O(n2)
29
29

Contenu connexe

Tendances

Pattern recognition binoy k means clustering
Pattern recognition binoy  k means clusteringPattern recognition binoy  k means clustering
Pattern recognition binoy k means clustering108kaushik
 
Large Scale Data Clustering: an overview
Large Scale Data Clustering: an overviewLarge Scale Data Clustering: an overview
Large Scale Data Clustering: an overviewVahid Mirjalili
 
Stevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsStevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsJames Stevens
 
IRJET- New Proposed Method for Solving Assignment Problem and Comparative Stu...
IRJET- New Proposed Method for Solving Assignment Problem and Comparative Stu...IRJET- New Proposed Method for Solving Assignment Problem and Comparative Stu...
IRJET- New Proposed Method for Solving Assignment Problem and Comparative Stu...IRJET Journal
 
K means Clustering
K means ClusteringK means Clustering
K means ClusteringEdureka!
 
"k-means-clustering" presentation @ Papers We Love Bucharest
"k-means-clustering" presentation @ Papers We Love Bucharest"k-means-clustering" presentation @ Papers We Love Bucharest
"k-means-clustering" presentation @ Papers We Love BucharestAdrian Florea
 
An improvement in k mean clustering algorithm using better time and accuracy
An improvement in k mean clustering algorithm using better time and accuracyAn improvement in k mean clustering algorithm using better time and accuracy
An improvement in k mean clustering algorithm using better time and accuracyijpla
 
Optimising Data Using K-Means Clustering Algorithm
Optimising Data Using K-Means Clustering AlgorithmOptimising Data Using K-Means Clustering Algorithm
Optimising Data Using K-Means Clustering AlgorithmIJERA Editor
 
K-MEDOIDS CLUSTERING USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
K-MEDOIDS CLUSTERING  USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...K-MEDOIDS CLUSTERING  USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
K-MEDOIDS CLUSTERING USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...ijscmc
 
Current clustering techniques
Current clustering techniquesCurrent clustering techniques
Current clustering techniquesPoonam Kshirsagar
 
The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)theijes
 
Subset sum problem(dp)
Subset sum problem(dp)Subset sum problem(dp)
Subset sum problem(dp)VishnuPratap7
 
K-Means clustring @jax
K-Means clustring @jaxK-Means clustring @jax
K-Means clustring @jaxAjay Iet
 
DATA MINING:Clustering Types
DATA MINING:Clustering TypesDATA MINING:Clustering Types
DATA MINING:Clustering TypesAshwin Shenoy M
 
Premeditated Initial Points for K-Means Clustering
Premeditated Initial Points for K-Means ClusteringPremeditated Initial Points for K-Means Clustering
Premeditated Initial Points for K-Means ClusteringIJCSIS Research Publications
 

Tendances (20)

Pattern recognition binoy k means clustering
Pattern recognition binoy  k means clusteringPattern recognition binoy  k means clustering
Pattern recognition binoy k means clustering
 
Large Scale Data Clustering: an overview
Large Scale Data Clustering: an overviewLarge Scale Data Clustering: an overview
Large Scale Data Clustering: an overview
 
Stevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsStevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting Algorithms
 
IRJET- New Proposed Method for Solving Assignment Problem and Comparative Stu...
IRJET- New Proposed Method for Solving Assignment Problem and Comparative Stu...IRJET- New Proposed Method for Solving Assignment Problem and Comparative Stu...
IRJET- New Proposed Method for Solving Assignment Problem and Comparative Stu...
 
K means Clustering
K means ClusteringK means Clustering
K means Clustering
 
"k-means-clustering" presentation @ Papers We Love Bucharest
"k-means-clustering" presentation @ Papers We Love Bucharest"k-means-clustering" presentation @ Papers We Love Bucharest
"k-means-clustering" presentation @ Papers We Love Bucharest
 
An improvement in k mean clustering algorithm using better time and accuracy
An improvement in k mean clustering algorithm using better time and accuracyAn improvement in k mean clustering algorithm using better time and accuracy
An improvement in k mean clustering algorithm using better time and accuracy
 
Optimising Data Using K-Means Clustering Algorithm
Optimising Data Using K-Means Clustering AlgorithmOptimising Data Using K-Means Clustering Algorithm
Optimising Data Using K-Means Clustering Algorithm
 
Improved k-means
Improved k-meansImproved k-means
Improved k-means
 
K means clustring @jax
K means clustring @jaxK means clustring @jax
K means clustring @jax
 
K-MEDOIDS CLUSTERING USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
K-MEDOIDS CLUSTERING  USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...K-MEDOIDS CLUSTERING  USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
K-MEDOIDS CLUSTERING USING PARTITIONING AROUND MEDOIDS FOR PERFORMING FACE R...
 
Current clustering techniques
Current clustering techniquesCurrent clustering techniques
Current clustering techniques
 
The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)The International Journal of Engineering and Science (The IJES)
The International Journal of Engineering and Science (The IJES)
 
Subset sum problem(dp)
Subset sum problem(dp)Subset sum problem(dp)
Subset sum problem(dp)
 
K-Means clustring @jax
K-Means clustring @jaxK-Means clustring @jax
K-Means clustring @jax
 
50120140505013
5012014050501350120140505013
50120140505013
 
DATA MINING:Clustering Types
DATA MINING:Clustering TypesDATA MINING:Clustering Types
DATA MINING:Clustering Types
 
Lect4
Lect4Lect4
Lect4
 
Premeditated Initial Points for K-Means Clustering
Premeditated Initial Points for K-Means ClusteringPremeditated Initial Points for K-Means Clustering
Premeditated Initial Points for K-Means Clustering
 
K mean-clustering
K mean-clusteringK mean-clustering
K mean-clustering
 

Similaire à Unit ii divide and conquer -1

Divide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotDivide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotCiaraAbila
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxnikshaikh786
 
UNIT I_PSPP - Illustrative Problems (1).pptx
UNIT I_PSPP - Illustrative Problems (1).pptxUNIT I_PSPP - Illustrative Problems (1).pptx
UNIT I_PSPP - Illustrative Problems (1).pptxRSathyaPriyaCSEKIOT
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applicationsyazad dumasia
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptxDIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptxLakshmiSamivel
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 

Similaire à Unit ii divide and conquer -1 (20)

Quick sort.pptx
Quick sort.pptxQuick sort.pptx
Quick sort.pptx
 
s4_quick_sort.ppt
s4_quick_sort.ppts4_quick_sort.ppt
s4_quick_sort.ppt
 
Divide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotDivide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing Pivot
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
 
Sorting
SortingSorting
Sorting
 
UNIT I_PSPP - Illustrative Problems (1).pptx
UNIT I_PSPP - Illustrative Problems (1).pptxUNIT I_PSPP - Illustrative Problems (1).pptx
UNIT I_PSPP - Illustrative Problems (1).pptx
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Lec35
Lec35Lec35
Lec35
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptxDIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 

Plus de subhashchandra197

Plus de subhashchandra197 (11)

Unit ii divide and conquer -4
Unit ii divide and conquer -4Unit ii divide and conquer -4
Unit ii divide and conquer -4
 
Unit ii divide and conquer -3
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3
 
Unit ii divide and conquer -2
Unit ii divide and conquer -2Unit ii divide and conquer -2
Unit ii divide and conquer -2
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
P,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problemsP,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problems
 
Turing machine material
Turing machine materialTuring machine material
Turing machine material
 
Turing machine types
Turing machine typesTuring machine types
Turing machine types
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Disjoint sets union, find
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
 

Dernier

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
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
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
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
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Dernier (20)

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
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...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
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, ...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 

Unit ii divide and conquer -1

  • 1. 8/31/2020 1 • Applications: Binary Search, Quick sort • Material prepared by • Dr. N. Subhash Chandra • Source: Web resources , Computer Algorithms by Sahni 1 What is Divide-and-Conquer?  Divide: Divide the problem into a number of smaller sub-problems ideally of about the same size.  Conquer: The smaller sub-problems are solved, typically recursively. If the sub-problem sizes are small enough, just solve the sub-problems in a straight forward manner.  Combine: If necessary, the solution obtained the smaller problems are connected to get the solution to the originalproblem. 2 1 2
  • 2. 8/31/2020 2 Contd …… Divide and Conquer typical case 3 Control abstraction for DandC Algorithm DandC(p) { if small (p)then return S(p) else { Divide P into small instances P1, P2, P3……..Pk,k≥1; Apply DandC to each of thesesub-problems; return combine (DandC(P1), DandC(P1),…. (DandC(Pk); } } 4 3 4
  • 3. 8/31/2020 3 5 Application - Binary Search Procedure • The method starts with looking at the middle element of the list. If it matches with the key element, then search iscomplete. • If the key element is less than the middle element, then the search continues with the first half of the list. • If the key element is greater than the middle element, then the search continues with the second half of the list. • This process continues until the key element is found or the search fails indicating that the key is not there in thelist. 6 5 6
  • 4. 8/31/2020 4 Contd ... Example: -4, -1, 0, 5, 10, 18, 32, 33, 98, 147, 154, 198, 250, 500. -4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500 Sol: The given list of elements are: 3 4 5 6 7 8 9 10 11 Low 0 1 2 High 12 13 14 Searching key '-1': Here the key to search is '-1' First calculate mid; Mid = (low + high)/2 = (0 +14) /2 =7 -4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500 3 4 5 Low 0 1 2 High 12 13 14 Mid 6 7 8 9 10 11 < First Half > < Second Half > 7 Contd … -4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500 3 12 13 14 Low 0 1 2 -1 < 32 Now mid = (0+6)/2 =3 High 4 5 6 7 8 9 10 11 Low 0 1 2 Mid 3 High 4 5 6 7 8 9 10 1112 13 14 -4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500 < First Half > < Second Half > -1 < 5 Now mid = (0+2)/2 =1 -4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500 4 5 6 7 8 9 10 11 12 13 14 L ow High 0 1 2 3 Low 0 Mid 1 High 2 3 4 5 6 7 8 9 10 11 12 13 14 -4 -1 0 5 10 18 27 32 33 98 147 154 198 250 500 Here, the search key -1 is found at position1 8 7 8
  • 6. 8/31/2020 6 Time complexity of Binary Search 11 Quick Sort • Application - Quick Sort • The entire list is partitioned into two sub-list one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another sub-list holds values greater than the pivot value. • Quick sort partitions an array and then calls itself recursively twice to sort the two resulting subarrays. 12 11 12
  • 7. 8/31/2020 7 Example Eg: 12 6 18 4 9 8 2 15 • In this example, we use the first number, say 12, which is called the pivot (rotate) element. Procedure [1] [2] [3] [4] [5] [6] [7] [8] [9] 12 6 18 4 9 8 2 15  • Let ‘i’ be the position of the second element and ‘j’ be the position of the last element. i.e. i =2 and j =8, in this example. • Assume that a [n+1] =, where ‘a’ is an array of size n. i j 2 8 13 Contd … • First scan the list from left to right (from i to j) can compare each and every element with the pivot. This process continues until an element found which is greater than or equal to pivot element. If such an element found, then that element position becomes the value of ‘i’. • Now scan the list from right to left (from j to i) and compare each and every element with the pivot. This process continues until an element found which is less than or equal to pivot element. If such an element finds then thatelement’s position become ‘j’ value. • Now compare ‘i’ and ‘j’. If i <j, then swap a[i] and a[j]. Otherwiseswap pivotelementand a[j]. 14 13 14
  • 8. 8/31/2020 8 Contd … [1] [2] [3] [4] [5] [6] [7] [8] [9] i j 12 6 18 4 9 8 2 15  2 8 12 6 18 4 9 8 2 15  3 7 12 6 2 4 9 8 18 15  7 6 Since i = 7 j =6, then swap pivot element and 6th element ( jth element), weget 8 6 2 4 9 12 18 15 Thus pivot reaches its original position. The elements on left to the right pivot are smaller than pivot (12) and right to pivot are greater pivot (12). 8 6 2 4 Sublist 1 9 12 18 15 Sublist 2 Now take sub-list1 and sub-list2 and apply the above process recursively, at last we get sorted list. 15 16 15 16
  • 10. 8/31/2020 10 Randomized Quick sort 19 • Pros and Cons • Advantage: It is the fastest sorting method among all the sorting methods. • Disadvantage: It is somewhat complex and little difficult to implement than other sorting. 20 19 20
  • 12. 8/31/2020 12 Time complexity of Quick sort 23 24 23 24
  • 14. 8/31/2020 14 TimeComplexity Best Case: In best case, consider the following twoassumptions- 1. The pivot, which we choose, will always be swapped into the exactly the middle of the list. And also consider pivot will have an equal number of elements both to its left and right. 2. The number of elements in the list is a powerof 2 i.e. n= 2y . 27 Contd….. Worst Case: Assume that the pivot partition the list into two parts, so that one of the partition has no elements while the other has all the other elements. 28 27 28
  • 15. 8/31/2020 15 Contd….. Average Case: Assume that the pivot partition the list into two parts, so that one of the partition has no elements while the other has all the other elements. Quick Sort Best Case O(n log n) Average Case O(n log n) Worst Case O(n2) 29 29