# Selection sort in kotlin

Kotlin program for Selection sort. Here mentioned other language solution.

``````/*
Kotlin program for selection sort
*/
class MySort
{
// Swap the array element
fun swap(arr: Array < Int > , x: Int, y: Int): Unit
{
// x and y are index of array
val temp: Int = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
fun selectionSort(arr: Array < Int > , n: Int): Unit
{
var min: Int ;
var i: Int = 0;
// Execute loop from 0..n
while (i < n)
{
// Get current index
min = i;
var j: Int = i + 1;
while (j < n)
{
if (arr[min] > arr[j])
{
// Get the minimum element index
min = j;
}
j += 1;
}
if (i != min)
{
// Swap minimum element at i index
this.swap(arr, i, min);
}
i += 1;
}
}
// Display array elements
fun display(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
// Display element value
print("  " + arr[i]);
i += 1;
}
print("\n");
}
}
fun main(args: Array < String > ): Unit
{
// Array of integer elements
val arr: Array < Int > = arrayOf(
8, 2, 3, 8, 1, 3, 73, 121, 54, 23, 84, 13, 67, 23, 52);
// Get the size of array
val n: Int = arr.count();
println(" Before Sort :");
// Test
println(" After Sort :");
}``````

Output

`````` Before Sort :
8  2  3  8  1  3  73  121  54  23  84  13  67  23  52
After Sort :
1  2  3  3  8  8  13  23  23  52  54  67  73  84  121``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.