Merge Sort
Sintu Mishra
Topics to be covered:
• ■ Introduction
• ■ Definition
• ■ Algorithm
• ■ Steps involved
• ■ Program
• ■ Applications
Introduction:
• Merge Sort is a complex and fast sorting
algorithm that repeatedly divides an
unsorted section into two equal
subsections, sorts them separately and
merges them correctly.
Definition:
• Merge sort is a DIVIDE AND
CONQUER algorithm. It divides
input array in two halves, calls
itself for the two halves and
then merges the two sorted
halves.
• The merge() function is used
for merging two halves.
Steps involved:
• – Divide the problem into
sub-problems that are
similar to the original but
smaller in size.
• – Conquer the sub-
problems by solving them
recursively. If they are
small enough, just solve
them in a straightforward
manner.
• – Combine the solutions to
create a solution to the
original problem.
Algorithm:
MERGE_SORT(ARR, BEG, END)
• Step 1: IF BEG < END
SET MID = (BEG + END)/2
CALL MERGE_SORT (ARR, BEG, MID)
CALL MERGE_SORT (ARR, MID+1, END)
MERGE (ARR, BEG, MID, END)
[END OF IF]
• Step 2: END
• The running time of merge
sort in the average case and
the worst case can be given as
O(n logn).
• Although merge sort has an
optimal time complexity, it
needs an additional space of
O(n) for the temporary array
TEMP.
Example:- Sort the array given below using merge sort.
39,9,81,45,9,27,72,18
Example:- Sort the array given below using merge sort
38,27,43,3,9,82,10
Solution:-
Program:
Why Merge Sort??
• ■ Compared to insertion sort merge sort is faster.
• ■ On small inputs, insertion sort may be faster. But
for large enough inputs, merge sort will always be
faster, because its running time grows more slowly
than insertion sorts.
Applications:-
• ■ Merge sort type algorithms allows large data sets
to be sorted easily.
• ■ Merge sort accesses data sequentially and the
need of random access is low.
• ■ Inversion count problem.
• ■ Used in External Sorting.
• ■ Organize an MP3 library.
• ■ The e-commerce application.
Thank You...!!!!

Presentation merge sort.pptx

  • 1.
  • 2.
    Topics to becovered: • ■ Introduction • ■ Definition • ■ Algorithm • ■ Steps involved • ■ Program • ■ Applications
  • 3.
    Introduction: • Merge Sortis a complex and fast sorting algorithm that repeatedly divides an unsorted section into two equal subsections, sorts them separately and merges them correctly.
  • 4.
    Definition: • Merge sortis a DIVIDE AND CONQUER algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. • The merge() function is used for merging two halves.
  • 5.
    Steps involved: • –Divide the problem into sub-problems that are similar to the original but smaller in size. • – Conquer the sub- problems by solving them recursively. If they are small enough, just solve them in a straightforward manner. • – Combine the solutions to create a solution to the original problem.
  • 6.
    Algorithm: MERGE_SORT(ARR, BEG, END) •Step 1: IF BEG < END SET MID = (BEG + END)/2 CALL MERGE_SORT (ARR, BEG, MID) CALL MERGE_SORT (ARR, MID+1, END) MERGE (ARR, BEG, MID, END) [END OF IF] • Step 2: END • The running time of merge sort in the average case and the worst case can be given as O(n logn). • Although merge sort has an optimal time complexity, it needs an additional space of O(n) for the temporary array TEMP.
  • 7.
    Example:- Sort thearray given below using merge sort. 39,9,81,45,9,27,72,18
  • 8.
    Example:- Sort thearray given below using merge sort 38,27,43,3,9,82,10 Solution:-
  • 9.
  • 12.
    Why Merge Sort?? •■ Compared to insertion sort merge sort is faster. • ■ On small inputs, insertion sort may be faster. But for large enough inputs, merge sort will always be faster, because its running time grows more slowly than insertion sorts.
  • 13.
    Applications:- • ■ Mergesort type algorithms allows large data sets to be sorted easily. • ■ Merge sort accesses data sequentially and the need of random access is low. • ■ Inversion count problem. • ■ Used in External Sorting. • ■ Organize an MP3 library. • ■ The e-commerce application.
  • 14.