🧮 Sorting Problems


Welcome to the Sorting section of JDCodebase!

Sorting is a core concept in computer science and often appears in real-world tasks like data processing, searching, and optimization. Understanding different sorting techniques helps in writing efficient and elegant code.

🔧 Basic Approach

- Choose the right sorting technique based on time and space needs.
- Sort the array in-place if possible, unless stability or extra copies are required.
- Combine sorting with other techniques like binary search, two pointers, or greedy logic.

📚 Common Sorting Techniques

  1. Bubble Sort: Repeatedly swaps adjacent elements. Easy to understand but slow (O(n²)).
  2. Selection Sort: Repeatedly selects the minimum and moves it to the front. Also O(n²).
  3. Insertion Sort: Builds the final sorted array one element at a time. Great for small or nearly sorted arrays.
  4. Merge Sort: Divide and conquer approach that splits the array and merges it back sorted. Stable and guaranteed O(n log n).
  5. Quick Sort: Picks a pivot, partitions the array around it. Fast in practice (avg. O(n log n)), but worst-case O(n²).
  6. Counting Sort: Non-comparison sort. Best for sorting integers in a limited range in O(n + k) time.
  7. Built-in JavaScript Sort: Use .sort((a, b) => a - b) for numeric sort. Behind the scenes, it uses quicksort (V8 engine).

📝 Problems List

  1. Squares of a Sorted Array (#977)
  2. Merge Sorted Array (#88)
  3. Sort Colors (#75)
  4. Sort Array by Parity (#905)
  5. Longest Harmonious Subsequence (#594)