SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
• Applications: Binary Search, Quick sort
• Material prepared by
• Dr. N. Subhash Chandra
• Source: Web resources , Computer Algorithms by
Sahni
What is Divide-and-Conquer?
 Divide: Divide the problem into a number of
smallersub-problems ideallyof 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.
Contd ……
Divide and Conquer typicalcase
Control abstraction for DandC
Algorithm DandC(p)
{
if small (p)then
returnS(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);
}
}
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.
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
>
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 11 12 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 o w 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
Time complexity of Binary Search
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.
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
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’sposition become‘j’ value.
• Now compare ‘i’ and ‘j’. If i <j, then swap a[i] and a[j].
Otherwiseswappivotelement and a[j].
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 1 2 1 8 1 5
Sublist 2
Now take sub-list1 and sub-list2 and apply the above process recursively,
at last we get sortedlist.
Randomized Quick sort
• 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.
Time complexity of Quick sort
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
.
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.
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)

Contenu connexe

Tendances

Chapter 14 quick_sort
Chapter 14 quick_sortChapter 14 quick_sort
Chapter 14 quick_sort
Terry Yoast
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 

Tendances (20)

Chapter 14 quick_sort
Chapter 14 quick_sortChapter 14 quick_sort
Chapter 14 quick_sort
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
 
Quick sort
Quick sortQuick sort
Quick sort
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Linear regression by Kodebay
Linear regression by KodebayLinear regression by Kodebay
Linear regression by Kodebay
 
HCR's Series (Expansion of factorial of any natural number)
HCR's Series (Expansion of factorial of any natural number)HCR's Series (Expansion of factorial of any natural number)
HCR's Series (Expansion of factorial of any natural number)
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Searching
SearchingSearching
Searching
 
Quick sort
Quick sortQuick sort
Quick sort
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 
Pca
PcaPca
Pca
 
Training machine learning k means 2017
Training machine learning k means 2017Training machine learning k means 2017
Training machine learning k means 2017
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Sorting
SortingSorting
Sorting
 
Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 

Similaire à Bs,qs,divide and conquer 1

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
 

Similaire à Bs,qs,divide and conquer 1 (20)

Quick sort.pptx
Quick sort.pptxQuick sort.pptx
Quick sort.pptx
 
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
 
s4_quick_sort.ppt
s4_quick_sort.ppts4_quick_sort.ppt
s4_quick_sort.ppt
 
Sorting
SortingSorting
Sorting
 
Lec35
Lec35Lec35
Lec35
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
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
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics07 Analysis of Algorithms: Order Statistics
07 Analysis of Algorithms: Order Statistics
 
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
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Divide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotDivide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing Pivot
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptxDIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 

Plus de 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
 

Plus de 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
 

Dernier

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
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
 
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
dharasingh5698
 
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
ankushspencer015
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Dernier (20)

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
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
 
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...
 
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 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...
 
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...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
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
 
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...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
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
 
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 ...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
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
 
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
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 

Bs,qs,divide and conquer 1

  • 1. • Applications: Binary Search, Quick sort • Material prepared by • Dr. N. Subhash Chandra • Source: Web resources , Computer Algorithms by Sahni
  • 2. What is Divide-and-Conquer?  Divide: Divide the problem into a number of smallersub-problems ideallyof 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.
  • 3. Contd …… Divide and Conquer typicalcase
  • 4. Control abstraction for DandC Algorithm DandC(p) { if small (p)then returnS(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); } }
  • 5.
  • 6. 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.
  • 7. 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 >
  • 8. 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 11 12 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 o w 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
  • 9.
  • 10.
  • 11. Time complexity of Binary Search
  • 12. 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.
  • 13. 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
  • 14. 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’sposition become‘j’ value. • Now compare ‘i’ and ‘j’. If i <j, then swap a[i] and a[j]. Otherwiseswappivotelement and a[j].
  • 15. 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 1 2 1 8 1 5 Sublist 2 Now take sub-list1 and sub-list2 and apply the above process recursively, at last we get sortedlist.
  • 16.
  • 17.
  • 18.
  • 20. • 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.
  • 21.
  • 22.
  • 23. Time complexity of Quick sort
  • 24.
  • 25.
  • 26.
  • 27. 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 .
  • 28. 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.
  • 29. 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)