Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mergeSort bug #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions MergeSort/MergeSort.kt
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
/**
* Created by gazollajunior on 09/04/16.
* Updated by yazdanmanesh on 03/16/23
*/
fun <T:Comparable<T>>mergesort(items:MutableList<T>):MutableList<T>{
if (items.isEmpty()){
fun <T : Comparable<T>> mergeSort(items: List<T>): List<T> {
if (items.size <= 1) {
return items
}

fun merge(left:MutableList<T>, right:MutableList<T>):MutableList<T>{
var merged: MutableList<T> = arrayListOf<T>()
while(!left.isEmpty() && !right.isEmpty()){
val temp:T
if (left.first() < right.first()) {
temp = left.removeAt(0)
fun merge(left: List<T>, right: List<T>): List<T> {
val merged = mutableListOf<T>()
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<String>) {
fun main() {
val names = mutableListOf("John", "Tim", "Zack", "Daniel", "Adam")
println(names)
var ordered = mergesort(names)
val ordered = mergeSort(names)
println(ordered)
}
}
39 changes: 22 additions & 17 deletions MergeSort/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,38 @@ source:Wikipedia
## The code

```kotlin
fun <T:Comparable<T>>mergesort(items:MutableList<T>):MutableList<T>{
if (items.isEmpty()){
fun <T : Comparable<T>> mergeSort(items: List<T>): List<T> {
if (items.size <= 1) {
return items
}

fun merge(left:MutableList<T>, right:MutableList<T>):MutableList<T>{
var merged: MutableList<T> = arrayListOf<T>()
while(!left.isEmpty() && !right.isEmpty()){
val temp:T
if (left.first() < right.first()) {
temp = left.removeAt(0)
fun merge(left: List<T>, right: List<T>): List<T> {
val merged = mutableListOf<T>()
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)
}

```
2 changes: 2 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -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/).
Expand Down