Created
March 1, 2017 15:22
-
-
Save AnthonyBY/9014c2b867dacfb5c5b4d4d68078e1af to your computer and use it in GitHub Desktop.
HackerRank: Sorting: Bubble Sort (Swift 3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// read the integer n | |
let n = Int(readLine()!)! | |
// read the array | |
var arr = readLine()!.components(separatedBy: " ").map{ Int($0)! } | |
var swapCount = 0 | |
for i in 0...arr.count-1 { | |
for j in i...arr.count-1 { | |
if arr[i] > arr[j] { | |
let temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp | |
swapCount += 1 | |
} | |
} | |
} | |
print("Array is sorted in \(swapCount) swaps.") | |
print("First Element: \(arr.first!)") | |
print("Last Element: \(arr.last!)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment