Introduction: Why Study Algorithms and Data Structures in Kotlin?
Kotlin is a modern, concise, and safe programming language that is actively used for Android development, backend services, and even desktop applications. But knowing how to write code in Kotlin is only half the battle. To become a truly good developer, you need to understand how algorithms and data structures work.
Why does a beginner need this? First, knowledge of basic structures (lists, stacks, queues, hash tables) helps you write more efficient and faster code. Second, algorithms are the foundation for solving technical problems in interviews. Third, it's simply interesting: you start to see the logic and mathematics behind the code.
In this article, we will cover key algorithms and data structures in Kotlin from scratch. All examples will be simple, understandable, and ready to run. Let's go!
1. Basic Data Structures in Kotlin
Arrays and Lists
Kotlin has two main types of collections: Array (fixed size) and List (dynamic). For most tasks, it is more convenient to use List.
fun main() { // Fixed-size array val numbersArray = arrayOf(1, 2, 3, 4, 5) println("Array: ${numbersArray.joinToString()}")
// Mutable list val mutableList = mutableListOf(10, 20, 30) mutableList.add(40) println("List after adding: $mutableList")
// Immutable list val immutableList = listOf(100, 200, 300) println("Immutable list: $immutableList")}Stack
A stack works on the LIFO (Last In, First Out) principle. Kotlin does not have a built-in Stack class, but it is easy to implement using MutableList.
class Stack<T> { private val elements = mutableListOf<T>()
fun push(item: T) = elements.add(item) fun pop(): T? = if (isEmpty()) null else elements.removeAt(elements.size - 1) fun peek(): T? = elements.lastOrNull() fun isEmpty() = elements.isEmpty() fun size() = elements.size}
fun main() { val stack = Stack<Int>() stack.push(1) stack.push(2) stack.push(3)
println("Top element: ${stack.peek()}") // 3 println("Popping: ${stack.pop()}") // 3 println("Now top: ${stack.peek()}") // 2}Queue
A queue works on the FIFO (First In, First Out) principle. In Kotlin, it is convenient to use Java's LinkedList for this.
import java.util.LinkedList
class Queue<T> { private val elements = LinkedList<T>()
fun enqueue(item: T) = elements.add(item) fun dequeue(): T? = elements.poll() fun peek(): T? = elements.peek() fun isEmpty() = elements.isEmpty() fun size() = elements.size}
fun main() { val queue = Queue<String>() queue.enqueue("First") queue.enqueue("Second") queue.enqueue("Third")
println("Serving: ${queue.dequeue()}") // First println("Next: ${queue.peek()}") // Second}2. Sorting Algorithms in Kotlin
Bubble Sort
A simple but slow algorithm. Suitable for learning, but not for real projects.
fun bubbleSort(arr: MutableList<Int>) { val n = arr.size for (i in 0 until n - 1) { for (j in 0 until n - i - 1) { if (arr[j] > arr[j + 1]) { val temp = arr[j] arr[j] = arr[j + 1] arr[j + 1] = temp } } }}
fun main() { val list = mutableListOf(64, 34, 25, 12, 22, 11, 90) bubbleSort(list) println("Sorted with bubble sort: $list")}