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

Added Heap Sort, Selection sort, and Bubble sort in Kotlin #134

Merged
merged 4 commits into from
Oct 5, 2020
Merged
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
32 changes: 32 additions & 0 deletions Kotlin/BubbleSort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.stream.Stream
import kotlin.random.Random

fun bubbleSort(array: IntArray) {
for (pass in 0 until array.size - 1) {
for (currentPosition in 0 until array.size - pass - 1) {
if (array[currentPosition] > array[currentPosition + 1]) {
val temp = array[currentPosition]
array[currentPosition] = array[currentPosition + 1]
array[currentPosition + 1] = temp
}
}
}
}

fun main() {
//just generate data
val random = Random
val size = 10L
val maxElement = 100
val array: IntArray = Stream.generate { random.nextInt(maxElement) }.limit(size).toArray()
.map { it.toString().toInt() }
.toIntArray()

println("Array element before bubble sort : ")
array.iterator().forEach { print("$it ") }

bubbleSort(array)

println("\nArray element after bubble sort : ")
array.iterator().forEach { print("$it ") }
}
56 changes: 56 additions & 0 deletions Kotlin/HeapSort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

import java.util.stream.Stream
import kotlin.random.Random


private fun buildMaxHeap(array: IntArray) {
for (i in array.size / 2 - 1 downTo 0) {
maxHeap(array, i)
}
}

private fun maxHeap(array: IntArray, rootIndex: Int, heapSize: Int = array.size - 1) {
val leftChildIndex = 2 * rootIndex + 1
val rightChildIndex = 2 * rootIndex + 2
var largestElementIndex = rootIndex

if (leftChildIndex <= heapSize && array[leftChildIndex] > array[rootIndex]) {
largestElementIndex = leftChildIndex
}

if (rightChildIndex <= heapSize && array[rightChildIndex] > array[largestElementIndex]) {
largestElementIndex = rightChildIndex
}

if (largestElementIndex != rootIndex) {
val temp = array[rootIndex]
array[rootIndex] = array[largestElementIndex]
array[largestElementIndex] = temp
maxHeap(array, largestElementIndex, heapSize)
}
}

fun main() {
//just generate data
val r = Random
val size = 30L
val maxElement = 500
val array: IntArray = Stream.generate { r.nextInt(maxElement) }.limit(size).toArray()
.map { it.toString().toInt() }
.toIntArray()

buildMaxHeap(array)

println("Array element before heap sort : ")
array.iterator().forEach { print("$it ") }

for (i in array.size - 1 downTo 1) {
val temp = array[0]
array[0] = array[i]
array[i] = temp
maxHeap(array, 0, i - 1)
}

println("\nArray element after heap sort : ")
array.iterator().forEach { print("$it ") }
}
41 changes: 41 additions & 0 deletions Kotlin/SelectionSort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.stream.Stream
import kotlin.random.Random

fun selectionSort(array: IntArray) {

val size = array.size
var temp : Int

for(i in size-1 downTo 0){
var max = i

for(j in 0 until i){
if(array[j] > array[max]) max=j
}

if(i!=max){
temp = array[i]
array[i] = array[max]
array[max] = temp
}
}

}

fun main() {
//just generate data
val r = Random
val size = 10L
val maxElement = 100
val array: IntArray = Stream.generate { r.nextInt(maxElement) }.limit(size).toArray()
.map { it.toString().toInt() }
.toIntArray()

println("Array element before selection sort : ")
array.iterator().forEach { print("$it ") }

selectionSort(array)

println("\nArray element after selection sort : ")
array.iterator().forEach { print("$it ") }
}