-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort.py
68 lines (61 loc) · 1.94 KB
/
bubble_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Following is the data of height of 10 students of Sports class in school.
Lined up in a random order in front of the teacher, who’s put to the task of
lining all up in an ascending order of height. Now your task is to help your
teacher in arranging them using following set of data and measure their
execution time and time complexity.
Height:
Student 1 2 3 4 5 6 7 8 9 10
Height 89 42 100 93 11 234 30 82 22 75
"""
import time
import random
def bubble_sort(student_list):
for i in range(0, len(student_list)-1):
for j in range(len(student_list)-i-1):
if(student_list[j] > student_list[j+1]):
student_list[j], student_list[j +
1] = student_list[j+1], student_list[j]
return student_list
try:
len_list = int(input("Enter no of student:"))
if len_list < 0:
print("Sorry,negative size")
else:
student_list = []
for i in range(0, len_list):
k = int(input("Enter student id "+str(i+1)+" height: "))
student_list.append(k)
s = time.time()
print("unorder student height list is: ", student_list)
print("sorted student height list is: ", bubble_sort(student_list))
e = time.time()
print("Exeution time", e-s)
except ValueError:
print('Please input a valid integer')
"""
# best case
student_list = [i for i in range(10000)]
print("Best Case")
s = time.time()
bubble_sort(student_list)
e = time.time()
print("Exeution time", e-s)
print()
# Worst case
print("Worst Case")
student_list = [i for i in range(10000, -1, -1)]
s = time.time()
bubble_sort(student_list)
e = time.time()
print("Exeution time", e-s)
print()
# Average case
print("Average Case")
student_list = [random.randint(0, 100000) for i in range(10000)]
s = time.time()
bubble_sort(student_list)
e = time.time()
print("Exeution time", e-s)
print()
"""