SlideShare a Scribd company logo
1 of 25
By,
LAKSHMI.S, MCA,M.PHIL,
ASSISTANT PROFESSOR,
DEPARTMENT OF COMPUTER SCIENCE,
SRI ADI CHUNCHANAGIRI WOMENS COLLEGE, CUMBUM.
DIVIDE AND CONQUER METHOD
WHAT IS DIVIDE AND CONQUER ALGORITHM ?
■ A divide and conquer algorithm is a strategy of solving a
large problem by
■ Breaking the problem into smaller sub-problems
■ Conquering the sub-problems, and
■ Combining them to get the desired output. PROBLEM(P)
P1
P2 Pn
……..
S1 S2 Sn
........
SOLUTION(S)
DIVIDE
CONQUER
COMBINE
.
How Divide and Conquer Algorithms Work?
Here are the steps involved:
DIVIDE : Divide the given problem into sub-problems using recursion.
CONQUER : Solve the smaller sub-problems recursively. If the subproblem is small
enough, then solve it directly.
COMBINE : Combine the solutions of the sub-problems that are part of the recursive
process to solve the actual problem
ALGORITHM FOR DIVIDE AND CONQUER
DAC(P)
{
if(small(P))
{
S(P);
}
else
{
divide P into P1,P2……..Pn
apply DAC(P1) ,DAC(P2),…..DAC(Pn)
combine(DAC(P1) ,DAC(P2),….DAC(Pn);
}
If(small(P)
FOR EX: (2+3)+(3-1)*(4+8)
(4+2)
(2+3)+ (3-1)*(4+8)
P1 P2 ….. Pn
5 + 2 * 12
=29
QUICK
SORT
MERGE
SORT
BINARY
SEARCH
BINARY SEARCH
Binary Search is one of the fastest searching
algorithms.
ARRAY SHOULD BE SORTED IN ASCENDING
ORDER
It works on the principle of divide and conquer
technique.
*
*
*
ALGORITHM FOR BINARY SEARCH
binarysearch(a, n, key)
start=0, end= n-1
while(start<=end)
{
mid=start+end ;
2
if(key==a[mid])
return mid;
else if(key<a[mid])
end=mid-1;
else
start=mid+1;
} #end of loop
return -1; #start>end
} unsuccessful search
5 9 17 23 25 45 59 63 71 89
0 1 2 3 4 5 6 7 8 9
start end
KEY=59 #element to be found
m=10
EXAMPLE OF BINARY SEARCH
mid=start+end
2
=0+9/2
=4.5 => 4
So the mid value is 4
a[mid] = a[4] = 25
KEY= MID
KEY< MID -> END=MID-1
KEY>MID -> START=MID+1
And here now, as the key value is greater than the mid value,
Start = mid +1
= 4 +1
= 5
59>25
45 59 63 71 89
5 6 7 8 9
Start End
Mid = start + end = 5+9 = 14 = 7
2 2 2
a[mid]= 7 = 63
Here the key value is lesser than the mid value,
end = mid – 1
= 7 - 1
= 6
59<63
45 59
5 6
Start end
Mid= 5+6/2 = 11/2 = 5.5 = 5
a[mid]=45
Now the mid value is lesser than the key value,
Start= mid+ 1
= 5 + 1
= 6
45<59
59
6
Start end
Mid = 6 + 6
2
= 6 a[mid] = a[6] = 59
As key = mid
return mid
FINALLY THE KEY VALUE IS FOUND USING BINARY SEARCH
QUICK SORT
Quicksort is a divide-and-conquer algorithm.
It works by selecting a 'pivot' element from the
array and partitioning the other elements into two
sub-arrays, according to whether they are less than
or greater than the pivot.
For this reason, it is sometimes called partition-
exchange sort.
The key process in quick Sort is a partition()
*
*
*
*
THE RIGHT
SIDE MUST BE
GREATER
THAN PIVOT
THE LEFT
SIDE MUST BE
LESSER THAN
PIVOT
PIVOT CONDITION
LET US UNDERSTAND THROUGH AN EXAMPLE:
6 3 7 2 4 5
L R
PIVOT
CASE 1 :
Here the right side element is lesser than the pivot (ie)., P>R
P = 6 R = 5
In this condition, we need to swap the elements
6 3 7 2 4 5
5 3 7 2 4 6
L P
R
Case 2:
Here, now compare the left side element and the pivot element
L=5 P=6 (P>L) # no swap
5 3 7 2 4 6
Case 3:
Here, as the left element is moved to the next element (ie)., 3
Compare the pivot element and the left element
L=3 P=6(P>L) # no swap
L
P
R
5 3 7 2 4 6
L P
R
Case: 4
Now, the left element is moved to the next element, compare the element
with the pivot element…
L=7 P=6 (P<L)
In this condition, we need to swap.
5 3 6 2 4 7
P R
L
Case: 5
As the elements were swapped, now compare the pivot element with
the right side element…
P= 6 R=7 (P<R)
Here the pivot element is lesser than the right
Side, hence no swap
5 3 6 2 4 7
Case:6
Here the pivot element is greater than the
right side element P>R
P=6 R=4 #SWAP
P R
L
Case: 7
Now the swapping takes place, let’s compare
the pivot element with the left side element
P=6 L=4 (P>L) #no swap
5 3 4 2 6 7
P
L
R
Case:8
Here the pivot is greater than the left side
element
P=6 L=2 #no swap
FINALLY, We get the array
L P
R
Apply the quick sort on the rounded parts
5 3 4 2
P L
R
In this array, the pivot element is taken as 5, and the right element is 2
P=5 R= 2 (P>R) # Swap
2 3 4 5
P R
L
2<5
3<5
4<5
Now merge all the arrays
2 3 4 5 6 7
This is how quick sort works
MERGE SORT
The divide-and-conquer algorithm breaks down a big
problem into smaller, more manageable pieces that
look similar to the initial problem.
It then solves these subproblems recursively and puts
their solutions together to solve the original problem.
Merge sort is defined as a sorting algorithm that works
by dividing an array into smaller subarrays, sorting
each subarray, and then merging the sorted subarrays
back together to form the final sorted array.
*
*
*
ALGORITHM FOR MERGE SORT
mergesort( A , low , high)
{
if(low<high)
(
mid=low+high
2
Mergesort(A, low, mid);
Mergesort(A, mid+1, high);
Merge(A, low, mid, high);
}
}
Mid= 0+8
2
= 4
MS(A, 0, 4)
MS(A, 5, 8)
15 5 24 8 1 3 16 10 20
0 1 2 3 4 5 6 7 8
Mid = 0+4/2 = 2 mid=5+8/2 = 13/2 = 6.5 = 6
A(0,2) A(3,4) A(5,6) A(7,8)
15 5 24 8 1 3 16 10 20
Mid= 0 + 2 / 2 = 1
15 5 24 8 1 3 16 10 20
15 5 24 8 1 3 16 10 20
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
15 5 24 8 1 3 16 20
10
20
10
16
3
1
8
24
5
15
{
5 15 24 1 8 3 16 10 20
{
{
{
1 5 8 15 24 3 10 16 20
LEFT SUB ARRAY RIGHT SUB ARRAY
L(i) R(j)
1 5 8 15 24 3 10 16 20
LEFT SUB ARRAY RIGHT SUB ARRAY
Compare the values on the left sub array and then the right sub array
Compare the values and merge them.
1 3 5 8 10 15 16 20 24
And through this, you will find the sorted array for merge sort
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx

More Related Content

Similar to DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx

3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury
A simple study on computer algorithms by S. M. Risalat Hasan ChowdhuryA simple study on computer algorithms by S. M. Risalat Hasan Chowdhury
A simple study on computer algorithms by S. M. Risalat Hasan ChowdhuryS. M. Risalat Hasan Chowdhury
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer scienceRiazul Islam
 
Divide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotDivide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotCiaraAbila
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Division of Polynomials.pptx
Division of Polynomials.pptxDivision of Polynomials.pptx
Division of Polynomials.pptxpandavlogsbyJM
 
Adding and subtracting
Adding and subtractingAdding and subtracting
Adding and subtractingDiana Pineda
 
Lesson 8: Rational Functions
Lesson 8: Rational FunctionsLesson 8: Rational Functions
Lesson 8: Rational FunctionsKevin Johnson
 
Divide-and-conquer
Divide-and-conquerDivide-and-conquer
Divide-and-conquerMrunal Patil
 
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
 
Civil Service Review in Math.pptx
Civil Service Review in Math.pptxCivil Service Review in Math.pptx
Civil Service Review in Math.pptxEllaineEspinosa1
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problemsSumita Das
 
Numerical Techniques
Numerical TechniquesNumerical Techniques
Numerical TechniquesYasir Mahdi
 
PRIMER GRADO ECUACIONES Y DESIGUALDADES EN UNA VARIABLE.pdf
PRIMER GRADO ECUACIONES Y DESIGUALDADES EN UNA VARIABLE.pdfPRIMER GRADO ECUACIONES Y DESIGUALDADES EN UNA VARIABLE.pdf
PRIMER GRADO ECUACIONES Y DESIGUALDADES EN UNA VARIABLE.pdfedwinllantoy2
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Pramit Kumar
 

Similar to DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx (20)

3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury
A simple study on computer algorithms by S. M. Risalat Hasan ChowdhuryA simple study on computer algorithms by S. M. Risalat Hasan Chowdhury
A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury
 
Data Structure Sorting
Data Structure SortingData Structure Sorting
Data Structure Sorting
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
s4_quick_sort.ppt
s4_quick_sort.ppts4_quick_sort.ppt
s4_quick_sort.ppt
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
Divide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing PivotDivide and conquer Partitioning: Choosing Pivot
Divide and conquer Partitioning: Choosing Pivot
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Division of Polynomials.pptx
Division of Polynomials.pptxDivision of Polynomials.pptx
Division of Polynomials.pptx
 
Adding and subtracting
Adding and subtractingAdding and subtracting
Adding and subtracting
 
Lesson 8: Rational Functions
Lesson 8: Rational FunctionsLesson 8: Rational Functions
Lesson 8: Rational Functions
 
Divide-and-conquer
Divide-and-conquerDivide-and-conquer
Divide-and-conquer
 
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
 
Civil Service Review in Math.pptx
Civil Service Review in Math.pptxCivil Service Review in Math.pptx
Civil Service Review in Math.pptx
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
 
Simple Equations I
Simple Equations ISimple Equations I
Simple Equations I
 
Numerical Techniques
Numerical TechniquesNumerical Techniques
Numerical Techniques
 
PRIMER GRADO ECUACIONES Y DESIGUALDADES EN UNA VARIABLE.pdf
PRIMER GRADO ECUACIONES Y DESIGUALDADES EN UNA VARIABLE.pdfPRIMER GRADO ECUACIONES Y DESIGUALDADES EN UNA VARIABLE.pdf
PRIMER GRADO ECUACIONES Y DESIGUALDADES EN UNA VARIABLE.pdf
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 

More from LakshmiSamivel

Greedy Algorithm for Computer Science.ppt
Greedy Algorithm for Computer Science.pptGreedy Algorithm for Computer Science.ppt
Greedy Algorithm for Computer Science.pptLakshmiSamivel
 
General methodin Data Structure for UG.pptx
General methodin Data Structure for UG.pptxGeneral methodin Data Structure for UG.pptx
General methodin Data Structure for UG.pptxLakshmiSamivel
 
DataSructure-Time and Space Complexity.pptx
DataSructure-Time and Space Complexity.pptxDataSructure-Time and Space Complexity.pptx
DataSructure-Time and Space Complexity.pptxLakshmiSamivel
 
Basic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptxBasic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptxLakshmiSamivel
 
Classification of datastructure.ppt
Classification of datastructure.pptClassification of datastructure.ppt
Classification of datastructure.pptLakshmiSamivel
 
Computer network notes
Computer network notesComputer network notes
Computer network notesLakshmiSamivel
 

More from LakshmiSamivel (15)

Greedy Algorithm for Computer Science.ppt
Greedy Algorithm for Computer Science.pptGreedy Algorithm for Computer Science.ppt
Greedy Algorithm for Computer Science.ppt
 
General methodin Data Structure for UG.pptx
General methodin Data Structure for UG.pptxGeneral methodin Data Structure for UG.pptx
General methodin Data Structure for UG.pptx
 
DataSructure-Time and Space Complexity.pptx
DataSructure-Time and Space Complexity.pptxDataSructure-Time and Space Complexity.pptx
DataSructure-Time and Space Complexity.pptx
 
Basic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptxBasic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptx
 
Presentation DM.pptx
Presentation DM.pptxPresentation DM.pptx
Presentation DM.pptx
 
Dos.pptx
Dos.pptxDos.pptx
Dos.pptx
 
Formatting tags
Formatting tagsFormatting tags
Formatting tags
 
Classification of datastructure.ppt
Classification of datastructure.pptClassification of datastructure.ppt
Classification of datastructure.ppt
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Semaphore
Semaphore Semaphore
Semaphore
 
Firewall ppt
Firewall pptFirewall ppt
Firewall ppt
 
View
ViewView
View
 
Procedures andcursors
Procedures andcursorsProcedures andcursors
Procedures andcursors
 
Computer network notes
Computer network notesComputer network notes
Computer network notes
 
OsI reference model
OsI reference modelOsI reference model
OsI reference model
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx

  • 1. By, LAKSHMI.S, MCA,M.PHIL, ASSISTANT PROFESSOR, DEPARTMENT OF COMPUTER SCIENCE, SRI ADI CHUNCHANAGIRI WOMENS COLLEGE, CUMBUM. DIVIDE AND CONQUER METHOD
  • 2. WHAT IS DIVIDE AND CONQUER ALGORITHM ? ■ A divide and conquer algorithm is a strategy of solving a large problem by ■ Breaking the problem into smaller sub-problems ■ Conquering the sub-problems, and ■ Combining them to get the desired output. PROBLEM(P) P1 P2 Pn …….. S1 S2 Sn ........ SOLUTION(S) DIVIDE CONQUER COMBINE
  • 3. . How Divide and Conquer Algorithms Work? Here are the steps involved: DIVIDE : Divide the given problem into sub-problems using recursion. CONQUER : Solve the smaller sub-problems recursively. If the subproblem is small enough, then solve it directly. COMBINE : Combine the solutions of the sub-problems that are part of the recursive process to solve the actual problem
  • 4. ALGORITHM FOR DIVIDE AND CONQUER DAC(P) { if(small(P)) { S(P); } else { divide P into P1,P2……..Pn apply DAC(P1) ,DAC(P2),…..DAC(Pn) combine(DAC(P1) ,DAC(P2),….DAC(Pn); } If(small(P) FOR EX: (2+3)+(3-1)*(4+8) (4+2) (2+3)+ (3-1)*(4+8) P1 P2 ….. Pn 5 + 2 * 12 =29
  • 6. BINARY SEARCH Binary Search is one of the fastest searching algorithms. ARRAY SHOULD BE SORTED IN ASCENDING ORDER It works on the principle of divide and conquer technique. * * *
  • 7. ALGORITHM FOR BINARY SEARCH binarysearch(a, n, key) start=0, end= n-1 while(start<=end) { mid=start+end ; 2 if(key==a[mid]) return mid; else if(key<a[mid]) end=mid-1; else start=mid+1; } #end of loop return -1; #start>end } unsuccessful search
  • 8. 5 9 17 23 25 45 59 63 71 89 0 1 2 3 4 5 6 7 8 9 start end KEY=59 #element to be found m=10 EXAMPLE OF BINARY SEARCH mid=start+end 2 =0+9/2 =4.5 => 4 So the mid value is 4 a[mid] = a[4] = 25
  • 9. KEY= MID KEY< MID -> END=MID-1 KEY>MID -> START=MID+1 And here now, as the key value is greater than the mid value, Start = mid +1 = 4 +1 = 5 59>25 45 59 63 71 89 5 6 7 8 9 Start End Mid = start + end = 5+9 = 14 = 7 2 2 2 a[mid]= 7 = 63
  • 10. Here the key value is lesser than the mid value, end = mid – 1 = 7 - 1 = 6 59<63 45 59 5 6 Start end Mid= 5+6/2 = 11/2 = 5.5 = 5 a[mid]=45
  • 11. Now the mid value is lesser than the key value, Start= mid+ 1 = 5 + 1 = 6 45<59 59 6 Start end Mid = 6 + 6 2 = 6 a[mid] = a[6] = 59 As key = mid return mid FINALLY THE KEY VALUE IS FOUND USING BINARY SEARCH
  • 12. QUICK SORT Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. For this reason, it is sometimes called partition- exchange sort. The key process in quick Sort is a partition() * * * *
  • 13. THE RIGHT SIDE MUST BE GREATER THAN PIVOT THE LEFT SIDE MUST BE LESSER THAN PIVOT PIVOT CONDITION LET US UNDERSTAND THROUGH AN EXAMPLE: 6 3 7 2 4 5 L R PIVOT CASE 1 : Here the right side element is lesser than the pivot (ie)., P>R P = 6 R = 5 In this condition, we need to swap the elements 6 3 7 2 4 5
  • 14. 5 3 7 2 4 6 L P R Case 2: Here, now compare the left side element and the pivot element L=5 P=6 (P>L) # no swap 5 3 7 2 4 6 Case 3: Here, as the left element is moved to the next element (ie)., 3 Compare the pivot element and the left element L=3 P=6(P>L) # no swap L P R
  • 15. 5 3 7 2 4 6 L P R Case: 4 Now, the left element is moved to the next element, compare the element with the pivot element… L=7 P=6 (P<L) In this condition, we need to swap. 5 3 6 2 4 7 P R L Case: 5 As the elements were swapped, now compare the pivot element with the right side element… P= 6 R=7 (P<R) Here the pivot element is lesser than the right Side, hence no swap
  • 16. 5 3 6 2 4 7 Case:6 Here the pivot element is greater than the right side element P>R P=6 R=4 #SWAP P R L Case: 7 Now the swapping takes place, let’s compare the pivot element with the left side element P=6 L=4 (P>L) #no swap 5 3 4 2 6 7 P L R
  • 17. Case:8 Here the pivot is greater than the left side element P=6 L=2 #no swap FINALLY, We get the array L P R Apply the quick sort on the rounded parts
  • 18. 5 3 4 2 P L R In this array, the pivot element is taken as 5, and the right element is 2 P=5 R= 2 (P>R) # Swap 2 3 4 5 P R L 2<5 3<5 4<5 Now merge all the arrays
  • 19. 2 3 4 5 6 7 This is how quick sort works
  • 20. MERGE SORT The divide-and-conquer algorithm breaks down a big problem into smaller, more manageable pieces that look similar to the initial problem. It then solves these subproblems recursively and puts their solutions together to solve the original problem. Merge sort is defined as a sorting algorithm that works by dividing an array into smaller subarrays, sorting each subarray, and then merging the sorted subarrays back together to form the final sorted array. * * *
  • 21. ALGORITHM FOR MERGE SORT mergesort( A , low , high) { if(low<high) ( mid=low+high 2 Mergesort(A, low, mid); Mergesort(A, mid+1, high); Merge(A, low, mid, high); } }
  • 22. Mid= 0+8 2 = 4 MS(A, 0, 4) MS(A, 5, 8) 15 5 24 8 1 3 16 10 20 0 1 2 3 4 5 6 7 8 Mid = 0+4/2 = 2 mid=5+8/2 = 13/2 = 6.5 = 6 A(0,2) A(3,4) A(5,6) A(7,8) 15 5 24 8 1 3 16 10 20 Mid= 0 + 2 / 2 = 1 15 5 24 8 1 3 16 10 20 15 5 24 8 1 3 16 10 20 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7
  • 23. 15 5 24 8 1 3 16 20 10 20 10 16 3 1 8 24 5 15 { 5 15 24 1 8 3 16 10 20 { { { 1 5 8 15 24 3 10 16 20 LEFT SUB ARRAY RIGHT SUB ARRAY L(i) R(j)
  • 24. 1 5 8 15 24 3 10 16 20 LEFT SUB ARRAY RIGHT SUB ARRAY Compare the values on the left sub array and then the right sub array Compare the values and merge them. 1 3 5 8 10 15 16 20 24 And through this, you will find the sorted array for merge sort