How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Kotlin programming language
How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Kotlin programming language
Table of Contents
Problem Statement
An O(n2) sorting algorithm which moves elements one at a time into the correct position. The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary. To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part. Although insertion sort is an O(n2) algorithm, its simplicity, low overhead, good locality of reference and efficiency make it a good choice in two cases:
The algorithm is as follows (from wikipedia): Writing the algorithm for integers will suffice.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Kotlin programming language
The code snippet you provided is an implementation of the insertion sort algorithm in Kotlin. Insertion sort is a relatively simple sorting algorithm that works by iterating through the array from left to right, and for each element, inserting it into its correct position in the sorted portion of the array.
The insertionSort
function takes an array of integers as input and sorts it in ascending order. The algorithm works as follows:
-
For each element in the array, starting with the second element, do the following:
- Save the current element in a variable called
value
. - Set a variable called
subIndex
to the index of the previous element in the array (index - 1
). - While
subIndex
is greater than or equal to 0 and the element at indexsubIndex
in the array is greater thanvalue
, do the following:- Swap the element at index
subIndex + 1
in the array with the element at indexsubIndex
. - Decrement
subIndex
by 1.
- Swap the element at index
- Insert the
value
into the array at indexsubIndex + 1
.
- Save the current element in a variable called
-
Repeat step 1 for each element in the array.
The time complexity of insertion sort is O(n^2), where n is the number of elements in the array. This means that the algorithm takes a relatively long time to sort large arrays. However, insertion sort is relatively simple to implement and can be efficient for small arrays.
The main
function in the code snippet you provided tests the insertionSort
function by sorting an array of integers. The unsorted array is printed to the console, the insertionSort
function is called to sort the array, and the sorted array is printed to the console.
Source code in the kotlin programming language
fun insertionSort(array: IntArray) {
for (index in 1 until array.size) {
val value = array[index]
var subIndex = index - 1
while (subIndex >= 0 && array[subIndex] > value) {
array[subIndex + 1] = array[subIndex]
subIndex--
}
array[subIndex + 1] = value
}
}
fun main(args: Array<String>) {
val numbers = intArrayOf(5, 2, 3, 17, 12, 1, 8, 3, 4, 9, 7)
fun printArray(message: String, array: IntArray) = with(array) {
print("$message [")
forEachIndexed { index, number ->
print(if (index == lastIndex) number else "$number, ")
}
println("]")
}
printArray("Unsorted:", numbers)
insertionSort(numbers)
printArray("Sorted:", numbers)
}
You may also check:How to resolve the algorithm Mandelbrot set step by step in the PHP programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Scheme programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the Raku programming language