# Selection sort in swift

Swift program for Selection sort. Here more solutions.

``````import Foundation
/*
Swift 4 program for selection sort
*/
class MySort
{
// Swap the array element
func swap(_ arr: inout[Int], _ x: Int, _ y: Int)
{
// x and y are index of array
let temp: Int = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
func selectionSort(_ arr: inout[Int], _ n: Int)
{
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
self.swap( &arr, i, min);
}
i += 1;
}
}
// Display array elements
func display(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
// Display element value
print("", arr[i], terminator: " ");
i += 1;
}
print("\n", terminator: "");
}
static func main()
{
// Array of integer elements
var arr: [Int] = [8, 2, 3, 8, 1, 3, 73, 121,
54, 23, 84, 13, 67, 23, 52];
// Get the size of array
let n: Int = arr.count;
print(" Before Sort :");
// Test
print(" After Sort :");
}
}
MySort.main();``````

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.