Skip to content

Commit

Permalink
Merge pull request #134 from rsdiz/kotlin-lang
Browse files Browse the repository at this point in the history
Added Heap Sort, Selection sort, and Bubble sort in Kotlin
  • Loading branch information
Anuja-19 authored Oct 5, 2020
2 parents baa72fe + 86b4f53 commit 41f485b
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
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 ") }
}

0 comments on commit 41f485b

Please sign in to comment.