मराठी

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

प्रश्न

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.

टीपा लिहा

उत्तर

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 5: Sorting - Exercise [पृष्ठ ८०]

APPEARS IN

एनसीईआरटी Computer Science [English] Class 12
पाठ 5 Sorting
Exercise | Q 6. | पृष्ठ ८०
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×