Bird
Raised Fist0
DSA Pythonprogramming~10 mins

Sorting Stability and When to Use Which Sort in DSA Python - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to sort the list while keeping the original order of equal elements.

DSA Python
sorted_list = sorted(data, key=lambda x: x.age, [1]=True)
Drag options to blanks, or click blank then click option'
Astable_sort
Bstable
Creverse_order
Dreverse
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent parameter like 'stable' or 'stable_sort'.
Confusing 'reverse' with sorting stability.
2fill in blank
medium

Complete the code to perform a stable sort by the 'score' attribute.

DSA Python
data.sort(key=lambda x: x.score, [1]=False)
Drag options to blanks, or click blank then click option'
Areverse
Bstable
Cinplace
Dorder
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use a 'stable' parameter which does not exist.
Setting 'reverse' to True when ascending order is needed.
3fill in blank
hard

Fix the error in the code to ensure a stable sort by 'name'.

DSA Python
sorted_data = sorted(data, key=lambda x: x.name, [1]=False)
Drag options to blanks, or click blank then click option'
Ainplace
Breverse
Corder
Dstable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stable' which is not a valid parameter.
Confusing sorting order with stability.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

DSA Python
{word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.

DSA Python
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys instead of uppercase keys.
Using '<' instead of '>' in the condition.
Using k instead of v for values.

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 unchanged.
B. It sorts the elements in ascending order only.
C. It uses less memory than unstable sorts.
D. It always sorts in place without extra space.

Solution

  1. Step 1: Understand stability in sorting

    A stable sort keeps elements with the same value in the same order as they appeared originally.
  2. Step 2: Compare options with definition

    Only It keeps the original order of equal elements unchanged. describes this property correctly. Other options describe different concepts.
  3. Final Answer:

    It keeps the original order of equal elements unchanged. -> Option A
  4. Quick Check:

    Stable sort = keeps order of equal elements [OK]
Hint: Stable means equal items keep their original order [OK]
Common Mistakes:
  • Confusing stability with sorting order
  • Thinking stability means less memory use
  • Assuming all sorts are stable
2. Which of the following Python sorting methods is stable by default?
easy
A. sorted() function
B. list.sort() method
C. Neither sorted() nor list.sort()
D. Both sorted() and list.sort()

Solution

  1. Step 1: Recall Python sorting stability

    Both sorted() and list.sort() use Timsort, which is stable.
  2. Step 2: Check options

    Options A and B are each correct alone, but Both sorted() and list.sort() correctly states both are stable.
  3. Final Answer:

    Both sorted() and list.sort() -> Option D
  4. Quick Check:

    Python sorts are stable by default [OK]
Hint: Python's built-in sorts are stable by default [OK]
Common Mistakes:
  • Thinking only one method is stable
  • Confusing stability with sorting order
  • Assuming custom sorts are stable by default
3. Consider this list of tuples representing (name, score):
data = [("Alice", 90), ("Bob", 90), ("Charlie", 85)]
What will be the output after sorting by score using sorted(data, key=lambda x: x[1])?
medium
A. [('Charlie', 85), ('Bob', 90), ('Alice', 90)]
B. [('Alice', 90), ('Bob', 90), ('Charlie', 85)]
C. [('Charlie', 85), ('Alice', 90), ('Bob', 90)]
D. [('Bob', 90), ('Alice', 90), ('Charlie', 85)]

Solution

  1. Step 1: Understand sorting by score

    The sort uses the second item (score) as key, sorting ascending: 85, 90, 90.
  2. Step 2: Check stability for equal scores

    Since Python's sort is stable, 'Alice' and 'Bob' keep their original order for score 90.
  3. Final Answer:

    [('Charlie', 85), ('Alice', 90), ('Bob', 90)] -> Option C
  4. Quick Check:

    Stable sort keeps 'Alice' before 'Bob' [OK]
Hint: Stable sort keeps original order for equal keys [OK]
Common Mistakes:
  • Swapping order of equal elements
  • Sorting descending by mistake
  • Ignoring stability property
4. You wrote this code to sort a list of dictionaries by 'age':
people = [{'name': 'Ann', 'age': 25}, {'name': 'Ben', 'age': 20}, {'name': 'Cara', 'age': 25}]
people.sort(key=lambda x: x['age'])
print(people)

But the output does not keep the original order of people with the same age. What is the likely cause?
medium
A. The key function is incorrect.
B. The dictionaries are unordered, causing instability.
C. The list.sort() method is stable, so no cause.
D. The sort method used is unstable.

Solution

  1. Step 1: Recall Python sort stability

    Python's list.sort() is stable, so it keeps order of equal keys.
  2. Step 2: Understand dictionary order

    Before Python 3.7, dictionaries did not preserve insertion order, so printing may show different order.
  3. Step 3: Identify cause

    If using older Python or unordered dicts, the printed order may seem unstable even if sort is stable.
  4. Final Answer:

    The dictionaries are unordered, causing instability. -> Option B
  5. Quick Check:

    Unordered dicts can hide stable sort [OK]
Hint: Unordered dicts can hide stable sort effects [OK]
Common Mistakes:
  • Blaming sort method instead of dict order
  • Changing key function unnecessarily
  • Ignoring Python version differences
5. You have a list of student records with (name, grade, age). You want to sort by grade ascending, then by age descending, but keep the original order for students with same grade and age. Which sorting approach is best?
hard
A. Use a stable sort twice: first by age descending, then by grade ascending.
B. Use an unstable sort once with a combined key (grade, -age).
C. Use a stable sort once with key (grade, age).
D. Sort by age ascending, then by grade descending using stable sorts.

Solution

  1. Step 1: Understand multi-level sorting with stability

    To sort by multiple keys and keep original order for ties, use stable sorts starting from the last key.
  2. Step 2: Apply sorting order

    First sort by age descending (second key), then by grade ascending (first key), both stable.
  3. Step 3: Check options

    Use a stable sort twice: first by age descending, then by grade ascending. matches this approach. Others either use unstable sort or wrong key order.
  4. Final Answer:

    Use a stable sort twice: first by age descending, then by grade ascending. -> Option A
  5. Quick Check:

    Stable sorts chained preserve multi-key order [OK]
Hint: Sort by last key first with stable sort, then first key [OK]
Common Mistakes:
  • Sorting keys in wrong order
  • Using unstable sort for multi-key sorting
  • Ignoring descending order for age
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard