Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

BSCS-402 DATA STRUCTURE

LAB 9
Q1.Consider the list of characters: [‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’]. Show how this list is
sorted

using the following algorithm.

• Bubble Sort
• Selection Sort
• Insertion Sort

Q2. Create a structure/class for a group of 50 students holding data for their Regn
no., Name, Course, CGPA.
a) Call linear search function to display data of student with a particular Regn no.
b) Call bubble sort function to arrange data of students according to Regn no.
c) Apply binary search on the above output (part b) to display data of a student
with a particular Regn no.
d) Use and modify Insertion sort logic to arrange data of students in descending
order of CGPA.

Q2.
S.no LAB_9_Q2.PY
1 from SpecialSearchingLibrary import LineraSearch as ls
2 from SpecialSearchingLibrary import bubbleSort as bs
3 from SpecialSearchingLibrary import bSearch as Bs
4 from SpecialSearchingLibrary import selectionSort as ss
5
6 class Uni:
7 def __int__(self,reg,name,course,cgpa):
8 self.reg = reg
9 self.name = name
10 self.course = course
11 self.cgpa = cgpa
12
13 def reg(self,reg):
14 self.reg = reg
15 def name(self,name):
16 self.name = name
17 def course(self,course):
18 self.course = course
19 def cgpa(self,cgpa):
20 self.cgpa = cgpa
21
22 def Students(self):
23 print("REG NO:",self.reg,"\tName :",self.name,"\tCourse
24 :",self.course,"\tCGPA:",self.cgpa)
25
26
27 s = []
28 # n = int(input("Enter total Number of Students:"))
29 # for i in range(n):
30 new_student = Uni()
31
32 new_student.name("Anas")
33 new_student.reg(12)
34 new_student.course("BSCS")
35 new_student.cgpa(3.1)
36 s.append(new_student)
37
38 new_student = Uni()
39 new_student.name("Izhan")
40 new_student.reg(23)
41 new_student.course("BSCS")
42 new_student.cgpa(3.5)
43 s.append(new_student)
44
45 new_student = Uni()
46 new_student.name("Umer")
47 new_student.reg(34)
48 new_student.course("BSCS")
49 new_student.cgpa(3.2)
50 s.append(new_student)
51
52 new_student = Uni()
53 new_student.name("AHmed")
54 new_student.reg(14)
55 new_student.course("BSCS")
56 new_student.cgpa(3.4)
57 s.append(new_student)
58
59
60
61 print("1.Call linear search function to display data of student with a
62 particular Regn no.")
63 print("2.Call bubble sort function to arrange data of students
64 according to Regn no.")
65 print("3.Apply binary search on the above output (part b) to display
66 data of a student with a particular Regn no.")
67 print("4.Use and modify Insertion sort logic to arrange data of
68 students in descending order of CGPA.")
69 print("5.Exit")
70 while True:
71 a= int(input("Selct an Option: "))
72 if a == 1:
73 pik = int(input("Enter Regn No :"))
74 print(ls(s, pik))
75 elif a==2:
76 for i in bs(s):
77 i.Students()
78 elif a == 3:
79 pik = int(input("Enter Regn No :"))
80 print(Bs(bs(s),pik, 0, len(s)-1))
81 elif a == 4:
82 for i in ss(s):
83 i.Students()
84 elif a == 5:
85 break
86 else:
87 print("Choose from Options!!!")

Result:
Q2.
S.no LAB_9_Q2.PY
1 def bubbleSort( theSeq ):
2 swap = 0
3 comparisons = 0
4 n = len( theSeq )
5 # Perform n-1 bubble operations on the sequence
6 for i in range( n - 1 ) :
7 # Bubble the largest item to the end.
8 for j in range( n-1 ) :
9 comparisons +=1
10 if theSeq[j] > theSeq[j + 1]: # swap the j and j+1 items.
11 tmp = theSeq[j]
12 theSeq[j] = theSeq[j + 1]
13 theSeq[j + 1] = tmp
14 swap += 1
15 print("Swapping times:",swap)
16 print("Comparison times:", comparisons)
17 return theSeq
18
19 print("Bubble sort")
20 print(bubbleSort(["P", "Y", "T", "H", "O", "N"]))
21
22
23 print()
24 print("Selection sort")
25 def selectionSort( theSeq ):
26 swap = 0
27 comparisons = 0
28 n = len( theSeq )
29 for i in range( n - 1 ):
30 # Assume the ith element is the smallest.
31 smallNdx = i
32 # Determine if any other element contains a smaller value.
33 for j in range( i + 1, n ):
34 comparisons += 1
35 if theSeq[j] < theSeq[smallNdx] :
36
37 smallNdx = j
38
39 # Swap the ith value and smallNdx value only if the smallest value
40 is
41 # not already in its proper position. Some implementations omit
42 testing
43 # the condition and always swap the two values.
44 if smallNdx != i :
45 tmp = theSeq[i]
46 theSeq[i] = theSeq[smallNdx]
47 theSeq[smallNdx] = tmp
48 swap += 1
49
50 print("Swapping times:", swap)
51 print("Comparison times:", comparisons)
52 return theSeq
53
54 print(selectionSort(["P", "Y", "T", "H", "O", "N"]))
55
56 # Sorts a sequence in ascending order using the insertion sort
57 algorithm.
58
59 print()
60 print("Insertion sort")
61 def insertionSort( theSeq ):
62 swap = 0
63 comparisons = 0
64 n = len( theSeq )
65 # Starts with the first item as the only sorted entry.
66 for i in range( 1, n ) :
67 # Save the value to be positioned.
68 value = theSeq[i]
69 # Find the position where value fits in the ordered part of the list.
70 pos = i
71 comparisons +=1
72 while pos > 0 and value < theSeq[pos - 1] :
73 swap +=1
74 # Shift the items to the right during the search.
75 theSeq[pos] = theSeq[pos - 1]
76 pos -= 1
77
78 # Put the saved value into the open slot.
79 theSeq[pos] = value
80
81 print("Swapping times:", swap)
82 print("Comparison times:", comparisons)
83 return theSeq
84
85 print(insertionSort(["P", "Y", "T", "H", "O", "N"]))
Result:

You might also like