Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
12.8k views
in Computer by (72.4k points)

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.

1 Answer

0 votes
by (89.5k points)
selected by
 
Best answer

def bubble_sort(DATA_LIST) :

i = 0

j = 0

l = len(DATA_LIST)

for i in range(l):

print “Bubble Sort Iterations – Asc order of Quantity”

for j in range(i+l,l):

if DATA_LlST[i][3] > DATA_LIST[j][3]:

# swapping

DATA_LIST[i][0], DATA_LIST[j] [0]=DATA LIST[j] [0],DATA_LIST[i] [0]

DATA_LIST[i][1], DATA_LIST[j][1]=DATA_ LIST[j][l],DATA_LIST[i][l]

DATA_LIST[i] [2], DATA_LIST[j] [2]=DATA_ LIST[j][2],DATA_LIST[i][2]

DATA_LIST[i][3], DATA_LIST[j][3]=DATA_ LIST[j][3] ,DATA_LIST[i] [3]

print DATA_LIST

def insertion_sort(DATA_LIST):

for K in range (1, len(DATA_LIST)):

temp=DATA_LIST[K][2]

ptr=K-1

print “Insertion Sort Iterations – Desc order of price”

while(ptr>=0) and DATA_LIST[ptr][2] < temp:

DATA_LIST[ptr+1] [0]=DATA_LIST[ptr] [0]

DATAJHST [ptr+1] [1]=DATA_LIST[ptr] [1]

DATA_LIST[ptr+1][2]=DATA_LIST[ptr][2]

DATA_LIST[ptr+1] [3]=DATA_LIST[ptr] [3]

ptr=ptr-1

DATA_LIST[ptr+1][2]=temp

print DATA_LIST

maxrange = input(“Enter Number of Items: “)

Items=[]

for i in range (maxrange):

Details=[]

Details. append(input(“Enter Item Code”))

Details.append(raw_input(“Enter Item name”))

Details.append(float(raw_input(“Enter price”)))

Details.append(input(“Enter Quantity”)) Items. append(Details)

print “BEFORE SORTING”,Items bubble_sort(Items)

insertion_sort(Items)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...