Back to Course


0% Complete
0/82 Steps
  1. Getting Started with Algorithm
    What is an Algorithm?
  2. Characteristics of Algorithm
    1 Topic
  3. Analysis Framework
  4. Performance Analysis
    3 Topics
  5. Mathematical Analysis
    2 Topics
  6. Sorting Algorithm
    Sorting Algorithm
    10 Topics
  7. Searching Algorithm
    6 Topics
  8. Fundamental of Data Structures
  9. Queues
  10. Graphs
  11. Trees
  12. Sets
  13. Dictionaries
  14. Divide and Conquer
    General Method
  15. Binary Search
  16. Recurrence Equation for Divide and Conquer
  17. Finding the Maximum and Minimum
  18. Merge Sort
  19. Quick Sort
  20. Stassen’s Matrix Multiplication
  21. Advantages and Disadvantages of Divide and Conquer
  22. Decrease and Conquer
    Insertion Sort
  23. Topological Sort
  24. Greedy Method
    General Method
  25. Coin Change Problem
  26. Knapsack Problem
  27. Job Sequencing with Deadlines
  28. Minimum Cost Spanning Trees
    2 Topics
  29. Single Source Shortest Paths
    1 Topic
  30. Optimal Tree Problem
    1 Topic
  31. Transform and Conquer Approach
    1 Topic
  32. Dynamic Programming
    General Method with Examples
  33. Multistage Graphs
  34. Transitive Closure
    1 Topic
  35. All Pairs Shortest Paths
    6 Topics
  36. Backtracking
    General Method
  37. N-Queens Problem
  38. Sum of Subsets problem
  39. Graph Coloring
  40. Hamiltonian Cycles
  41. Branch and Bound
    2 Topics
  42. 0/1 Knapsack problem
    2 Topics
  43. NP-Complete and NP-Hard Problems
    1 Topic
In Progress
Lesson 18 of 43
In Progress

Merge Sort

As another example of divide-and-conquer, we investigate a sorting algorithm that has the nice property that in the worst case its complexity is 0(n logn). This algorithm is called merge sort. We assume throughout that the elements are to be sorted in non decreasing order.Given a sequence of n elements( also called keys) a[l],… ,a[n], the general idea is to imagine them split into two sets a[l],… ,a[[n/2]] and a[[n/2]+ 1],… ,a[n]. Each set is individually sorted,and the resulting sorted sequences are merged to produce a single sorted sequence of n elements. Thus we have another ideal example of the divide-and-conquer strategy in which the splitting is into two equal-sized sets and the combining operation is the merging of two sorted sets into one.

MergeSort (Algorithm 6) describes this process very succinctly using recursion and a function Merge (Algorithm 7)which merges two sorted sets. Before executing Merge Sort,the n element should be placed in a[1 :n]. Then MergeSort (1,n) causes the keys to be rearranged into non decreasing order in a.

KodNest Capture35
Algorithm 6: Merge sort
KodNest Capture36
Algorithm 7: Merging two sorted sub arrays using auxiliary storage

Below figure shows the diagram of working of merge sort algorithm in divide and conquer approach.

KodNest 1 10

New Report