sorting_algorithm_animations
sorting_algorithm_animations
Insertion sort: works by taking elements from the list one by one and inserting
them in their correct position into a new sorted list. In arrays, the new list and
the remaining elements can share the array's space
Selection sort always performs Θ(n) swaps, while insertion sort performs Θ(n2)
swaps in the average and worst cases. Because swaps require writing to the
array, selection sort is preferable if writing to memory is expensive.
Bubble sort: The algorithm starts at the beginning of the data set. It compares
the first two elements, and if the first is greater than the second, it swaps
them. It continues doing this for each pair of adjacent elements to the end of
the data set. It then starts again with the first two elements, repeating until no
swaps have occurred on the last pass.
Merge Sort: starts by comparing every two elements (i.e., 1 with 2, then 3 with
4...) and swapping them if the first should come after the second. It then
merges each of the resulting lists of two into lists of four, then merges those
lists of four, and so on; until at last two lists are merged into the final sorted
list.
Heap Sort: works by determining the largest element of the list, placing that at
the end of the list, then continuing with the rest of the list, but accomplishes
this task efficiently by using a data structure called a heap, a special type of
binary tree.
Quick Sort is a divide and conquer algorithm which relies on
a partition operation: to partition an array, we choose an element, called
a pivot, move all smaller elements before the pivot, and move all greater
elements after it. This can be done efficiently in linear time and in place. We
then recursively sort the lesser and greater sublists. The most complex issue
in quicksort is choosing a good pivot element; consistently poor choices of
pivots can result in drastically slower O(n²) performance, but if at each step
we choose a random entry as the pivot, the runtime is O(n log n).
More information about these sorts and the code can be see by clicking on the
algorithm name in the next activity.
For each of the following questions, first use your understanding of the
to test your solution. Third, if your conjecture did not agree with the actual
have few unique values, does that make the quicksort faster or slower?
Sorts have been studied for decades. The ideal sort depends on the initial
order of the data and whether stability is required.
Memory Usage: Some algorithms store the values in the original array while it
is being sorted and don't need any extra space. We call these algoirthms in
place. Recursive methods need extra space for a runtime stack. Others
require working space in addition to the original storage space.
Complexity: Some sorts are O(n log n) while others are O(n2).
Some sorting methods are negatively impacted by data that is in reverse order
or data that has very few unique entries.
In the sort detective which follows, you are able to explore various properties
of sorting algorithms. Notice:
a. The specific data to be sorted affects running time. The same sort doesn't always
take the same time for a specific data size.
b. For large data sets, the difference in speed between algorithms is dramatic.