A list contains Item_code, Item_name, qty and price. Sort the list :
- In ascending order of qty using Bubble sort.
- In descending order of price using Insertion sort.
Program:-
def bubble_sort(LIST) : #bubble sort function as required in a question
i = 0
j = 0
l = len(LIST)
for i in range(l):
print("Bubble Sort Iterations – Asc order of Quantity")
for j in range(i+l,l):
if LIST[i][3] > LIST[j][3]: # swapping
LIST[i][0], LIST[j][0]=LIST[j][0],LIST[i][0]
LIST[i][1], LIST[j][1]=LIST[j][l],LIST[i][l]
LIST[i][2], LIST[j][2]=LIST[j][2],LIST[i][2]
LIST[i][3], LIST[j][3]=LIST[j][3],LIST[i][3]
print(LIST)
def insertion_sort(LIST): #insertion sort function as required in a question
for K in range (1, len(LIST)):
temp=LIST[K][2]
ptr=K-1
print ("Insertion Sort Iterations – Desc order of price")
while(ptr>=0) and LIST[ptr][2] < temp:
LIST[ptr+1][0] = LIST[ptr][0]
LIST[ptr+1][1] = LIST[ptr][1]
LIST[ptr+1][2] = LIST[ptr][2]
LIST[ptr+1][3] = LIST[ptr][3]
ptr=ptr-1
LIST[ptr+1][2]=temp
print(LIST)
maxrange = int(input("Enter Number of Items: " )) #take all necessary inputs from a user
Items=[]
print()
for i in range (maxrange):
Details=[]
Details. append(input("Enter Item Code: "))
Details.append(input("Enter Item name: "))
Details.append(float(input("Enter price: ")))
Details.append(input("Enter Quantity: "))
Items.append(Details)
print()
print("BEFORE SORTING",Items)
print(bubble_sort(Items))
print(insertion_sort(Items))
Program Images:-
Program Explanation:-
Firstly, user will enter total number of items. Then, add details of the items, means created nested list. Each detail is fetched from a user. Then, list is printed that how items are stored before sorting. Thereby, bubble_sort function calls out which will display the result or can say sort the list according the the requirement of a question. Similarly, for insertion_sort function calls out and print the result according the question.
Comments
Post a Comment