- The elements are split into two sub-arrays (n/2) again and again until only one element is left.
- Merge sort uses additional storage for sorting the auxiliary array.
- Merge sort uses three arrays where two are used for storing each half, and the third external one is used to store the final sorted list by merging the other two and each array is then sorted recursively.
- At last, all sub-arrays are merged to make it ‘n’ element size of the array.
Below is the image to illustrate Merge Sort:
Insertion Sort is a sorting algorithm in which elements are taken from an unsorted item, inserting it in sorted order in front of the other items, and repeating until all items are in order. The algorithm is simple to implement and usually consists of two loops: an outer loop to pick items and an inner loop to iterate through the array. It works on the principle of the sorting playing cards in our hands.
Below is the image to illustrate Insertion Sort:
Difference between Merge sort and Insertion sort:
Time Complexity: In Merge Sort the Worst Case: O(N*log N), Average Case: O(N*log N), and Best Case: O(N*log N),
In Insertion Sort the Worst Case: O(N2), Average Case: O(N2), and Best Case: O(N).
Space Complexity: Merge sort being recursive takes up the auxiliary space complexity of O(N) hence it cannot be preferred over the place where memory is a problem,
In Insertion sort only takes O(1) auxiliary space complexity. It sorts the entire array just by using an extra variable.
Datasets: Merge Sort is preferred for huge data sets. It happens to compare all the elements present in the array hence is not much helpful for small datasets,
Insertion Sort is preferred for fewer elements. It becomes fast when data is already sorted or nearly sorted because it skips the sorted values.
- Efficiency: Considering average time complexity of both algorithm we can say that Merge Sort is efficient in terms of time and Insertion Sort is efficient in terms of space.
Sorting Method: The merge sort is an external sorting method in which the data that is to be sorted cannot be accommodated in the memory and needed auxiliary memory for sorting,
Insertion sort is based on the idea that one element from the input elements is consumed in each iteration to find its correct position i.e., the position to which it belongs in a sorted array.
Stability: Merge sort is stable as two elements with equal value appear in the same order in sorted output as they were in the input unsorted array,
Insertion sort takes O(N2) time on both data structures(Array and Linked list). If the CPU has an efficient memory block move function then the array may be quicker. Otherwise, there probably isn’t that much of a time difference.
|Parameters||Merge Sort||Insertion Sort|
|Worst Case Complexity||O(N*log N)||O(N2)|
|Average Case Complexity||O(N*log N)||O(N2)|
|Best Case Complexity||O(N*log N)||O(N)|
|Auxiliary Space Complexity||O(N)||O(1)|
|Works well on||On huge dataset.||On small dataset.|
|Efficiency||Comparitively Efficient.||Comparitively Inefficient.|
|Algorithm Paradigm||Divide and Conquer||Incremental Approach|
|Uses||It is used for sorting linked list in O(N*log N), for Inversion Count problem, External sorting, etc.||It is used when number of elements is small. It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array.|
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Merge Sort with O(1) extra space merge and O(n lg n) time
- Insertion sort to sort even and odd positioned elements in different orders
- Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Quick Sort vs Merge Sort
- Time complexity of insertion sort when there are O(n) inversions?
- An Insertion Sort time complexity question
- Recursive Insertion Sort
- Insertion Sort
- Insertion Sort for Doubly Linked List
- C Program for Insertion Sort
- Insertion sort using C++ STL
- C Program for Binary Insertion Sort
- Java Program for Binary Insertion Sort
- Python Program for Binary Insertion Sort
- C Program for Recursive Insertion Sort
- Java Program for Recursive Insertion Sort
- Python Program for Recursive Insertion Sort
- Insertion Sort by Swapping Elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.