diff --git a/MergeSort/MergeSort.kt b/MergeSort/MergeSort.kt index 3041d10..9b6110e 100644 --- a/MergeSort/MergeSort.kt +++ b/MergeSort/MergeSort.kt @@ -1,40 +1,44 @@ /** * Created by gazollajunior on 09/04/16. + * Updated by yazdanmanesh on 03/16/23 */ -fun >mergesort(items:MutableList):MutableList{ - if (items.isEmpty()){ +fun > mergeSort(items: List): List { + if (items.size <= 1) { return items } - fun merge(left:MutableList, right:MutableList):MutableList{ - var merged: MutableList = arrayListOf() - while(!left.isEmpty() && !right.isEmpty()){ - val temp:T - if (left.first() < right.first()) { - temp = left.removeAt(0) + fun merge(left: List, right: List): List { + val merged = mutableListOf() + var leftIndex = 0 + var rightIndex = 0 + + while (leftIndex < left.size && rightIndex < right.size) { + if (left[leftIndex] < right[rightIndex]) { + merged.add(left[leftIndex]) + leftIndex++ } else { - temp = right.removeAt(0) + merged.add(right[rightIndex]) + rightIndex++ } - merged.add(temp) } - if (!left.isEmpty()) merged.addAll(left) - if (!right.isEmpty()) merged.addAll(right) - return merged - } + // Add the remaining elements from the unfinished list + merged.addAll(left.subList(leftIndex, left.size)) + merged.addAll(right.subList(rightIndex, right.size)) - val pivot = items.count()/2 + return merged + } - var left = mergesort(items.subList(0, pivot)) - var right = mergesort(items.subList(pivot, items.count()-1)) + val pivot = items.size / 2 + val left = mergeSort(items.subList(0, pivot)) + val right = mergeSort(items.subList(pivot, items.size)) return merge(left, right) } - -fun main(args: Array) { +fun main() { val names = mutableListOf("John", "Tim", "Zack", "Daniel", "Adam") println(names) - var ordered = mergesort(names) + val ordered = mergeSort(names) println(ordered) -} +} \ No newline at end of file diff --git a/MergeSort/README.markdown b/MergeSort/README.markdown index 8f301ac..d905d31 100644 --- a/MergeSort/README.markdown +++ b/MergeSort/README.markdown @@ -7,33 +7,38 @@ source:Wikipedia ## The code ```kotlin -fun >mergesort(items:MutableList):MutableList{ - if (items.isEmpty()){ +fun > mergeSort(items: List): List { + if (items.size <= 1) { return items } - fun merge(left:MutableList, right:MutableList):MutableList{ - var merged: MutableList = arrayListOf() - while(!left.isEmpty() && !right.isEmpty()){ - val temp:T - if (left.first() < right.first()) { - temp = left.removeAt(0) + fun merge(left: List, right: List): List { + val merged = mutableListOf() + var leftIndex = 0 + var rightIndex = 0 + + while (leftIndex < left.size && rightIndex < right.size) { + if (left[leftIndex] < right[rightIndex]) { + merged.add(left[leftIndex]) + leftIndex++ } else { - temp = right.removeAt(0) + merged.add(right[rightIndex]) + rightIndex++ } - merged.add(temp) } - if (!left.isEmpty()) merged.addAll(left) - if (!right.isEmpty()) merged.addAll(right) - return merged - } + // Add the remaining elements from the unfinished list + merged.addAll(left.subList(leftIndex, left.size)) + merged.addAll(right.subList(rightIndex, right.size)) - val pivot = items.count()/2 + return merged + } - var left = mergesort(items.subList(0, pivot)) - var right = mergesort(items.subList(pivot, items.count()-1)) + val pivot = items.size / 2 + val left = mergeSort(items.subList(0, pivot)) + val right = mergeSort(items.subList(pivot, items.size)) return merge(left, right) } + ``` \ No newline at end of file diff --git a/README.markdown b/README.markdown index fb75e05..47406bd 100644 --- a/README.markdown +++ b/README.markdown @@ -1,3 +1,5 @@ +Please consider submitting your pull requests. This project is an updated version of Kotlin-Algorithms , and I'm seeking your help to keep it up-to-date. + # Welcome to Kotlin Algorithms Here you'll find implementations of popular algorithms and data structures in [Kotlin programming language](https://kotlinlang.org/).