📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will write a Program to implement the Insertion Sort algorithm in Kotlin.
Insertion sort is a more useful algorithm. Like bubble sort and selection sort, insertion sort has an average time complexity of O(n²), but the performance of insertion sort can vary. The more the data is already sorted, the less work needs to do.Insertion Sort Algorithm in Kotlin
import java.util.*
fun <E: Comparable<E>> Array<E>.sort() {
val len = size
for (i in 1 until len) {
var key = this[i]
var j = i - 1;
while(j >= 0 && this[j] > key) {
this[j + 1] = this[j]
j--
}
this[j + 1] = key
}
}
fun <E: Comparable<E>> List<E>.sort(): List<E> {
val len = size
val resultList = toMutableList()
for (i in 1 until len) {
var key = resultList[i]
var j = i - 1;
while(j >= 0 && resultList[j].compareTo(key) > 0) {
resultList[j + 1] = resultList[j]
j--
}
resultList[j + 1] = key
}
return resultList
}
fun main(args: Array<String>) {
println("Sorting Array of elements")
val nums = arrayOf(2, 12, 89, 23, 76, 43, 12)
nums.sort()
println(Arrays.toString(nums))
println()
println("Sorting List of elements")
val nums1 = listOf(2, 12, 89, 23, 76, 43, 12)
val result = nums1.sort()
println(result)
}
Output:
Sorting Array of elements
[2, 12, 12, 23, 43, 76, 89]
Sorting List of elements
[2, 12, 12, 23, 43, 76, 89]
fun <E: Comparable<E>> Array<E>.sort() {
val len = size
for (i in 1 until len) {
var key = this[i]
var j = i - 1;
while(j >= 0 && this[j] > key) {
this[j + 1] = this[j]
j--
}
this[j + 1] = key
}
}
fun <E: Comparable<E>> List<E>.sort(): List<E> {
val len = size
val resultList = toMutableList()
for (i in 1 until len) {
var key = resultList[i]
var j = i - 1;
while(j >= 0 && resultList[j].compareTo(key) > 0) {
resultList[j + 1] = resultList[j]
j--
}
resultList[j + 1] = key
}
return resultList
}
Related Data Structures and Algorithms in Kotlin
- Bubble Sort Algorithm in Kotlin
- Heap Sort Algorithm in Kotlin
- Insertion Sort Algorithm in Kotlin
- Merge Sort Algorithm in Kotlin
- Quick Sort Algorithm in Kotlin
- Selection Sort Algorithm in Kotlin
- Stack Data Structure Implementation in Kotlin
- Queue Data Structure Implementation in Kotlin
- Deque Implementation in Kotlin
- Singly Linked List Implementation in Kotlin
- Doubly Linked List Implementation in Kotlin
- Circular Linked List Implementation in Kotlin
- Bubble Sort Algorithm in Kotlin
- Heap Sort Algorithm in Kotlin
- Insertion Sort Algorithm in Kotlin
- Merge Sort Algorithm in Kotlin
- Quick Sort Algorithm in Kotlin
- Selection Sort Algorithm in Kotlin
- Stack Data Structure Implementation in Kotlin
- Queue Data Structure Implementation in Kotlin
- Deque Implementation in Kotlin
- Singly Linked List Implementation in Kotlin
- Doubly Linked List Implementation in Kotlin
- Circular Linked List Implementation in Kotlin
Comments
Post a Comment
Leave Comment