Hello, aspiring programmers! Today's topic is "Merge Sort." Merge Sort is a sorting technique akin to arranging a deck of shuffled cards in order. However, for data at the Internet scale, Merge Sort outperforms your regular techniques. Today, we'll explore Merge Sort, code it in Kotlin, and analyze its speed. Ready? Let's get started!
In computer science, Merge Sort is a popular method to sort elements. Merge Sort uses the same 'divide-and-conquer' strategy for sorting as the familiar Quick Sort algorithm. Imagine you have one long music playlist mixed up with songs. You want to sort these songs from A to Z. That's what Merge Sort does to an array.
In the three steps of Merge Sort:
- Split the array into halves.
- Sort each half separately.
- Merge the sorted halves back together.
We will start by building code for merging two sorted parts. The merge process involves making two halves play sort and seek. It compares elements from two halves and merges them so that the resulting list is sorted as well.
Let's code a merge() function in Kotlin that will do just that. Note that the final variant of the merge sort function will perform every operation "in place," meaning there will not be two actual arrays; we will operate on parts of one array. With this in mind, let's implement the merge function to take just one array and treat its parts like separate arrays.
