Project 3 of 15, for Neil
Sorting Visualiser
A sorting visualiser for the sim league's lap times: selection, insertion, merge and quick sort, each played back frame by frame as bars, then raced against sorted() on ten thousand laps, fastest first.
Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- NeilI've got the sim league's laps for the whole term, all ten thousand of them in the order they were set.
- NeilI want them fastest first, and everyone tells me to just sort it, but nobody ever says which sort.
- NeilObviously that means I want to watch four of them race each other.
- PaulThere are four worth knowing, and once you've built them you'll see why nobody bothers writing their own.
A sorting visualiser, in two files, each sort written by hand: no sorted() or .sort() inside your functions. A lap is its time off the track record, in tenths of a second, so the smallest is the fastest. Every sort takes a frames list (or None, the default) and, if it's a list, puts a copy of the pile on it after each pass or round, so the work can be played back.
In piles.py, two sorts that change the list they're given, fastest first, and hand back None, like .sort(), and a way to draw a pile:
draw(pile)gives back the pile as text, one line per lap: its tenths right-aligned in three places, a space, and a#for each tenth.selection_sort(laps, frames=None): find the fastest of what's left and swap it into place. A frame after every pass.insertion_sort(laps, frames=None): take each lap in turn and slide it left past every slower one. A frame after every pass.
In sorter.py, two faster sorts for the whole league, which hand back a new list sorted by key and leave items as it was. Both are stable: on a tie, items keep the order they came in, so the lap set first stays ahead, as it does in qualifying.
merge(left, right, key)takes two lists already sorted bykeyand gives back one, sorted bykey. On a tie, the item fromleftcomes first.merge_sort(items, key=lambda item: item, frames=None): every item starts as a pile of one; neighbouring piles are merged in pairs, round after round, until one pile is left.quick_sort(items, key=lambda item: item, frames=None): everything starts in one pile; each round, every pile of two or more is split round its first item into the faster ones, then each item the same as the pivot as a pile of its own, then the slower ones, until no pile has two items.
For merge and quick sort, a frame is every pile after the round, laid end to end. The visualiser at the bottom of sorter.py is written.
7 ####### 3 ### 9 ######### 3 ### 1 # 12 ############ 5 ##### Which sort: selection, insertion, merge or quick? merge Step 1 3 ### 7 ####### 3 ### 9 ######### 1 # 12 ############ 5 ##### Step 2 3 ### 3 ### 7 ####### 9 ######### 1 # 5 ##### 12 ############ Step 3 1 # 3 ### 3 ### 5 ##### 7 ####### 9 ######### 12 ############ Merge sort: 3 steps Sim league: 10000 laps, merge and quick agree with sorted(): True sorted() 0.002 s, merge 0.033 s, quick 0.016 s
What you learn
What's in it
- Fastest to the front
- One pass
- Sliding into place
- One lap slides
- How Paul sorts his marking
- Two sorts, frame by frame
- Split round a pivot
- After one split
- Two tidy piles into one
- When the loop stops
- Ties keep their order
- Three laps at four tenths
- How Paul merges two piles
- Fix Yahya's version
- Make the sorting visualiser
- Ship it