Skip to content
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

Assignment 1 - Selection Sort #2

Open
gmertk opened this issue Nov 17, 2015 · 6 comments
Open

Assignment 1 - Selection Sort #2

gmertk opened this issue Nov 17, 2015 · 6 comments

Comments

@gmertk
Copy link
Owner

gmertk commented Nov 17, 2015

When you ask someone to sort a list of numbers in ascending order, you will see that the common intuition will be like "Select the minimum card and put it in the first place. Select the second minimum and put it in the second place. And so on". This is called selection sort.

  1. Write a selectionSort function that takes an integer array and returns it as sorted in ascending order. Don't mutate the original array (don't use inout). Note that there is already a swap function in the standard library. When using the swap, you need to guard it from swapping a location with itself.
  2. Turn the function into a generic function so that it can sort any type of array. Only constraint is that elements of the array should conform to the Comparable protocol.
  3. Instead of sorting the array in ascending order, modify your function to accept a closure that takes two arguments of the same type as the array’s contents, and returns Bool. You can name this closure parameter isOrderedBefore (as in sort function of the standard library). Check what @noescape means in Swift. Also you now use another function to do the comparison, you can remove the Comparable constraint.
  4. Instead of only sorting arrays, modify your function to sort any type conforming to SequenceType. Make your function to be an extension of SequenceType so that we can use it as [1, 3, 2].selectionSort(<) and [1: "b", 2: "a"].selectionSort {$0.0 > $1.0}.

Due Date: Next Wednesday 25.11.2015

Next assignment will be about writing an insertion sort (as you guessed). Feel free to send comments 😄

@phelgo
Copy link

phelgo commented Nov 21, 2015

Excellent initiative. I'm travelling at the moment but I'll submit a PR before the deadline.

@gmertk
Copy link
Owner Author

gmertk commented Nov 21, 2015

Thank you @phelgo!

Will be waiting for your PR.

@phelgo phelgo mentioned this issue Nov 24, 2015
@DominicFrei
Copy link

@gmertk Awesome idea! I like people who like to teach.
I found this repo just yesterday and missed the deadline for assignment 1, but I'd try to present some helping comments on the current pull requests if that's ok (actually I already started). :)

@garigari-kun
Copy link

@gmertk Nice work! Me too. I just found this repo so I can't meet the deadline for assignment1. Would you still accept PR for assignment1??

@DominicFrei
Copy link

@gmertk I just thought:
Maybe a nice idea is to not have deadlines at all, but just different tasks that can be done and whenever someone new finds the repo, PRs are possible for every tasks, which lead to reviews and discussions, which again lead to improving us all.
That sounds nice. :)

@gmertk
Copy link
Owner Author

gmertk commented Nov 27, 2015

@DominicFrei It could be nice. But I would like to have a soft deadline to keep going. Deadlines are good. It motivates you to solve one assignment every few days.

@K-jpn PRs are still possible for older assignments so that everyone can comment them. Though I'm not sure if we should keep them open for all the time.

diegopetrucci added a commit to diegopetrucci/SwiftAlgorithmsClassroom that referenced this issue Nov 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants