SlideShare a Scribd company logo
1 of 15
MERGE SORT
 Merging is the process of combining two or
more sorted array into a third sorted array. It
was one of the first sorting algorithms used on
a computer and was developed by John Von
Neumann. Divide the array into approximately
n/2 sub-arrays of size two and set the element
in each sub array. Merging each sub-array
with the adjacent sub-array will get another
sorted sub-array of size four. Repeat this
process until there is only one
array remaining of size n.
Cont..
Since at any time the two arrays being merged are
both sub-arrays of A, lower and upper bounds are
required to indicate the sub-arrays of a being
merged. l1 and u1 represents the lower and upper
bands of the first sub-array and l2 and u2
represents the lower and upper bands of the
second sub-array respectively.
Let A be an array of n number of elements to be
sorted A[1], A[2] ...... A[n].
Step 1: Divide the array A into approximately n/2
sorted sub-array of size 2. i.e., the elements in the
(A [1], A [2]), (A [3], A [4]), (A [k], A [k + 1]), (A [n –
1], A [n]) sub-arrays are in
sorted order.
Cont..
Step 2: Merge each pair of pairs to obtain the
following list of sorted sub-array of size 4; the
elements in the sub-array are also in the sorted
order.
(A [1], A [2], A [3], A [4)),...... (A [k – 1], A [k], A [k +
1], A [k + 2]),...... (A [n – 3], A [n – 2], A [n – 1], A
[n].
Step 3: Repeat the step 2 recursively until there is
only one sorted array of size n.
To illustrate the merge sort algorithm consider the
following array with 7 elements
[42], [33], [23], [74], [44], [67], [49]
Mergesort
The mergesort algorithm involves mainly
three steps:
Mergesort is a divide and conquer algorithm
Recursively sort the first and second halves
separately
Merge the two sorted halves into a sorted
group
42 , 33 23 , 74 44 , 67 49
33,42 23,74 44,67 49
23,33,42,74 44,49,67
23,33,42,44,49,67,74
Divider and conquer
 So array can be split into two and then
merge
Arrange in ascending order::
10 12 15 8 13 20 25
10 12 15 8 13 20 25
8 10 12 13 15 20 25
merge
ALGORITHM
A Let be a linear array of size n, A [1], A [2], A [3] ...... A [n],
l1 and u1 represent lower and upper bounds of the first
sub-array and l2 and u2 represent the lower and
upperbound of the second sub-array. Aux is an auxiliary
array of size n. Size is the sizes ofmerge files.
1. Input an array of n elements to be sorted
2. Size = 1
3. Repeat through the step 13 while (Size < n)
(a) set l1 = 0; k = 0
4. Repeat through step 10 while ((l1+Size) < n)
(a) l2 = l1+Size
(b) u1 = l2–1
5. If ((l2+Size–1) < n)
(i) u2 = l2+Size–1
(b) Else
(i) u2 = n-1
6. Initialize i = l1; j = l2 and repeat through step 7 if (i <= u1) and ( j < = u2)
7. If (A [i] < = A[j])
(i) Aux [k] = A [i++]
(b) Else
(i) Aux [k] = A [j++]
8. Repeat the step 8 by incrementing the value of k until (i < = u1)
(a) Aux [k] = A [I++]
9. Repeat the step 9 by incrementing the value of k until ( j < = u2)
(a) Aux [k] = A [ j++]
10. L1=u2+1
11. Initialize I = l1 and repeat the step 11 if (k < n) by incrementing I by one
(a) Aux [k++] = A[I]
12. Initialize I=0 and repeat the step 12 if (I < n) by incrementing I by one
(a) A [i] = A [I]
13. Size = Size*2
14. Exit
//PROGRAM TO IMPLEMENT MERGING OF TWO
SORTED ARRAYS
//INTO A THIRD SORTED ARRAY
//CODED AND COMPILED IN C
#include<conio.h>
#include<stdio.h>
void main()
{
int arr1[20],arr2[20],arr3[40];
int i,j,k;
int max1,max2;
clrscr();
printf (“nEnter the number of elements in list1 : ”);
scanf (“%d”,&max1);
printf (“nTake the elements in sorted order :n”);
for (i=0;i<max1;i++)
{
printf (“nEnter element %d : ”,i+1);
scanf (“%d”,&arr1[i]);
}
printf (“nEnter the number of elements in list2 : ”);
scanf (“%d”,&max2);
printf (“nTake the elements in sorted order :n”);
for (i=0;i<max2;i++)
{
printf (“nEnter element %d : ”,i+1);
scanf (“%d”,&arr2[i]);
}
Cont..
Cont.
/* Merging */
i=0; /*Index for first array*/j=0; /*Index for second array*/
k=0; /*Index for merged array*/
while( (i < max1) && (j < max2) )
{
if ( arr1[i] < arr2[j] )
arr3[k++]=arr1[i++];
else
arr3[k++]=arr2[j++];
}/*End of while*/
/*Put remaining elements of arr1 into arr3*/
while( i < max1 )
arr3[k++]=arr1[i++];
/*Put remaining elements of arr2 into arr3*/
Con..
while( j < max2 )
arr3[k++]=arr2[j++];
/*Merging completed*/
printf (“nList 1 : ”);
for (i=0;i<max1;i++)
printf (“%d ”,arr1[i]);
printf (“nList 2 : ”);
for (i=0;i<max2;i++)
printf(“%d ”,arr2[i]);
printf (“nMerged list : ”);
for( i=0;i<max1+max2;i++)
printf (“%d ”,arr3[i]);
getch();
}/*End of main()*/
Let f (n) denote the number of comparisons
needed to sort an n element array A using
the merge sort algorithm. The algorithm
requires almost log n passes, each
involving n or fewer comparison. In average
and worst case the merge sort requires O(n
log n ) comparisons.
The main drawback of merge sort is that it
requires O(n) additional space for the
auxiliary array.
TIME COMPLEXITY
Merge sort

More Related Content

What's hot

Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 

What's hot (20)

Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Merge sort
Merge sortMerge sort
Merge sort
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Bucket sort
Bucket sortBucket sort
Bucket sort
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
3.6 radix sort
3.6 radix sort3.6 radix sort
3.6 radix sort
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Merge sort
Merge sortMerge sort
Merge sort
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 

Viewers also liked

Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
hermiraguilar
 
Filosofía Contemporanea
Filosofía ContemporaneaFilosofía Contemporanea
Filosofía Contemporanea
vinisdb87
 

Viewers also liked (20)

Mergesort
MergesortMergesort
Mergesort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk through
 
Merging learning and change management sahrp conference october 2013
Merging learning and change management sahrp conference october 2013Merging learning and change management sahrp conference october 2013
Merging learning and change management sahrp conference october 2013
 
Merge sort
Merge sortMerge sort
Merge sort
 
05 dc1
05 dc105 dc1
05 dc1
 
Algorithms for External Memory Sorting
Algorithms for External Memory SortingAlgorithms for External Memory Sorting
Algorithms for External Memory Sorting
 
Merge sort
Merge sortMerge sort
Merge sort
 
MSc_thesis
MSc_thesisMSc_thesis
MSc_thesis
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Implementing Merge Sort
Implementing Merge SortImplementing Merge Sort
Implementing Merge Sort
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort Algorithm
 
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
 
Merger Implementation Steps+Issues
Merger Implementation Steps+IssuesMerger Implementation Steps+Issues
Merger Implementation Steps+Issues
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
 
Filosofía Contemporanea
Filosofía ContemporaneaFilosofía Contemporanea
Filosofía Contemporanea
 
Filosofía contemporanea
Filosofía contemporaneaFilosofía contemporanea
Filosofía contemporanea
 

Similar to Merge sort

data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
palhimanshi999
 
ELEG 320L – Signals & Systems Laboratory Dr. Jibran Khan Yous.docx
ELEG 320L – Signals & Systems Laboratory Dr. Jibran Khan Yous.docxELEG 320L – Signals & Systems Laboratory Dr. Jibran Khan Yous.docx
ELEG 320L – Signals & Systems Laboratory Dr. Jibran Khan Yous.docx
toltonkendal
 
Introduction To Matrix
Introduction To MatrixIntroduction To Matrix
Introduction To Matrix
Annie Koh
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTSBASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
jainyshah20
 
c++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdfc++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdf
dhavalbl38
 

Similar to Merge sort (20)

Ada notes
Ada notesAda notes
Ada notes
 
M269 Data Structures And Computability.docx
M269 Data Structures And Computability.docxM269 Data Structures And Computability.docx
M269 Data Structures And Computability.docx
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
 
Sorting
SortingSorting
Sorting
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Brute force
Brute forceBrute force
Brute force
 
1D Array
1D Array1D Array
1D Array
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanations
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
ELEG 320L – Signals & Systems Laboratory Dr. Jibran Khan Yous.docx
ELEG 320L – Signals & Systems Laboratory Dr. Jibran Khan Yous.docxELEG 320L – Signals & Systems Laboratory Dr. Jibran Khan Yous.docx
ELEG 320L – Signals & Systems Laboratory Dr. Jibran Khan Yous.docx
 
Introduction To Matrix
Introduction To MatrixIntroduction To Matrix
Introduction To Matrix
 
Sorting2
Sorting2Sorting2
Sorting2
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTSBASIC OF ALGORITHM AND MATHEMATICS STUDENTS
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
c++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdfc++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdf
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 

Merge sort

  • 1.
  • 2. MERGE SORT  Merging is the process of combining two or more sorted array into a third sorted array. It was one of the first sorting algorithms used on a computer and was developed by John Von Neumann. Divide the array into approximately n/2 sub-arrays of size two and set the element in each sub array. Merging each sub-array with the adjacent sub-array will get another sorted sub-array of size four. Repeat this process until there is only one array remaining of size n.
  • 3. Cont.. Since at any time the two arrays being merged are both sub-arrays of A, lower and upper bounds are required to indicate the sub-arrays of a being merged. l1 and u1 represents the lower and upper bands of the first sub-array and l2 and u2 represents the lower and upper bands of the second sub-array respectively. Let A be an array of n number of elements to be sorted A[1], A[2] ...... A[n]. Step 1: Divide the array A into approximately n/2 sorted sub-array of size 2. i.e., the elements in the (A [1], A [2]), (A [3], A [4]), (A [k], A [k + 1]), (A [n – 1], A [n]) sub-arrays are in sorted order.
  • 4. Cont.. Step 2: Merge each pair of pairs to obtain the following list of sorted sub-array of size 4; the elements in the sub-array are also in the sorted order. (A [1], A [2], A [3], A [4)),...... (A [k – 1], A [k], A [k + 1], A [k + 2]),...... (A [n – 3], A [n – 2], A [n – 1], A [n]. Step 3: Repeat the step 2 recursively until there is only one sorted array of size n. To illustrate the merge sort algorithm consider the following array with 7 elements [42], [33], [23], [74], [44], [67], [49]
  • 5. Mergesort The mergesort algorithm involves mainly three steps: Mergesort is a divide and conquer algorithm Recursively sort the first and second halves separately Merge the two sorted halves into a sorted group
  • 6. 42 , 33 23 , 74 44 , 67 49 33,42 23,74 44,67 49 23,33,42,74 44,49,67 23,33,42,44,49,67,74
  • 7. Divider and conquer  So array can be split into two and then merge Arrange in ascending order:: 10 12 15 8 13 20 25 10 12 15 8 13 20 25 8 10 12 13 15 20 25 merge
  • 8. ALGORITHM A Let be a linear array of size n, A [1], A [2], A [3] ...... A [n], l1 and u1 represent lower and upper bounds of the first sub-array and l2 and u2 represent the lower and upperbound of the second sub-array. Aux is an auxiliary array of size n. Size is the sizes ofmerge files. 1. Input an array of n elements to be sorted 2. Size = 1 3. Repeat through the step 13 while (Size < n) (a) set l1 = 0; k = 0 4. Repeat through step 10 while ((l1+Size) < n) (a) l2 = l1+Size (b) u1 = l2–1 5. If ((l2+Size–1) < n) (i) u2 = l2+Size–1 (b) Else (i) u2 = n-1
  • 9. 6. Initialize i = l1; j = l2 and repeat through step 7 if (i <= u1) and ( j < = u2) 7. If (A [i] < = A[j]) (i) Aux [k] = A [i++] (b) Else (i) Aux [k] = A [j++] 8. Repeat the step 8 by incrementing the value of k until (i < = u1) (a) Aux [k] = A [I++] 9. Repeat the step 9 by incrementing the value of k until ( j < = u2) (a) Aux [k] = A [ j++] 10. L1=u2+1 11. Initialize I = l1 and repeat the step 11 if (k < n) by incrementing I by one (a) Aux [k++] = A[I] 12. Initialize I=0 and repeat the step 12 if (I < n) by incrementing I by one (a) A [i] = A [I] 13. Size = Size*2 14. Exit
  • 10. //PROGRAM TO IMPLEMENT MERGING OF TWO SORTED ARRAYS //INTO A THIRD SORTED ARRAY //CODED AND COMPILED IN C #include<conio.h> #include<stdio.h> void main() { int arr1[20],arr2[20],arr3[40]; int i,j,k; int max1,max2; clrscr(); printf (“nEnter the number of elements in list1 : ”); scanf (“%d”,&max1); printf (“nTake the elements in sorted order :n”);
  • 11. for (i=0;i<max1;i++) { printf (“nEnter element %d : ”,i+1); scanf (“%d”,&arr1[i]); } printf (“nEnter the number of elements in list2 : ”); scanf (“%d”,&max2); printf (“nTake the elements in sorted order :n”); for (i=0;i<max2;i++) { printf (“nEnter element %d : ”,i+1); scanf (“%d”,&arr2[i]); } Cont..
  • 12. Cont. /* Merging */ i=0; /*Index for first array*/j=0; /*Index for second array*/ k=0; /*Index for merged array*/ while( (i < max1) && (j < max2) ) { if ( arr1[i] < arr2[j] ) arr3[k++]=arr1[i++]; else arr3[k++]=arr2[j++]; }/*End of while*/ /*Put remaining elements of arr1 into arr3*/ while( i < max1 ) arr3[k++]=arr1[i++]; /*Put remaining elements of arr2 into arr3*/
  • 13. Con.. while( j < max2 ) arr3[k++]=arr2[j++]; /*Merging completed*/ printf (“nList 1 : ”); for (i=0;i<max1;i++) printf (“%d ”,arr1[i]); printf (“nList 2 : ”); for (i=0;i<max2;i++) printf(“%d ”,arr2[i]); printf (“nMerged list : ”); for( i=0;i<max1+max2;i++) printf (“%d ”,arr3[i]); getch(); }/*End of main()*/
  • 14. Let f (n) denote the number of comparisons needed to sort an n element array A using the merge sort algorithm. The algorithm requires almost log n passes, each involving n or fewer comparison. In average and worst case the merge sort requires O(n log n ) comparisons. The main drawback of merge sort is that it requires O(n) additional space for the auxiliary array. TIME COMPLEXITY