What does it mean for a sorting algorithm to be stable?
A stable sorting algorithm keeps the original order of equal elements the same after sorting. For example, if two items have the same value, they stay in the order they appeared before sorting.
Click to reveal answer
beginner
Name a common stable sorting algorithm.
Merge Sort is a common stable sorting algorithm. It keeps equal elements in their original order while sorting.
Click to reveal answer
intermediate
Why might you choose an unstable sort like Quick Sort over a stable sort?
Quick Sort is often faster and uses less memory than stable sorts. If you don't need to keep the order of equal elements, Quick Sort is a good choice for speed.
Click to reveal answer
intermediate
When is it important to use a stable sorting algorithm?
Use a stable sort when you want to sort by multiple criteria step-by-step. For example, first sort by age, then by name. Stability keeps the previous order intact.
Click to reveal answer
intermediate
What is a key difference between Merge Sort and Heap Sort regarding stability?
Merge Sort is stable, meaning it keeps equal elements in order. Heap Sort is unstable, so equal elements might change order after sorting.
Click to reveal answer
Which sorting algorithm is stable by default?
AMerge Sort
BQuick Sort
CHeap Sort
DSelection Sort
✗ Incorrect
Merge Sort keeps the order of equal elements, so it is stable. Quick Sort, Heap Sort, and Selection Sort are generally unstable.
Why choose a stable sort when sorting by multiple keys?
ATo keep the order of equal elements from previous sorts
BBecause stable sorts are always faster
CTo use less memory
DBecause unstable sorts can't sort multiple keys
✗ Incorrect
Stable sorts keep the order of equal elements, which helps when sorting by multiple keys step-by-step.
Which sorting algorithm is usually fastest but unstable?
ABubble Sort
BQuick Sort
CInsertion Sort
DMerge Sort
✗ Incorrect
Quick Sort is often the fastest in practice but is unstable.
If you want to sort a large list with limited memory and don't care about stability, which sort is best?
ACounting Sort
BMerge Sort
CBubble Sort
DQuick Sort
✗ Incorrect
Quick Sort uses less memory than Merge Sort and is fast, making it good for large lists when stability is not needed.
Which sorting algorithm is stable and good for linked lists?
AHeap Sort
BQuick Sort
CMerge Sort
DSelection Sort
✗ Incorrect
Merge Sort is stable and works well with linked lists because it doesn't require random access.
Explain what sorting stability means and why it matters when sorting by multiple criteria.
Think about how sorting twice by different keys keeps the order.
You got /3 concepts.
Describe when you would choose Quick Sort over Merge Sort and the trade-offs involved.
Consider performance and stability needs.
You got /4 concepts.
Practice
(1/5)
1. What does it mean when a sorting algorithm is called stable?
easy
A. It keeps the original order of equal elements after sorting
B. It sorts elements faster than any other algorithm
C. It uses less memory than unstable sorts
D. It only works on numbers, not strings
Solution
Step 1: Understand stability in sorting
A stable sort keeps the relative order of items that compare equal the same as before sorting.
Step 2: Compare options
Only It keeps the original order of equal elements after sorting describes this property correctly. Other options describe speed, memory, or data type restrictions, which are unrelated to stability.
Final Answer:
It keeps the original order of equal elements after sorting -> Option A
Quick Check:
Stable sort = keeps equal items order [OK]
Hint: Stable means equal items keep their original order [OK]
Common Mistakes:
Confusing stability with speed
Thinking stability means less memory use
Believing stability depends on data type
2. Which of the following Go code snippets correctly uses the sort.SliceStable function to sort a slice of structs by an integer field Age?
Hint: Use SliceStable with less-than for ascending stable sort [OK]
Common Mistakes:
Using sort.Slice instead of sort.SliceStable
Reversing comparison for ascending order
Mixing stable and unstable sort functions
3. Given the following Go code, what will be the output after sorting?
type Item struct {
Name string
Rank int
}
items := []Item{{"A", 2}, {"B", 1}, {"C", 2}}
sort.Slice(items, func(i, j int) bool { return items[i].Rank < items[j].Rank })
for _, item := range items {
fmt.Print(item.Name, " ")
}
medium
A. A C B
B. B A C
C. B C A
D. C A B
Solution
Step 1: Understand sorting criteria
The slice is sorted by Rank ascending using sort.Slice, which is unstable.
Step 2: Analyze order of equal Rank elements
Items with Rank 2 are "A" and "C". Since sort.Slice is unstable, their relative order may change. The output shows "B" first (Rank 1), then "C", then "A".
A. Using sort.Slice instead of sort.SliceStable causes unstable sort
B. The comparison function should use > instead of <
C. The slice must be converted to []struct before sorting
D. The code is correct and stable by default
Solution
Step 1: Identify sorting function used
The code uses sort.Slice, which is unstable.
Step 2: Understand stability requirement
To sort stably by length, sort.SliceStable must be used instead.
Final Answer:
Using sort.Slice instead of sort.SliceStable causes unstable sort -> Option A
Quick Check:
Stable sort requires SliceStable, not Slice [OK]
Hint: Use SliceStable for stable sorting, not Slice [OK]
Common Mistakes:
Thinking comparison direction affects stability
Believing slice type conversion is needed
Assuming sort.Slice is stable
5. You have a list of employees with fields Name and Department. You want to sort them first by Department alphabetically and then by Name alphabetically within each department, preserving the order of employees with the same name. Which sorting approach is best in Go?
hard
A. Use one unstable sort by Department only
B. Use two stable sorts: first by Name, then by Department
C. Use one stable sort by concatenated Department+Name string
D. Use two unstable sorts: first by Department, then by Name
Solution
Step 1: Understand multi-level sorting with stability
To sort by Department then Name, stable sorts allow sorting by the secondary key first, then primary key.
Step 2: Apply stable sorts in correct order
Sort first by Name (secondary key) stably, then by Department (primary key) stably. This preserves Name order within Departments.
Final Answer:
Use two stable sorts: first by Name, then by Department -> Option B
Quick Check:
Two stable sorts in reverse order = correct multi-level sort [OK]
Hint: Sort by secondary key first, then primary key with stable sorts [OK]
Common Mistakes:
Using unstable sorts causing order loss
Sorting primary key first losing secondary order
Trying one sort on combined keys without stability
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.