Skip to content

Commit c796a29

Browse files
Create Selection_Sort_in_Kotlin.kt
1 parent 7edd9c6 commit c796a29

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Kotlin/Selection_Sort_in_Kotlin.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
fun <T:Comparable<T>>selectionsort(items:MutableList<T>):MutableList<T>{
2+
if (items.isEmpty()){
3+
return items
4+
}
5+
for (idx in 0..items.count()){
6+
val array = items.subList(0,items.count()-idx)
7+
val minItem = array.min()
8+
val indexOfMinItem = array.indexOf(minItem)
9+
10+
if (minItem != null) {
11+
items.removeAt(indexOfMinItem)
12+
items.add(minItem)
13+
}
14+
}
15+
return items
16+
}
17+
18+
19+
fun main(args: Array<String>) {
20+
println("Selection Sort")
21+
val names = mutableListOf("John", "Tim", "Zack", "Daniel", "Adam")
22+
println(names)
23+
var ordered = insertionsort(names)
24+
println(ordered)
25+
}
26+
//contributed by sakshamsachdeva

0 commit comments

Comments
 (0)