Skip to content

Assignment 4 - Quicksort #34

@gmertk

Description

@gmertk

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.

  1. 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.
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions