forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_Sort.kt
More file actions
26 lines (24 loc) · 741 Bytes
/
Selection_Sort.kt
File metadata and controls
26 lines (24 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Function for Selection Sort
fun Selection_Sort(list: MutableList<Int>):List<Int> {
for(i in 0 until (list.size) ) {
var min_index: Int = i
for(j in (i+1) until (list.size)) {
if(list[j]<list[min_index]) {
min_index = j
}
}
var temp: Int = list[min_index]
list[min_index] = list[i]
list[i] = temp
}
return list
}
//Driver function for selection sort
fun main(args: Array<String>) {
val list = mutableListOf(4, 67, 89, 2, 45, 102)
println("Unsorted List: $list")
val sortedList = Selection_Sort(list)
println("Sorted List: $sortedList")
}
//Input: Unsorted: [4, 67, 89, 2, 45, 102]
//Output: Sorted: [2, 4, 25, 67, 89, 102]