Skip to content

Instantly share code, notes, and snippets.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Answer to http://codereview.stackexchange.com/questions/67804
*
* Builds all combinations of the given elements.
*/
@max333
max333 / KotlinMergeSort.md
Last active October 1, 2024 07:07
Kotlin Merge Sort Implementations: IntArray vs Array<Int> vs Coroutines (multi-threaded)

Kotlin Merge Sort Implementations: IntArray vs Array<Int> vs Coroutines (multi-threaded)

I was answering a post on codereview.stackexchange.com which led me to explore the performance penalty of using Array<Int> (Java Integer[]) instead of IntArray (Java int[]).

IntArray is expected to have better performance because it avoids the indirection of boxing/unboxing. But for code reuse purposes, Array<Int> is preferable since the algorithm can then be coded once for Array<T: Comparable<T>> and used with any comparable type.

I benchmarked the two approaches and along the way I also compared them to a multi-threaded merge-sort algorithm and merge-sort written in Java. For the multi-threaded version I used Kotlin coroutines.

It is frustrating to code the merge-sort algorithm for IntArray and Array separately since they are identical, except for t