And then we have to merge all these sorted subarrays, step by step to form one single sorted array. In the above example, we are given 2 arrays [1, 4, 5, 6] and [2, 3, 7]. Then, it repeatedly merges these subarrays, to produce new sorted subarrays, and in the end, one complete sorted array is produced.
Final merged and sorted array: [1, 2, 3, 4, 5, 6, 7] The left and the right halves can always be sorted recursively using the same algorithm. So, let us understand it well using the above example.
As we know that, in the bubble sort we use the comparison based technique to sort an array.
Recursion word refers to the repetition with itself by using the smaller input values. In merge sort we follow the following steps: We take a variable p and store the starting index of our array in this.
And to merge the subarrays, made by dividing the original array of n elements, a running time of O n will be required.
Then we divide these 2 subarrays again, just like we divided our main array and this continues. So we will break these subarrays into even smaller subarrays, until we have multiple subarrays with single element in them. When we use recursion to perform the bubble sort, it is called Recursive Bubble Sort.
Let us place a pointer at the head of each array. Divide and Conquer If we can break a single big problem into smaller sub-problems, solve the smaller sub-problems and combine their solutions to find the solution for the original big problem, it becomes easier to solve the whole problem.