I’m learning Kotlin. It’s a statically typed programming language developed by JetBrains running on the JVM. I believe Kotlin has a great future on Android, because it`s offering a lot of functions that Java is missing and i think Google wouldn’t officially support it if they think otherwise. Spring Boot also supports Kotlin. Recently Jake Wharton announced that he is working on Kotlin at Google. He’s involved in the libraries like Retrofit, ButterKnife and RxJava.

A few days ago i read a blog post (Merge Sort in Swift) of my colleague Thomas Hanning. And i wanted to find out how a merge sort in Kotlin would look like.

What is merge sort?

Merge Sort was invented by John von Neumann in 1945, and is an algorithm of class to the “divide and conquer”. Meaning, the algorithm splits an input into various pieces, sorts them and then merges them back together.

The function mergeSort() gets a list and splits at the middle into two new lists. This lists will be passed to mergeSort again until there only lists with one item.

The function merge sorts two lists and merge it into a new list.

Example Project

You can try the code yourself in browser on Try Kotlin