SlideShare a Scribd company logo
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

More Related Content

What's hot

Pattern recognition binoy k means clustering
Pattern recognition binoy  k means clusteringPattern recognition binoy  k means clustering
Pattern recognition binoy k means clustering
108kaushik
 
Large Scale Data Clustering: an overview
Large Scale Data Clustering: an overviewLarge Scale Data Clustering: an overview
Large Scale Data Clustering: an overview
Vahid Mirjalili
 
Stevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsStevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting Algorithms
James 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 Clustering
Edureka!
 
"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
Adrian 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 accuracy
ijpla
 
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
IJERA Editor
 
Improved k-means
Improved k-meansImproved k-means
Improved k-means
Kasun Ranga Wijeweera
 
K means clustring @jax
K means clustring @jaxK means clustring @jax
K means clustring @jax
Yaduvanshi Yadav
 
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 techniques
Poonam 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 @jax
Ajay Iet
 
50120140505013
5012014050501350120140505013
50120140505013
IAEME Publication
 
DATA MINING:Clustering Types
DATA MINING:Clustering TypesDATA MINING:Clustering Types
DATA MINING:Clustering Types
Ashwin Shenoy M
 
Lect4
Lect4Lect4
Lect4
sumit621
 
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
IJCSIS Research Publications
 
K mean-clustering
K mean-clusteringK mean-clustering
K mean-clustering
Afzaal Subhani
 

What's hot (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
 

Similar to Unit ii divide and conquer -1

Quick sort.pptx
Quick sort.pptxQuick sort.pptx
Quick sort.pptx
Anbarasan Radhakrishnan R
 
s4_quick_sort.ppt
s4_quick_sort.ppts4_quick_sort.ppt
s4_quick_sort.ppt
AliAhmad38278
 
Divide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotDivide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing Pivot
CiaraAbila
 
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
nikshaikh786
 
Sorting
SortingSorting
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
RSathyaPriyaCSEKIOT
 
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
yazad dumasia
 
Lec35
Lec35Lec35
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptxDIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
LakshmiSamivel
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
DrRanjeetKumar51721
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
RenalthaPujaBagaskar
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
SushantRaj25
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
KamalAlbashiri
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
Krish_ver2
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
MuhammadSheraz836877
 
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
Abdii Rashid
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 

Similar to 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)
 

More from subhashchandra197

Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.pptUnit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
subhashchandra197
 
Unit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptxUnit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptx
subhashchandra197
 
Data Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptxData Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptx
subhashchandra197
 
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
subhashchandra197
 
Data science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptxData science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptx
subhashchandra197
 
Unit ii divide and conquer -4
Unit ii divide and conquer -4Unit ii divide and conquer -4
Unit ii divide and conquer -4
subhashchandra197
 
Unit ii divide and conquer -3
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3
subhashchandra197
 
Unit ii divide and conquer -2
Unit ii divide and conquer -2Unit ii divide and conquer -2
Unit ii divide and conquer -2
subhashchandra197
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
subhashchandra197
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
subhashchandra197
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
subhashchandra197
 
P,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problemsP,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problems
subhashchandra197
 
Turing machine material
Turing machine materialTuring machine material
Turing machine material
subhashchandra197
 
Turing machine types
Turing machine typesTuring machine types
Turing machine types
subhashchandra197
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
subhashchandra197
 
Disjoint sets union, find
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
subhashchandra197
 

More from subhashchandra197 (16)

Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.pptUnit-I Objects,Attributes,Similarity&Dissimilarity.ppt
Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
 
Unit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptxUnit-I- Introduction- Traits of Big Data-Final.pptx
Unit-I- Introduction- Traits of Big Data-Final.pptx
 
Data Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptxData Science : Unit-I -Hypothesis and Inferences.pptx
Data Science : Unit-I -Hypothesis and Inferences.pptx
 
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
 
Data science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptxData science Lecture Notes: Reporting vs Analysis.pptx
Data science Lecture Notes: Reporting vs Analysis.pptx
 
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
 

Recently uploaded

Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
enizeyimana36
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
zubairahmad848137
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
HODECEDSIET
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 

Recently uploaded (20)

Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 

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