Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Algo report Sorting Techniques
1. Bubble sort
Bubble sort has worst-case and average complexity both О(n2
), where n is the number of items
being sorted.
The only significant advantage that bubble sort has over most other implementations,
even quicksort, but not insertion sort, is that the ability to detect that the list is sorted is efficiently
built into the algorithm.
When the list is already sorted (best-case), the complexity of bubble sort is only O(n).
Data structure Array
Worst case performance
Best case performance
Average case performance
Worst case space complexity auxiliary
Insertion sort
Efficient for (quite) small data sets
Adaptive (i.e., efficient) for data sets that are already substantially sorted: the time
complexity is O(n + d), where d is the number of inversions
More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such
as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
Stable; i.e., does not change the relative order of elements with equal keys
In-place; i.e., only requires a constant amount O(1) of additional memory space
The best case input is an array that is already sorted. In this case insertion sort has a linear
running time (i.e., Θ(n)).
The average case and the worst case is quadratic (i.e., O(n2
)).
Class Sorting algorithm
Data structure Array
Worst case performance О(n2
) comparisons, swaps
Best case performance O(n) comparisons, O(1)
swaps
Average case performance О(n2
) comparisons, swaps
Worst case space
complexity
О(n) total, O(1) auxiliary
2. Selection sort
It has O(n2) time complexity.
Generally performs worse than the similar insertion sort.
Selection sort is noted for its simplicity, and it has performance advantages over more
complicated algorithms in certain situations, particularly where auxiliary memory is
limited.
Selection sort is preferable to insertion sort in terms of number of writes (Θ(n) swaps
versus Ο(n2
) swaps). (This can be important if writes are significantly more expensive than
reads, such as withEEPROM or Flash memory, where every write lessens the lifespan of the
memory.)
Quicksort
Worst case performance O(n2
)
Best case performance O(n log n) (simple partition)
or O(n) (three-way partition and
equal keys)
Average case
performance
O(n log n)
Worst case space
complexity
O(n) auxiliary (naive)
O(log n) auxiliary (Sedgewick
1978)
O(n log n) comparisons to sort n items.
In the worst case, it makes O(n2) comparisons.
Quicksort is often faster in practice than other O(n log n) algorithms. Additionally, quicksort's
sequential and localized memory references work well with a cache.
Is not a stable sort.
O(log n) additional space used by the stack during the recursion
Choice of pivot
The leftmost element of the partition would often be chosen as the pivot element.
Unfortunately, this causes worst-case behavior on already sorted arrays.
The problem was easily solved by choosing either a random index for the pivot, choosing
the middle index of the partition or (especially for longer partitions) choosing the median of
the first, middle and last element of the partition for the pivot (as recommended
by Sedgewick).
3. Complexity
Worst case :
T(n) = O(n²)
Avg Case :
T(n) = O(n log n).
Solving the recurrence gives C(n) = 1.39n log₂ n.
This means that, on average, quicksort performs only about 39% worse than in its best case. In
this sense it is closer to the best case than the worst case.
Heapsort
Worst case performance
Best case performance [1]
Average case performance
Worst case space complexity auxiliary
Although somewhat slower in practice on most machines than a well-implemented quicksort,
it has the advantage of a more favorable worst-case O(n log n) runtime. Heapsort is an in-
place algorithm, but it is not a stable sort.
Because of the O(n log n) upper bound on heapsort's running time and constant upper
bound on its auxiliary storage, embedded systems with real-time constraints or systems
concerned with security often use heapsort.
Heapsort requires only a constant amount of memory.
4. Merge sort
Worst case performance O(n log n)
Best case performance O(n log n) typical,
O(n) natural variant
Average case performance O(n log n)
Worst case space complexity O(n) auxiliary
Most implementations produce a stable sort.
If the running time of merge sort for a list of length n is T(n), then the recurrence T(n) =
2T(n/2) + n.
For large n and a randomly ordered input list, merge sort's expected (average) number of
comparisons approaches α·n fewer than the worst case
where
In the worst case, merge sort does about 39% fewer comparisons than quicksort does in
the average case.
In terms of moves, merge sort's worst case complexity is O(n log n)—the same complexity
as quicksort's best case, and merge sort's best case takes about half as many iterations as
the worst case.
Merge sort is a stable sort and is more efficient at handling slow-to-access sequential media.
Merge sort is often the best choice for sorting a linked list. The slow random-access
performance of a linked list makes some other algorithms (such as quicksort) perform poorly,
and others (such as heapsort) completely impossible.