Skip to content

Commit 106b65e

Browse files
committed
addition of Bubble sort algorithm
1 parent 185571d commit 106b65e

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Foundation
2+
3+
public func BubbleSort<T> (_ elements: [T]) -> [T] where T: Comparable {
4+
var array = elements
5+
6+
for i in 0..<array.count {
7+
for j in i+1..<array.count {
8+
if array[j] < array[i] {
9+
let tmp = array[i]
10+
array[i] = array[j]
11+
array[j] = tmp
12+
}
13+
}
14+
}
15+
16+
return array
17+
}
18+
19+
20+
var array = [Int]()
21+
22+
for _ in 0...10 {
23+
array.append(Int.random(in: 0...500))
24+
}
25+
26+
print("before:",array)
27+
print("after:",BubbleSort(array))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// BubbleSort.swift
3+
//
4+
// Created by Julio Brazil on 1/10/18.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7+
// associated documentation files (the "Software"), to deal in the Software without restriction, including
8+
// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
10+
// following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all copies or substantial
13+
// portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16+
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
17+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18+
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19+
// OR OTHER DEALINGS IN THE SOFTWARE.
20+
//
21+
22+
import Foundation
23+
24+
/// Performs the bubble sort algorithm in the array
25+
///
26+
/// - Parameter elements: the array to be sorted
27+
/// - Returns: an array with the same elements but in order
28+
public func BubbleSort<T> (_ elements: [T]) -> [T] where T: Comparable {
29+
var array = elements
30+
31+
for i in 0..<array.count {
32+
for j in i+1..<array.count {
33+
if array[j] < array[i] {
34+
let tmp = array[i]
35+
array[i] = array[j]
36+
array[j] = tmp
37+
}
38+
}
39+
}
40+
41+
return array
42+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Bubble Sort/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

0 commit comments

Comments
 (0)