SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Similaire à DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx

Adding and subtracting
Adding and subtractingAdding and subtracting
Adding and subtracting
Diana Pineda
 
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
 
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
edwinllantoy2
 

Similaire à 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)
 

Plus de LakshmiSamivel

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

Dernier

Dernier (20)

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 Inventory
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
Word Stress rules esl .pptx
Word Stress rules esl               .pptxWord Stress rules esl               .pptx
Word Stress rules esl .pptx
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 

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