-
-
Notifications
You must be signed in to change notification settings - Fork 359
Added BogoSort in Swift 4.1 #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
f5ccae7
3e82fd1
090d5b7
fa07e5e
b68abf2
e4c61b1
8450b8c
ba208f7
2f1723b
cae4e82
e4e82c3
a88b732
f18feab
8c3b742
0a0fc50
414a7e8
9578b02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ Maxime Dherbécourt | |
Jess 3Jane | ||
Pen Pal | ||
Chinmaya Mahesh | ||
Kjetil Johannessen | ||
Unlambder | ||
Kjetil Johannessen | ||
CDsigma |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,10 @@ | |
"name": "Go" | ||
}, | ||
{ | ||
"lang": "swift", | ||
"name": "Swift" | ||
}, | ||
{ | ||
"lang": "racket", | ||
"name": "Racket" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,9 @@ In code, it looks something like this: | |
{% sample lang="rs" %} | ||
[import, lang:"rust"](code/rust/bogosort.rs) | ||
{% endmethod %} | ||
{% sample lang="swift" %} | ||
[import, lang:"swift"](code/swift/bogoSort.swift) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the capitalization of the "S" in the file name. Some languages (like Java) require capitalization to match classes and such inside the files, but I'd prefer to keep everything else lowercase. Unless it's some kind of convention in Swift. In that case, keep it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just updated the file name 👍 |
||
{% endmethod %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's only supposed to be one |
||
|
||
That's it. | ||
Ship it! | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
|
||
|
||
func isSorted(inputArray: [Int]) -> Bool { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick 1: 2 out of 3 functions start with an empty line. I don't know which style is best, but I do know that consistency is always rule number 1 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya that's a good note I'll make the line spacing consistent in each function |
||
for i in 0..<inputArray.count-1 { | ||
if inputArray[i] > inputArray[i+1] { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
|
||
|
||
func shuffle(inputArray: inout [Int]) -> [Int] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick 2: a quick Google search showed me that there is a native There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if there's a reason to use version 4.1 specifically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh my bad, I had not seen the version was specifically 4.1, it's right there in the title -_- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most people who use Swift tend to use the version which comes with Xcode. Currently the stable build of Xcode uses Swift 4.1 so I thought that would be the best choice. There is an Xcode 10 beta which uses Swift 4.2 but that won't be the stable build for a few months so until then I think Swift 4.1 is the best choice. |
||
|
||
var shuffledArray = [Int]() | ||
|
||
for _ in 0..<inputArray.count { | ||
let rand = Int(arc4random_uniform(UInt32(inputArray.count))) | ||
shuffledArray.append(inputArray[rand]) | ||
inputArray.remove(at: rand) | ||
} | ||
|
||
return shuffledArray | ||
} | ||
|
||
|
||
|
||
func bogoSort(sortArray: inout [Int]) -> [Int] { | ||
|
||
while(!isSorted(inputArray: sortArray)) { | ||
sortArray = shuffle(inputArray: &sortArray) | ||
} | ||
|
||
return sortArray | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two lines with the brackets are indented with a bunch of spaces and then 3 tabs. The file is tab indented. Try to make sure you only use tabs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that just committed some changes so no more spaces are used