In merge sort, we break the given array midway. (step 1)
We have 2 subarrays: [4, 2, 7, 1] and [9,12,11,10]
Then we divide these 2 subarrays again. (step 2)
We have 4 subarrays: [4,2], [7,1], [9,12], [11,10]
And so on till we have subarrays with single element. (step 3)
We have 8 subarrays: [4], [2], [7], [1], [9], [12], [11], [10]
We start merging the subarrays:
we have 4 sorted subarrays with 2 elements (step 4)
They are [2,4], [1,7], [9,12], [10,11]
2 sorted subarrays with 4 elements (step 5)
They are [1,2,4,7] and [9,10,11,12]
And finally we have a sorted list of numbers (step 6)
Result: [1,2,4,7,9,10,11,12]
Comments
Leave a comment