Skip to content

Commit

Permalink
Merge pull request #16 from VladimirCreator/selection-sort / Add Sele…
Browse files Browse the repository at this point in the history
…ction sort
  • Loading branch information
VladimirCreator authored May 16, 2023
2 parents a2166db + 85d5a5f commit 4ae4244
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
3. GitHub Explorer
4. KVO
5. Reverse Polish Notation
6. Text2Binary
6. Selection Sort
7. Text2Binary

## Alphabet
A tool to sort a list of words in alphabetical order.
Expand Down Expand Up @@ -52,6 +53,15 @@ A tool for solving expressions.
### Example
![Reverse Polish Notation](./photos/reverse_polish_notation.png)

## Selection Sort
A tool for sorting collections.

### Files
1. [`./swift/selection_sort.swift`](./swift/selection_sort.swift)

### Example
![Selection Sort](./photos/selection_sort.png)

## Text2Binary
A tool for converting texts to binaries.

Expand Down
Binary file added photos/selection_sort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions swift/selection_sort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fileprivate func sort<T: Comparable>(_ array: inout [T], using predicate: (T, T) -> Bool) {
guard array.count >= 2 else { return }

for elementIndex in Int.zero..<array.count-1 {
var predicateIndex = elementIndex

for index in predicateIndex+1..<array.count {
if predicate(array[predicateIndex], array[index]) {
predicateIndex = index
}
}
if elementIndex != predicateIndex {
array.swapAt(elementIndex, predicateIndex)
}
}
}

0 comments on commit 4ae4244

Please sign in to comment.