# Selection sort in scala

Scala program for Selection sort. Here more solutions.

``````/*
Scala program for selection sort
*/
class MySort()
{
// Swap the array element
def swap(arr: Array[Int], x: Int, y: Int): Unit = {
// x and y are index of array
var temp: Int = arr(x);
arr(x) = arr(y);
arr(y) = temp;
}
def selectionSort(arr: Array[Int], n: Int): Unit = {
var min: Int = 0;
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
swap(arr, i, min);
}
i += 1;
}
}
// Display array elements
def display(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
// Display element value
print("  " + arr(i));
i += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: MySort = new MySort();
// Array of integer elements
var arr: Array[Int] = Array(
8, 2, 3, 8, 1, 3, 73, 121, 54, 23, 84, 13, 67, 23, 52);
// Get the size of array
var n: Int = arr.length;
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.