#Sorting#Computer-Science#CS251 A sorted array looks like this Such that
Any sorting algorithm solves the following problem. “Given an unsorted array, decide how to permute the array such that the array elements are sorted”
- Sorting is a decision making algorithm, the algorithm has to choose things.
There are 4 main things we look at in sorting algorithms
Stability
- Does it keep it’s output stable?
- This may or may not be important based on the problem
- Example:
- If sorting algorithm returns it is stable. The ordering of like elements is preserved.
- If it returns it is considered unstable
Certification
- Can we prove the algorithm will work?
- This can be trivial or it can be difficult
Runtime Complexity
- How fast is it?
- We analyze this with Runtime Expressions and Big O Notation
Space Complexity
- If a copy of the array is needed, the algorithm will require more space
- An In Place algorithm works directly on the input array
- A not in place algorithm needs some copy of the input to work
Lowest Runtime Proof
No sorting algorithm can do better than with compares/swaps.
Let’s consider an array of 3 elements. Let’s make a decision tree of every comparison a sorting algorithm COULD do on this array.
The leaves of this tree correspond to every permutation of the array. We have n! leaves, sorting algorithms find one of the n! permutations that is sorted.
This isn’t a full binary tree, it’s missing 4 leaves (shown with dotted lines). We can work with half of the items to make proving easier. Since this is an inequality, it will still hold. Let’s just say every item is We will have of them
A sorting algorithms number of compares CANNOT grow slower than We can only do faster sorting if we don’t compare items. This leads to the use of Non-Comparison Sorts
Sorting Algorithms
There are various sorting algorithms. Here’s basic information on each, go to each sorting algorithms individual page to see more about it.
Bubble Sort
- Best Case
- Worst Case
- Stable
- In Place
Selection Sort
- Best Case
- Worst Case
- NOT Stable
- In Place
Insertion Sort
- Best Case
- Worst Case
- Stable
- In Place
- Variation: Shell Sort
Heap Sort
- Not Stable
- In Place
Merge Sort
- Stable
- Not In Place
Quick Sort
- Best Case )
- Worst Case
- Not Stable
- In Place
Counting Sort
- Stable
- Not in Place
Radix Sort
- Time depends on sorting algorithm used
- Stable
- Not in place