Sorting Algorithms Explained: In-Place, Comparison-Based, and Time Complexity

AlgorithmIn-Place SortComparison-BasedAverage PerformanceTime Complexity
Selection SortYYO(n²)O(n²)
Bubble SortYYDepends on input stateO(n²)
Insertion SortYYSensitive to input orderO(n²)
Shell SortYYVaries depending on gap sequence usedO(n²)
Quick SortYYO(n log n)Worst: O(n²), Best/Average: O(n log n)
Merge SortNYO(n log n)Worst/Best/Average: O(n log n)
Heap SortYYO(n log n)Worst/Best/Average: O(n log n)
Counting SortNNO(n + k) (k is input range)O(n + k)
Radix SortNNO(dn) (d is number of digits), O(n) (if d is constant)O(dn), O(n) (if d is constant)
Bucket SortNNO(n) (if data is uniformly distributed)O(n) (if data is uniformly distributed)

In-Place Sort

  • Refers to sorting algorithms that use only a constant amount of extra space beyond the input array.
  • Does not require additional storage proportional to the size of the input data (n).
  • Quick Sort, Selection Sort, Bubble Sort, Insertion Sort, Shell Sort, and Heap Sort fall into this category.
  • Merge Sort, Counting Sort, Radix Sort, and Bucket Sort are not in-place because they use extra space.

Comparison-Based Sort

  • Sorting algorithms that determine the order of elements based on comparisons between key values.
  • Includes Selection, Bubble, Insertion, Shell, Quick, Merge, and Heap Sort.
  • Counting, Radix, and Bucket Sort use distribution-based logic and are not comparison-based.

Time Complexity

  • A measure of algorithm efficiency, describing how execution time increases with input size (n).
  • Typically described using worst-case performance.
  • Some algorithms like Quick Sort and Merge Sort have different time complexities for best, average, and worst cases.

Average Performance

  • Expected performance over all possible inputs.
  • For large input sizes, algorithms with better time complexity are generally more efficient.

post-title-1747726208530