English

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. - Computer Science (Python)

Advertisements
Advertisements

Question

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.

Short Note

Solution

Insertion sort, as this technique allows to insert a value at its position in a sorted list.

lista = [ ]
i = 1
more = 'y'

while more == 'y':
    name = input ("Enter next name : ")
    lista.append(name)
    j = i
    pos = len(lista)
    save = list[pos-1]
    j = pos - 1
    while (j > 0 and lista[j-1] > save):
        lista[j] = lista[j-i]
        j = j - 1
    lista[j] = save
    more = input("Want to enter more ? (y/n) :")
print("final list")
print(lista)
shaalaa.com
Insertion Sort
  Is there an error in this question or solution?
Chapter 5: Sorting - Exercise [Page 80]

APPEARS IN

NCERT Computer Science [English] Class 12
Chapter 5 Sorting
Exercise | Q 6. | Page 80
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×