# Sorting Algorithms

(use with UCSMP Precalculus and Discrete Mathematics, Lesson 6-9)

If you can't see the demo above, click here.

## Directions

This applet demonstrates two different sorting algorithms, bubble sort and quick sort. You can step through these algorithms to see exactly how they work by clicking the STEP button, or you can watch them run through with the RUN button. Each step represents one comparison and possibly a swap. For the bubble sort algorithm there is an additional option called shortcut. The shortcut option tells the bubble sort algorithm to quit if it has made a whole pass without making a swap. If this happens, you know it is already sorted. This will often eliminate several passes. However, the algorithm presented in the book doesn't include this option so it is not turned on by default.

On the representations, a bar will turn blue when it is in the correct position. The arrows to the left of the bars represent the state of the algorithm. For quick sort, the blue arrow points to the pivot, the bottom red arrow points to last line that was shorter than the pivot, and the other arrow points to the line currently being compared with the pivot. For bubble sort, the lower arrow points to the current line that is being compared and the upper arrow points to the line such that all the lines after it are in order.