Skip to content

Commit

Permalink
selection sort
Browse files Browse the repository at this point in the history
  • Loading branch information
suparna-khamaru authored Jun 14, 2020
1 parent b292468 commit 67c40c0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions SelectionSort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Foundation

func SelectionSort() -> [Int] {

var numbers = [7,6,2,19,11,45,99,23,56,78,9]
print("Before sorting: \(numbers)")

var minIndex = 0

for i in 0..<numbers.count {

minIndex = i

for j in (i+1)..<numbers.count {

if numbers[j] < numbers[minIndex] {
minIndex = j
}

numbers.swapAt(i, minIndex)
}
}
return numbers
}

print("After sorting: \(SelectionSort())")

/*
Output printed:

Before sorting: [7, 6, 2, 19, 11, 45, 99, 23, 56, 78, 9]
After sorting: [2, 6, 7, 9, 11, 19, 23, 45, 78, 56, 99]

*/

0 comments on commit 67c40c0

Please sign in to comment.