-
Notifications
You must be signed in to change notification settings - Fork 2
/
BubbleSort.swift
46 lines (30 loc) · 875 Bytes
/
BubbleSort.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// BubbleSort.swift
// AdvancedDataStructures
//
// Created by Vladislav Fitc on 26.10.17.
// Copyright © 2017 Fitc. All rights reserved.
//
import Foundation
class BubbleSort<E: Comparable>: SortAlgorithm {
typealias Element = E
let input: [Element]
var output: [Element] = []
init(input: [Element]) {
self.input = input
}
func perform() {
var array = input
var sorted: Bool = false
while !sorted {
sorted = true
for index in 0..<array.endIndex-1 {
if array[index] > array[index+1] {
array.swapAt(index, index+1)
sorted = false
}
}
}
output = array
}
}