MCS-208 · Unit 9

Searching and Sorting Techniques

Section 8 of 9

Block 3 · Unit 9Searching and Sorting Techniques7 min · Section 8 of 9

9.8Sorting on Several Keys

Covers IGNOU MCS-208 §9.6

Real records are often sorted by more than one field: students by class, then by roll number; cards by suit, then by face value. The first key is the primary (most significant) key; ties are broken by the secondary key. There are two general strategies.

MSD and LSD

  • MSD (most significant digit/key first): sort by the primary key into piles, then sort each pile separately by the secondary key, and so on.
  • LSD (least significant digit/key first): sort the whole file by the least significant key, then by the next key, …, and finally by the primary key, using a stable sort for every pass except the first.

“Digit” here means “key”. MSD and LSD only decide the order of the passes; each pass may use any suitable sorting method (LSD needs a stable one).

MSDLSD
First pass sorts byPrimary keyLeast significant key
Later passesEach pile separatelyThe whole file
Stability requiredNoYes (for every pass after the first)
Typical useSorting by a few keys, recursive splittingRadix sort of numbers digit by digit, punched-card sorters

Radix sort is the LSD method applied to the digits of numbers: sort by units digit, then tens, then hundreds, each time with a stable distribution into 10 piles. For nn numbers of dd digits it takes O(d(n+10))O(d(n + 10)) time, without any key comparisons.

Key takeaways

  • Primary key decides the main order; secondary keys break ties.
  • MSD: sort by the primary key into piles, then sort each pile.
  • LSD: sort by the least significant key first, the primary key last, with stable sorts.
  • Radix sort is LSD on digits: O(d(n+b))O(d(n + b)) for base bb.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In the LSD method for sorting students by class and then roll number, which key is sorted first?

Show answer

Answer: B. Roll number

LSD starts with the least significant key (roll number) and ends with the primary key (class).

Question 2 · multiple choice

Why must the passes of the LSD method (after the first) be stable?

Show answer

Answer: C. To keep the order from earlier passes among records with equal keys in the current pass

Otherwise the last pass would scramble the secondary order inside each group.

Question 3 · multiple choice

Which sort is suitable for the later passes of LSD?

Show answer

Answer: D. Insertion sort or merge sort

Both are stable. Quick sort and heap sort are not.

Question 4 · multiple choice

What does this program print?

C · q9-radix.c
#include <stdio.h>

int main(void)
{
    int a[] = {32, 15, 41, 23, 12}, b[5], n = 5;
    /* one LSD pass: stable distribution by the units digit */
    int k = 0;
    for (int d = 0; d <= 9; d++)
        for (int i = 0; i < n; i++)
            if (a[i] % 10 == d) b[k++] = a[i];
    for (int i = 0; i < n; i++) printf("%d ", b[i]);
    printf("\n");
    return 0;
}
Show answer

Answer: C. 41 32 12 23 15

Units digits 1: 41; 2: 32, 12 (in input order, stable); 3: 23; 5: 15. The last option breaks stability (12 before 32). A second pass on the tens digit would give the fully sorted order 12 15 23 32 41.

Question 5 · multiple choice

In the MSD method, what happens after the first pass?

Show answer

Answer: A. Each pile (group with the same primary key) is sorted separately by the next key.

Piles never mix in MSD, so they can be sorted independently.

Question 6 · short answer

Explain the MSD and LSD methods for sorting a deck of cards by suit and face value.

Show worked answer

MSD: first sort the 52 cards into 4 piles by suit (the most significant key); then sort each pile separately by face value (Ace to King); finally stack the piles in suit order. LSD: first sort all 52 cards into 13 piles by face value (the least significant key) and stack the piles in order; then sort the whole deck into 4 piles by suit using a stable method (e.g. dealing the cards in order onto the suit piles), so that within each suit the face-value order from the first pass is kept; stack the suit piles. Both give the deck ordered by suit and, within suits, by face value; LSD requires the second pass to be stable.

↑ ↓ move↵ openTab switch type