-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from rsdiz/kotlin-lang
Added Heap Sort, Selection sort, and Bubble sort in Kotlin
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ") } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ") } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ") } | ||
} |