You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The most famous sorting algorithm. Invented by Tony Hoare. There is a short interview with him telling his invention of quicksort. Read more about quicksort. We will go over two approaches here. First one is the real implementation of quicksort. Second one is less efficient but functional implementation with filters.
git checkout assignment004 to start.
Quicksort In-place
Implement Lomuto's partition algorithm. Remember that you need to guard swap from swapping a location with itself.
Implement Hoare's partition algorithm. Note that if you follow the pseudo code given in Wikipedia, the pivot's final location might not be at the returned index. You can check Sedgewick's Algorithms book for another version. Add more tests when you are done with your implementation.
By using your hoarePartition, implement an in-place quicksort.
Read about the Dutch National Flag problem. Your function takes a pivot's index. It should rearrange the elements such that all elements less than pivot come first, followed by elements equal to pivot, followed by elements greater than pivot.
You can check out how Swift implements sort. The file is a GYB template which stands for Generate Your Boilerplate. You need to clone the Swift repo, and run ./utils/gyb on the Sort.swift.gyb to convert it to actual Swift source code. As you can see, the function is called introSort, a hybrid sorting algorithm where quicksort falls back to insertion sort and heap sort.
Functional quicksort
This is a rather inefficient but neat solution. Pick a pivot and remove it from the array. Then use filter to create two arrays: one containing smaller items than the pivot, other one containing the rest. Then call the function on these arrays, and append them to each other with the pivot in the middle.
Due date: 27.12.2015
The text was updated successfully, but these errors were encountered:
The most famous sorting algorithm. Invented by Tony Hoare. There is a short interview with him telling his invention of quicksort. Read more about quicksort. We will go over two approaches here. First one is the real implementation of quicksort. Second one is less efficient but functional implementation with filters.
git checkout assignment004
to start.swap
from swapping a location with itself.hoarePartition
, implement an in-place quicksort.sort
. The file is a GYB template which stands for Generate Your Boilerplate. You need to clone the Swift repo, and run./utils/gyb
on theSort.swift.gyb
to convert it to actual Swift source code. As you can see, the function is calledintroSort
, a hybrid sorting algorithm where quicksort falls back to insertion sort and heap sort.filter
to create two arrays: one containing smaller items than the pivot, other one containing the rest. Then call the function on these arrays, and append them to each other with the pivot in the middle.Due date: 27.12.2015
The text was updated successfully, but these errors were encountered: