📖 Scenario: Imagine you work in a library system. You have a list of books, each with a title and a year of publication. You want to sort the books by their year but keep the original order of books with the same year. This is called a stable sort.Later, you want to sort the books by title quickly, where stability is not important.
🎯 Goal: You will create an array of books, then sort it by year using a stable sorting method, and then sort it by title using a faster but unstable sorting method. You will print the list after each sort to see the difference.
📋 What You'll Learn
Create an array of 5 books with exact titles and years
Create a stable sort function to sort books by year
Create an unstable sort function to sort books by title
Print the books after each sort showing their title and year
💡 Why This Matters
🌍 Real World
Sorting books or records by different fields while preserving order is common in library systems, databases, and spreadsheets.
💼 Career
Understanding sorting stability helps in choosing the right sorting algorithm for data processing, reporting, and UI display tasks.
Progress0 / 4 steps
1
Create the array of books
Create a struct called Book with title (string) and year (int). Then create an array called books with these 5 books exactly: {"C Programming", 1978}, {"Algorithms", 1990}, {"Data Structures", 1990}, {"Operating Systems", 1985}, {"Networking", 1990}.
DSA C
Hint
Use typedef struct to define Book. Use an array of 5 Book elements with exact titles and years.
2
Add a stable sort function to sort by year
Add a function called stableSortByYear that uses insertion sort to sort the books array by year in ascending order. This sort must be stable, so books with the same year keep their original order.
DSA C
Hint
Use insertion sort logic to keep the order of equal years stable.
3
Add an unstable sort function to sort by title
Add a function called unstableSortByTitle that uses selection sort to sort the books array by title in ascending alphabetical order. This sort is unstable, so books with the same title order may change.
DSA C
Hint
Use selection sort with strcmp to compare titles.
4
Print the books after each sort
Write a printBooks function that prints each book's title and year in the format Title - Year. Then in main, first call stableSortByYear on books and print the list, then call unstableSortByTitle on books and print the list again.
DSA C
Hint
Print the books after sorting by year, then print again after sorting by title. Use printf with format "%s - %d\n".
Practice
(1/5)
1. What does it mean when a sorting algorithm is called stable?
easy
A. It sorts elements in descending order
B. It uses less memory than unstable sorts
C. It keeps the original order of equal elements after sorting
D. It always sorts in linear time
Solution
Step 1: Understand stability in sorting
A stable sort keeps the relative order of elements that compare equal unchanged.
Step 2: Compare options with definition
Only It keeps the original order of equal elements after sorting matches this definition exactly.
Final Answer:
It keeps the original order of equal elements after sorting -> Option C
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 faster sorting
Assuming stability relates to memory use
2. Which of the following is the correct syntax to call the qsort function in C for sorting an array arr of integers with n elements?
easy
A. qsort(arr, n, compare, sizeof(int));
B. qsort(arr, n, sizeof(int), compare);
C. qsort(arr, sizeof(int), n, compare);
D. qsort(arr, compare, n, sizeof(int));
Solution
Step 1: Recall qsort parameter order
The qsort function parameters are: pointer to array, number of elements, size of each element, and comparison function pointer.
Step 2: Match parameters with options
qsort(arr, n, sizeof(int), compare); matches this order exactly: arr, n, sizeof(int), compare.
Final Answer:
qsort(arr, n, sizeof(int), compare); -> Option B
Quick Check:
qsort syntax = arr, n, size, compare [OK]
Hint: Remember qsort: array, count, size, compare function [OK]
Common Mistakes:
Mixing up size and count parameters
Placing compare function in wrong position
Using wrong order of arguments
3. Consider the following C code snippet using bubble sort on an array of structs with an integer key and a character value. Which output shows the array after sorting by key, preserving stability?
typedef struct {
int key;
char value;
} Item;
void bubbleSort(Item arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j].key > arr[j+1].key) {
Item temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
Item arr[5] = {{2, 'a'}, {3, 'b'}, {2, 'c'}, {1, 'd'}, {3, 'e'}};
bubbleSort(arr, 5);
for (int i = 0; i < 5; i++) {
printf("%d%c ", arr[i].key, arr[i].value);
}
return 0;
}
medium
A. 1d 2a 2c 3b 3e
B. 1d 2c 2a 3e 3b
C. 1d 2c 2a 3b 3e
D. 1d 2a 2c 3e 3b
Solution
Step 1: Understand bubble sort stability
Bubble sort is stable, so equal keys keep their original order.
Step 2: Trace sorting of array
Original order for key=2 is 'a' then 'c', for key=3 is 'b' then 'e'. After sorting by key, order is 1d, 2a, 2c, 3b, 3e.
Final Answer:
1d 2a 2c 3b 3e -> Option A
Quick Check:
Bubble sort stable = keeps order of equal keys [OK]
Hint: Stable sort keeps original order of equal keys [OK]
Common Mistakes:
Assuming bubble sort is unstable
Swapping equal keys and changing order
Mixing up output order of equal keys
4. The following code attempts to sort an array using selection sort but does not preserve stability. What change fixes this to make it stable?
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
medium
A. Sort array in descending order
B. Use bubble sort instead of selection sort
C. Change comparison to <= instead of <
D. Replace swap with shifting elements to insert min element
Solution
Step 1: Identify why selection sort is unstable
Selection sort swaps min element directly, which can reorder equal elements and break stability.
Step 2: Fix by shifting elements
Instead of swapping, shift elements right and insert min element at correct position to keep order.
Final Answer:
Replace swap with shifting elements to insert min element -> Option D
Quick Check:
Stable selection sort = shift, not swap [OK]
Hint: Shift elements instead of swap for stable selection sort [OK]
Common Mistakes:
Just changing comparison operator
Switching to bubble sort without reason
Sorting in wrong order
5. You have a large dataset with many duplicate keys and need to sort it by key while preserving the order of duplicates. The dataset is too large to fit in memory, so you must use external sorting. Which sorting algorithm is best suited for this task?
hard
A. External merge sort because it is stable and handles large data
B. Quick sort because it is fastest in average case
C. Heap sort because it uses constant memory
D. Selection sort because it is simple to implement
Solution
Step 1: Understand requirements for large data and stability
Large data needs external sorting; stability is required to keep duplicate order.
Step 2: Match algorithm features
External merge sort is stable and designed for large data using disk; quicksort and heapsort are unstable and in-memory; selection sort is inefficient.
Final Answer:
External merge sort because it is stable and handles large data -> Option A
Quick Check:
Large stable sort = external merge sort [OK]
Hint: Use external merge sort for large stable sorting [OK]
Common Mistakes:
Choosing quicksort ignoring stability
Picking heapsort without external support
Using simple sorts for large data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.