Selection Sort in Kotlin

Table Of Contents

Sorting is a fundamental operation in computer science, and there are various algorithms to achieve it. One such simple yet effective algorithm is Selection Sort. In this blog post, we’ll explore the Selection Sort algorithm, its implementation in Kotlin, and analyze its time complexity.

Selection Sort Overview

Selection Sort is a comparison-based sorting algorithm that works by dividing the input array into two parts: the sorted and the unsorted sub-arrays. The algorithm repeatedly finds the minimum element from the unsorted sub-array and swaps it with the first element of the unsorted subarray. This process is repeated until the entire array is sorted.

Implementation in Kotlin

Let’s implement Selection Sort in Kotlin:

fun selectionSort(arr: IntArray) {
    val n = arr.size

    for (i in 0 until n - 1) {
        var minIndex = i

        for (j in i + 1 until n) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j
            }
        }

        // Swap the found minimum element with the first element
        val temp = arr[minIndex]
        arr[minIndex] = arr[i]
        arr[i] = temp
    }
}

fun main() {
    val array = intArrayOf(64, 25, 12, 22, 11)
    
    println("Original Array: ${array.joinToString(", ")}")

    selectionSort(array)

    println("Sorted Array: ${array.joinToString(", ")}")
}

In this implementation, the selectionSort() function takes an array of integers and sorts it in ascending order using the Selection Sort algorithm.

Analysis of Time Complexity

Selection Sort has a time complexity of O(n²) where n is the number of elements in the array. This makes it inefficient for large datasets. The algorithm performs poorly compared to more advanced sorting algorithms like Merge Sort or Quick Sort.

Conclusion

While Selection Sort is a simple algorithm to understand and implement, it is not the most efficient sorting algorithm for large datasets. In real-world scenarios, it’s often better to use more optimized algorithms for sorting. Nevertheless, learning and implementing Selection Sort can be a valuable exercise in understanding sorting algorithms and their performance characteristics.

In summary, we’ve covered the basics of Selection Sort, implemented it in Kotlin, and discussed its time complexity. I hope this blog post helps you gain a better understanding of Selection Sort and its role in sorting algorithms. Happy coding!

Written By:

Ezra Kanake

Written By:

Ezra Kanake

Ezra is a passionate Kotlin developer and technical writer. He loves working on open-source projects and sharing knowledge across the globe.

Recent Posts

Guide to JUnit 5 Functional Interfaces

In this article, we will get familiar with JUnit 5 functional interfaces. JUnit 5 significantly advanced from its predecessors. Features like functional interfaces can greatly simplify our work once we grasp their functionality.

Read more

Getting Started with Spring Security and JWT

Spring Security provides a comprehensive set of security features for Java applications, covering authentication, authorization, session management, and protection against common security threats such as CSRF (Cross-Site Request Forgery).

Read more

Creating and Publishing an NPM Package with Automated Versioning and Deployment

In this step-by-step guide, we’ll create, publish, and manage an NPM package using TypeScript for better code readability and scalability. We’ll write test cases with Jest and automate our NPM package versioning and publishing process using Changesets and GitHub Actions.

Read more