Episode 7 — DSA with JavaScript / 7.9 — Sorting Algorithms

7.9 — Interview Questions: Sorting Algorithms

<< Overview


Q1. What is the difference between stable and unstable sorting?

Answer: Stable sort preserves relative order of equal elements. Merge sort and insertion sort are stable; quick sort and selection sort are not.

Q2. When would you use insertion sort over merge sort?

Answer: For small arrays (<20 elements) or nearly sorted data where insertion sort's O(n) best case applies.

Q3. Explain merge sort's divide and conquer approach.

Answer: Split array in half recursively until single elements, then merge sorted halves back up. O(n log n) guaranteed.

Q4. Why can quick sort be O(n²) in the worst case?

Answer: When pivot selection is poor (always min or max), one partition has n-1 elements. Fix: random pivot or median-of-three.

Q5. What is cyclic sort? When is it applicable?

Answer: Place each element at its correct index. Only works when elements are in range 1..N or 0..N-1. O(n) time.

Q6. How do you find the kth largest element?

Answer: QuickSelect (partition-based): O(n) average. Or use a min-heap of size k: O(n log k).

Q7. What makes merge sort preferred for linked lists?

Answer: No random access needed; merge can be done in O(1) space on linked lists.

Q8. Compare time and space of all major sorting algorithms.

Answer: See comparison table in the chapter. Key: merge sort guarantees O(n log n) but needs O(n) space; quick sort is O(n log n) average with O(log n) space.

Q9. What is counting sort?

Answer: Count occurrences of each value, then reconstruct. O(n+k) where k is range. Only for integers with small range.

Q10. How does radix sort work?

Answer: Sort by each digit from least significant to most significant using a stable sort (counting sort). O(d × (n+k)).

Q11. When to use which sort?

SituationBest sort
General purposeQuick sort / merge sort
Nearly sortedInsertion sort
Linked listMerge sort
Integers in rangeCounting / radix sort
Elements 1-NCyclic sort
Memory constrainedQuick sort / heap sort
Stability requiredMerge sort / insertion sort

Q12. Explain Dutch National Flag algorithm.

Answer: 3-pointer partition of 0s, 1s, 2s in single O(n) pass. Used in 3-way quicksort.

Q13. How to sort in O(n) for specific inputs?

Answer: Counting sort (small range), radix sort (fixed digits), cyclic sort (1..N range).

Q14. What is external sorting?

Answer: For data too large for memory. Split into chunks, sort each in memory, merge chunks from disk.


Quick-fire table

#QuestionAnswer
1Merge sort time?O(n log n) always
2Quick sort worst?O(n²) with bad pivot
3Insertion sort best?O(n) — nearly sorted
4Cyclic sort time?O(n) for 1..N range
5Stable sorts?Merge, insertion, counting
6Unstable sorts?Quick, selection, heap
7Merge sort space?O(n)
8Quick sort space?O(log n) stack
9Counting sort time?O(n+k)
10Best general sort?Quick sort (avg O(n log n))

Rapid self-check cards

SC-001

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-002

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-003

  • Q: Is merge sort stable?
  • A: Yes

SC-004

  • Q: Is quick sort stable?
  • A: No

SC-005

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-006

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-007

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-008

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-009

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-010

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-011

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-012

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-013

  • Q: Is merge sort stable?
  • A: Yes

SC-014

  • Q: Is quick sort stable?
  • A: No

SC-015

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-016

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-017

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-018

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-019

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-020

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-021

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-022

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-023

  • Q: Is merge sort stable?
  • A: Yes

SC-024

  • Q: Is quick sort stable?
  • A: No

SC-025

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-026

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-027

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-028

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-029

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-030

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-031

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-032

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-033

  • Q: Is merge sort stable?
  • A: Yes

SC-034

  • Q: Is quick sort stable?
  • A: No

SC-035

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-036

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-037

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-038

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-039

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-040

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-041

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-042

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-043

  • Q: Is merge sort stable?
  • A: Yes

SC-044

  • Q: Is quick sort stable?
  • A: No

SC-045

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-046

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-047

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-048

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-049

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-050

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-051

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-052

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-053

  • Q: Is merge sort stable?
  • A: Yes

SC-054

  • Q: Is quick sort stable?
  • A: No

SC-055

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-056

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-057

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-058

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-059

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-060

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-061

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-062

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-063

  • Q: Is merge sort stable?
  • A: Yes

SC-064

  • Q: Is quick sort stable?
  • A: No

SC-065

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-066

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-067

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-068

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-069

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-070

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-071

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-072

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-073

  • Q: Is merge sort stable?
  • A: Yes

SC-074

  • Q: Is quick sort stable?
  • A: No

SC-075

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-076

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-077

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-078

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-079

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-080

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-081

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-082

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-083

  • Q: Is merge sort stable?
  • A: Yes

SC-084

  • Q: Is quick sort stable?
  • A: No

SC-085

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-086

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-087

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-088

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-089

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-090

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-091

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-092

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-093

  • Q: Is merge sort stable?
  • A: Yes

SC-094

  • Q: Is quick sort stable?
  • A: No

SC-095

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-096

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-097

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-098

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-099

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-100

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-101

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-102

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-103

  • Q: Is merge sort stable?
  • A: Yes

SC-104

  • Q: Is quick sort stable?
  • A: No

SC-105

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-106

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-107

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-108

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-109

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-110

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-111

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-112

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-113

  • Q: Is merge sort stable?
  • A: Yes

SC-114

  • Q: Is quick sort stable?
  • A: No

SC-115

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-116

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-117

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-118

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-119

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-120

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-121

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-122

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-123

  • Q: Is merge sort stable?
  • A: Yes

SC-124

  • Q: Is quick sort stable?
  • A: No

SC-125

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-126

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-127

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-128

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-129

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-130

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-131

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-132

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-133

  • Q: Is merge sort stable?
  • A: Yes

SC-134

  • Q: Is quick sort stable?
  • A: No

SC-135

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-136

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-137

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-138

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-139

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-140

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-141

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-142

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-143

  • Q: Is merge sort stable?
  • A: Yes

SC-144

  • Q: Is quick sort stable?
  • A: No

SC-145

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-146

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-147

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-148

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-149

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-150

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-151

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-152

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-153

  • Q: Is merge sort stable?
  • A: Yes

SC-154

  • Q: Is quick sort stable?
  • A: No

SC-155

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-156

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-157

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-158

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-159

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-160

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-161

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-162

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-163

  • Q: Is merge sort stable?
  • A: Yes

SC-164

  • Q: Is quick sort stable?
  • A: No

SC-165

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-166

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-167

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-168

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-169

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-170

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-171

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-172

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-173

  • Q: Is merge sort stable?
  • A: Yes

SC-174

  • Q: Is quick sort stable?
  • A: No

SC-175

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-176

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-177

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-178

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-179

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-180

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-181

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-182

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-183

  • Q: Is merge sort stable?
  • A: Yes

SC-184

  • Q: Is quick sort stable?
  • A: No

SC-185

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-186

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-187

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-188

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-189

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-190

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data

SC-191

  • Q: Merge sort complexity?
  • A: O(n log n) time, O(n) space

SC-192

  • Q: Quick sort worst case?
  • A: O(n²) with bad pivot choice

SC-193

  • Q: Is merge sort stable?
  • A: Yes

SC-194

  • Q: Is quick sort stable?
  • A: No

SC-195

  • Q: Cyclic sort range?
  • A: Only for elements 1..N or 0..N-1

SC-196

  • Q: Insertion sort best case?
  • A: O(n) — already sorted

SC-197

  • Q: Selection sort swaps?
  • A: O(n) — one swap per pass

SC-198

  • Q: What is QuickSelect?
  • A: Find kth element in O(n) average

SC-199

  • Q: Counting sort constraint?
  • A: Need small integer range

SC-200

  • Q: When use insertion sort?
  • A: Small arrays or nearly sorted data