Kotlin Program to Sort an Array in Ascending Order

In this blog post, we will learn how to write a Kotlin program to Sort an Array in Ascending Order.

Sorting an array in ascending order is a common operation in programming. It allows us to arrange the elements in increasing order, making it easier to search, compare, or present the data. In this blog post, we will explore a Kotlin program that efficiently sorts an array in ascending order. We will walk through the program step by step, explaining the logic behind it.

Kotlin Program: Sorting an Array in Ascending Order

To sort an array in ascending order in Kotlin, we can utilize a sorting algorithm called "Selection Sort." Let's dive into the code:
fun selectionSort(array: IntArray) {
    val n = array.size

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

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

        if (minIndex != i) {
            val temp = array[i]
            array[i] = array[minIndex]
            array[minIndex] = temp
        }
    }
}

fun main() {
    val numbers = intArrayOf(10, 5, 8, 20, 13)

    println("Original Array: ${numbers.joinToString(", ")}")
    selectionSort(numbers)
    println("Sorted Array (Ascending Order): ${numbers.joinToString(", ")}")
}

Output:

Original Array: 10, 5, 8, 20, 13
Sorted Array (Ascending Order): 5, 8, 10, 13, 20
Explanation: 
The selectionSort() function takes an integer array as input and sorts it in ascending order. 

We retrieve the size of the array using the size property and store it in the variable n

We use a nested loop structure to perform the selection sort algorithm. The outer loop iterates from 0 to n - 1, representing the current position for selecting the minimum element. 

Inside the outer loop, we initialize the minIndex variable to the current position. 

The inner loop starts from i + 1 and iterates until n, comparing each element with the element at minIndex. If a smaller element is found, we update minIndex to the index of that element. 

After completing the inner loop, we check if the minIndex is different from the current position i. If it is, we swap the elements at positions i and minIndex to place the minimum element in the correct position.

The outer loop continues until all elements are sorted, resulting in the array being sorted in ascending order. 

In the main() function, we create a sample input array (val numbers = intArrayOf(10, 5, 8, 20, 13)) and print the original array to the console using println(). Then, we call the selectionSort() function to sort the array in ascending order and print the sorted array to the console. 

Conclusion

In this blog post, we have discussed a Kotlin program that efficiently sorts an array in ascending order using the selection sort algorithm. Sorting an array is a fundamental operation in programming, and understanding this program equips you with the necessary skills to manipulate and organize data. 

Feel free to explore and modify the code to suit your specific needs. Sorting algorithms are essential for various applications, such as data analysis, searching, and organizing information. Happy coding!

Comments