Merge Sort
- source code (cpp)
- Algorithm
to sort a[0...n] in nondecreasing order
- divide the array into half.
- recursively sort left side
- recursively sort right side
- merge left and right
- Divide-And-Conquer paradigm
- Time Complexity
- worst, average, best: O(n log n)
- Cons
- requires 2n space for a temporary array
- uses Recursion, log n stack space
- good to use in large data sets.
|
Insertion Sort - source code (cpp cpp2)
- Algorithm to sort a[0...n] in nondecreasing order
- Find minimum of a[0...n] and store it at a[0].
- for (j=1, j<n; j++)
- place a[j] in its correct position in the sorted set a[0...j-1]
- Time Complexity
- worst: O(n2); -- sorted in decreasing order
- best: Θ(n); -- sorted in nondecreasing order
- Good only for small data sets
|
Quick Sort - source code (java)
- Algorithm to sort a[0...n] in nondecreasing order
- Uses partitioning scheme
- selects a pivot.
- everything to the left of the pivot is smaller than the pivot
- everything to the right of the pivot is larger than the pivot
- First, partition among a pivot j. Upon completion, we know everything left of j is less than j, and everything right of j is greater than j
- Then, recursively partition left and right side of j.
- Time Complexity
- worst: O(n2); -- sorted in nondecreasing order, with first element as pivot
- average, best:O(n log n); -- values are in random order
- Space Complexity
- worst: n-1 stack space needed for recursion
- Use random pivots to break the chances of dealing with the worst case (randomized quicksort)
- Good on random data of all sizes, preferably large data sets
- Extremely fast in practical applications
|
Selection - source code (java)
- Algorithm to select the kth smallest item of a set
- Uses paritioning scheme as in quicksort
- select a pivot, then partition. The position of the pivot is at a[j].
- if j == k, then the pivot is the kth smallest
- if k < j, then partition a[0...j-1]
- if k > j, then partition a[j+1...n]
- Time Complexity
- Worst Case: O(n2)
- k = n and the elements are arranged so that the pivot on the ith call to partition() is the ith smallest number
- Best, Average Case: O(n)
|