Merge Sort is sorting based on Divide and Conquer strategy and is one of the famous algorithms among Sorting. In this article, we will be discussing about Merge Sort Kotlin Implementation.
How Merge sort Works?
Merge sort algorithm technique basically involves two processes – one process splits the array into two halves and sorts them individually. The other process involves merging them again into a single array. This first process of splitting the array goes on and on and on till the array divides into small subfiles with single elements and later merged. Thus it does not need separate stack.
Let us quickly dive into the Merge sort kotlin implementation. In addition to commenting the code wherever its necessary, I tried to explain in brief at the bottom of the implementation for those who seek more info.
Code Explanation for Merge Sort Kotlin Algorithm
- Read the Input and Split into array: Here we are trying to read the input whose values are separated by spaces(for eg., 5 4 3 2 1) and later converting them into an array of integers. Next, mergeSort() function is called in 5th Line.
- Calculating the middle element: To split the array into two halves we are calculating and splitting array into two halves – 0 to middle, middle + 1 to array.length-1
- To sort left half: Recursively calling mergeSort() function on the left half to make it sorted
- To sort right half: Recursively calling mergeSort() function on the right half to make it sorted
- Merge them: Here we are calling merge() function which takes few parameters as shown to help them sort and merge. We added additional points in this function. Lets see them individually
- Copying both halves into helper array: Here we are copying both halves into helper array.
- Helper variables: helperLeft and helperRight keeps the track of where the start of left and right halves is.
- Condition to check helper left and helper right: checking if both the helper variables are within the limits
- Check if value at helperLeft index is less than that of value at helper right: If it’s true, then we’ll fill the current position of array with the element present at helperLeft index and increment the helperLeft to compare with other right elements in next iteration.
- Right element is smaller than left element: If the right value is true, we’ll perform a similar operation as that of the above.
- Copy the rest of left side of array into target: Here we will be copying any elements which are left in the left side of array after merging. We don’t copy the right half because they will be already be present at their respective spots. For example if we take the values as [1, 4, 5, || 3, 9, 10] before merging the elements left will be [9, 10] which will be present at last place both in helper array and our array. Hence no need to copy them.
Here the Runtime for merge sort is O(n log n) for average and worst cases.
In the next article, we will be looking into other few major sorting techniques.
Want to improve this article / contribute new articles? Reach us at – https://www.coderefer.com/write-an-article/