An Overview of Sorting Algorithms

Sorting Algorithms Time Complexity Chart

Comparison Table

AlgorithmBest CaseAverage CaseWorst CaseSpace ComplexityStable?In-Place?
Quick SortO(n log n)O(n log n)O(n²)O(log n)NoYes
Merge SortO(n log n)O(n log n)O(n log n)O(n)YesNo
Heap SortO(n log n)O(n log n)O(n log n)O(1)NoYes
Insertion SortO(n)O(n²)O(n²)O(1)YesYes
Selection SortO(n²)O(n²)O(n²)O(1)NoYes
Bubble SortO(n)O(n²)O(n²)O(1)YesYes
Shell SortO(n log n)O(n log² n)O(n log² n)O(1)NoYes
Counting SortO(n + k)O(n + k)O(n + k)O(n + k)YesNo
Radix SortO(nk)O(nk)O(nk)O(n + k)YesNo
Bucket SortO(n + k)O(n + k)O(n²)O(n + k)YesNo
Tim SortO(n)O(n log n)O(n log n)O(n)YesNo
Tree SortO(n log n)O(n log n)O(n²)O(n)YesNo

Visual Comparison


Time Complexity Comparison:
────────────────────────────────────────────────────────────────────
Algorithm        Best        Average       Worst        Space
────────────────────────────────────────────────────────────────────
Quick Sort      n log n     n log n       n²           log n
Merge Sort      n log n     n log n       n log n      n
Heap Sort       n log n     n log n       n log n      1
Insertion Sort  n           n²            n²           1
Selection Sort  n²          n²            n²           1
Bubble Sort     n           n²            n²           1
Shell Sort      n log n     n log² n      n log² n     1
Counting Sort   n + k       n + k         n + k        n + k
Radix Sort      nk          nk            nk           n + k
Bucket Sort     n + k       n + k         n²           n + k
Tim Sort        n           n log n       n log n      n
Tree Sort       n log n     n log n       n²           n
────────────────────────────────────────────────────────────────────

Detailed Explanation

1. Comparison-Based Sorts

AlgorithmBest Use CaseKey Characteristics
Quick SortLarge datasets, general purposeFast average case, cache friendly
Merge SortLarge datasets, stable requiredConsistent O(n log n), good for linked lists
Heap SortWhen worst-case mattersGuaranteed O(n log n), in-place
Insertion SortSmall arrays, nearly sortedAdaptive, simple implementation
Selection SortSmall arraysMinimizes swaps, simple
Bubble SortEducational purposesSimple but inefficient
Shell SortMedium-sized arraysImproved insertion sort

2. Non-Comparison Sorts

AlgorithmRequirementsBest For
Counting SortInteger keys in small rangeSmall integer ranges
Radix SortFixed-size keysNumbers, strings with fixed length
Bucket SortUniformly distributed dataFloating-point numbers

3. Hybrid Sorts

AlgorithmCompositionUse Case
Tim SortMerge + InsertionPython's built-in sort, Java Arrays.sort()
Intro SortQuick + HeapC++ std::sort

Memory Usage Comparison


Memory Usage (from lowest to highest):
1. Heap Sort, Insertion Sort, Selection Sort, Bubble Sort, Shell Sort → O(1)
2. Quick Sort → O(log n) [recursion stack]
3. Tree Sort → O(n)
4. Merge Sort, Tim Sort → O(n)
5. Counting Sort, Radix Sort, Bucket Sort → O(n + k)

Stability Analysis

Stable Algorithms (preserve original order of equal elements):

  • Insertion Sort

  • Bubble Sort

  • Merge Sort

  • Counting Sort

  • Radix Sort

  • Bucket Sort

  • Tim Sort

  • Tree Sort

Unstable Algorithms:

  • Quick Sort

  • Heap Sort

  • Selection Sort

  • Shell Sort

Practical Recommendations

For Small Arrays (n < 50):

  • Insertion Sort - Simple and efficient for small data

For Medium Arrays (50 < n < 1000):

  • Shell Sort - Good balance of simplicity and efficiency

  • Quick Sort - With good pivot selection

For Large Arrays (n > 1000):

  • Quick Sort - Generally fastest in practice

  • Merge Sort - When stability is required

  • Heap Sort - When worst-case performance is critical

For Special Cases:

  • Nearly Sorted Data: Insertion Sort (O(n))

  • Small Integer Range: Counting Sort (O(n + k))

  • Fixed-length keys: Radix Sort (O(nk))

  • Uniformly distributed: Bucket Sort (O(n + k))

Real-world Usage

  • C++ std::sort(): IntroSort (Quick + Heap + Insertion)

  • Java Arrays.sort(): TimSort (Merge + Insertion)

  • Python sorted(): TimSort

  • JavaScript Array.sort(): TimSort or QuickSort (implementation-dependent)

Core i3 vs i5 vs i7

  The actual difference between Intel's Core i3, i5, i7, and i9 processors lies in their performance, core/thread count, clock speeds, cache size, and features like Turbo Boost, Hyper-Threading, and overclocking support.  

Here’s a detailed breakdown of how they compare (as of 2024, covering up to 14th Gen Intel CPUs):

 1. Core & Thread Count (Higher Tier = More Cores/Threads)


- P = Performance cores (faster, handle heavy tasks)  

- E = Efficiency cores (handle background tasks, power-efficient)  

- Older gens (pre-12th Gen) had only P-cores.  


 2. Clock Speeds & Turbo Boost (Higher Tier = Faster)

- i3: Lower base clocks, modest Turbo Boost.  

- i5: Balanced clocks, good Turbo Boost.  

- i7: Higher base & boost clocks.  

- i9: Highest clocks, aggressive Turbo Boost (e.g., up to 5.8 GHz on i9-14900K).  


Example (Desktop 14th Gen):  

- i3-14100 → 3.5 GHz (4.7 GHz Turbo)  

- i5-14600K → 3.5 GHz (5.3 GHz Turbo)  

- i7-14700K → 3.4 GHz (5.6 GHz Turbo)  

- i9-14900K → 3.2 GHz (5.8 GHz Turbo)  


 3. Cache Size (More Cache = Better Performance)

- i3: 8-12MB L3 cache  

- i5: 12-24MB L3 cache  

- i7: 20-30MB L3 cache  

- i9: 24-36MB L3 cache  

*(Larger cache helps with gaming and multitasking.)*  


 4. Features & Technologies

 

5. Use Case Recommendations

- Core i3: Basic tasks (web browsing, office work, light gaming).  

- Core i5: Best value (gaming, productivity, light content creation).  

- Core i7: High-end gaming, video editing, 3D rendering.  

- Core i9: Extreme performance (4K video editing, AAA gaming, heavy workloads).  


 6. Generational Differences (Important!)

- 12th Gen (Alder Lake) & newer use Hybrid Architecture (P + E cores).  

- 10th/11th Gen had only P-cores (no E-cores).  

- Laptop vs. Desktop: Laptop CPUs (U/H-series) have fewer cores but optimized for efficiency.  


 Summary Table (Desktop 14th Gen Example)




The main differences between i3, i5, i7, and i9 are:  

✅ Core/Thread count (i9 > i7 > i5 > i3)  

✅ Clock speeds & Turbo Boost (i9 boosts highest)  

✅ Cache size (i9 has the most L3 cache)  

✅ Features (i7/i9 get better overclocking, PCIe, etc.)  


For most users:  

- Gaming? → i5 or i7.  

- Productivity? → i7 or i9.  

- Budget build? → i3 or i5.