Merge Sorting
Merge sorting also works on the principle of "divide and conquer".The algorithm of merge sort is quite different from that of quick-sort but its best case performance is also O(nlog(n)).The step by step process is-
Merge sorting also works on the principle of "divide and conquer".The algorithm of merge sort is quite different from that of quick-sort but its best case performance is also O(nlog(n)).The step by step process is-
- Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
- Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
- Firstly divide the given array of n elements into n individual numbers.
- Then compare the adjacent elements and merge them in the sorted order.
- Then compare the corresponding elements of the resulting lists and arrange them in the sorted order.
- And finally the sorted list will be obtained.
Complexity Analysis of Merge Sort
Worst Case Time Complexity : O(n log n)Best Case Time Complexity : O(n log n)
Average Time Complexity : O(n log n)
Space Complexity : O(n)
- Time complexity of Merge Sort is O(n Log n) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and take linear time to merge two halves.
- It requires equal amount of additional space as the unsorted list. Hence its not at all recommended for searching large unsorted lists.
- It is the best Sorting technique for sorting Linked Lists.
Comments
Post a Comment
Comment on articles for more info.