Skip to content

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

Merged
merged 17 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ Maxime Dherbécourt
Jess 3Jane
Pen Pal
Chinmaya Mahesh
Kjetil Johannessen
Unlambder
Kjetil Johannessen
CDsigma
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
"name": "Go"
},
{
"lang": "swift",
"name": "Swift"
},
{
Copy link
Contributor

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.

Copy link
Contributor Author

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

"lang": "racket",
"name": "Racket"
},
Expand Down
3 changes: 3 additions & 0 deletions chapters/sorting_searching/bogo/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updated the file name 👍

{% endmethod %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's only supposed to be one {% endmethod %} statement at the end. You have to remove the one in line 38.


That's it.
Ship it!
Expand Down
39 changes: 39 additions & 0 deletions chapters/sorting_searching/bogo/code/swift/bogoSort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation


func isSorted(inputArray: [Int]) -> Bool {

Copy link
Member

@jiegillet jiegillet Jun 29, 2018

Choose a reason for hiding this comment

The 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 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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] {
Copy link
Member

Choose a reason for hiding this comment

The 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 shuffle function in Swift 4.2+. Maybe it's best to use it.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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 -_-
But yeah, I'd like to hear the reason too then :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}