Skip to content

Commit

Permalink
mejorar version
Browse files Browse the repository at this point in the history
  • Loading branch information
Ing-Brayan-Martinez committed Jul 1, 2024
1 parent 948e553 commit 154a57e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Sources/Ordering/SelectionSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,26 @@
// Created by Brayan Martinez on 28/6/24.
//

public func selectionSort(data: inout [Int]) {
for i in 0..<(data.count-1) {
var min_index = i
for j in i + 1..<(data.count) {
if (data[min_index] > data[j]) {
min_index = j
}
}
if (min_index != i) {
swap(first: i, second: min_index, data: &data)
}
}
}

private func swap(first: Int, second: Int, data: inout [Int]) {
var value1 = data[first]
var value2 = data[second]
let temp = value1
value1 = value2
value2 = temp
data[first] = value1
data[second] = value2
}
30 changes: 30 additions & 0 deletions Tests/OrderingTest/SelectionSortTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// SelectionSortTest.swift
//
//
// Created by Brayan Martinez on 30/6/24.
//

import Testing
@testable import Util
@testable import Ordering

@Test func selectionSortTest() async throws {
var data: [Int] = getData()

print("-- SelectionSort -- \n\n")

//before
print("Before Sorting: \n")
printData(data: &data)

//sort
print("\n")
selectionSort(data: &data)

//after
print("After Sorting: \n")
printData(data: &data)

print("Test Passed!")
}

0 comments on commit 154a57e

Please sign in to comment.