Skip to content

Instantly share code, notes, and snippets.

@byJeevan
Last active July 31, 2017 14:16
Show Gist options
  • Save byJeevan/62ea67584ae62e4781083f2612e36f25 to your computer and use it in GitHub Desktop.
Save byJeevan/62ea67584ae62e4781083f2612e36f25 to your computer and use it in GitHub Desktop.
//Input array
var arr = ["31415926535897932384626433832795", "4", "66", "100"];
func comp(s1 : String, s2:String) -> Bool {
let length1 = s1.characters.count
let length2 = s2.characters.count
if length1 < length2 {
return true;
}
if length1 > length2 {
return false;
}
if Int64(s1)! < Int64(s2)! {
return true;
}
if Int64(s1)! > Int64(s2)! {
return false;
}
return false;
}
//selection-sort
var sortedArr = [String]()
var minIndex = 0;
for i in 0..<(arr.count - 1) {
minIndex = i;
for j in (i+1)..<arr.count {
if comp(s1: arr[j], s2: arr[minIndex]) {
minIndex = j;
}
}
//swap old min value with new min
var temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
//Log the result.
print(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment