Skip to content
Exercises · Q6

Q.During admission in a course, the names of the students are inserted in ascending order. Thus, performing the sorting operation at the time of inserting elements in a list. Identify the type of sorting technique being used and write a program using a user defined function that is invoked every time a name is input and stores the name in ascending order of names in the list.

Uttarakhand UbseTextbookSubjective· 2mImportance★★★★★
83% · 10/12 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Sorting as the data arrives — placing each new item straight into its correct slot in an already-ordered list — is insertion sort. The function insert_name() below is called on every admission: it shifts every name greater than the new one one position right, then drops the new name into the gap. The list is therefore in ascending order after every single insertion.

Concept understanding — identifying the technique

Look at what the admission office actually does:

  • The names already in the register are sorted.
  • A new student arrives, and that one name has to find its place among them.
  • Nothing else moves out of order; only the names bigger than the newcomer slide one step right.

That is the definition of insertion sort: maintain a sorted prefix, and insert the next element into it. It is the only one of the elementary sorts that is incremental — it can sort data that is still arriving, which is exactly the situation here. Bubble sort and selection sort both need the whole list in hand before they can start.

Important

Insertion sort is the natural choice for an online problem (data arrives one item at a time) and for nearly sorted data, where it costs only O(n).

Algorithm — inserting one name

  1. Append the new name at the end of the list (this reserves a slot).
  2. Set i to the index of the last old name.
  3. While i >= 0 and names[i] is alphabetically greater than the new name: a. Copy names[i] one position to the right. b. Decrease i by 1.
  4. Write the new name at names[i + 1] — the gap left behind.

Comparing with .lower() makes the ordering case-insensitive, so anita and Anita sort alike.

Program

def insert_name(names, new_name):
    '''Insert new_name into 'names', keeping it sorted in ascending order.'''
    names.append(new_name)                       # step 1 - reserve a slot
    i = len(names) - 2                           # step 2 - last old index

    while i >= 0 and names[i].lower() > new_name.lower():
        names[i + 1] = names[i]                  # step 3a - shift right
        i -= 1                                   # step 3b

    names[i + 1] = new_name                      # step 4 - place it
    return names


# ---- driver: called every time a name is input ----
students = []
n = int(input('How many admissions? '))
for k in range(n):
    name = input('Enter name of student: ')
    insert_name(students, name)
    print('Register now:', students)

print('\nFinal admission list (ascending):', students)

Sample run (input names: Rahul, Anita, Zoya, Meera)

How many admissions? 4
Enter name of student: Rahul
Register now: ['Rahul']
Enter name of student: Anita
Register now: ['Anita', 'Rahul']
Enter name of student: Zoya
Register now: ['Anita', 'Rahul', 'Zoya']
Enter name of student: Meera
Register now: ['Anita', 'Meera', 'Rahul', 'Zoya']

Final admission list (ascending): ['Anita', 'Meera', 'Rahul', 'Zoya']

Dry run of the insertion of Meera

List before this call: ['Anita', 'Rahul', 'Zoya']. After append, the working list is ['Anita', 'Rahul', 'Zoya', 'Meera'] and i = 2.

StepiCompare names[i] with MeeraActionList during the shift
12Zoya > Meerashift Zoya rightAnita, Rahul, Zoya, Zoya

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.