forked from kumaya/python-programs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.py
More file actions
Latest commit
128 lines (108 loc) · 4.31 KB
/
Copy pathsorting.py
File metadata and controls
128 lines (108 loc) · 4.31 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
""" Bubble sort: The bubble sort makes multiple passes through a list.
It compares adjacent items and exchanges those that are out of order.
Each pass through the list places the next largest value in its proper place.
"""
defbubble_sort(inp_list):
foriinrange(len(inp_list)-1, 0, -1):
forjinrange(i):
ifinp_list[j] >inp_list[j+1]:
temp=inp_list[j]
inp_list[j] =inp_list[j+1]
inp_list[j+1] =temp
returninp_list
""" Selection sort: selection sort looks for the largest value as it makes a
pass and, after completing the pass, places it in the proper location.
"""
defselection_sort(inp_list):
foriinrange(len(inp_list)-1, 0, -1):
pos_of_max=0
forjinrange(1, i+1):
ifinp_list[j] >inp_list[pos_of_max]:
pos_of_max=j
temp=inp_list[i]
inp_list[i] =inp_list[pos_of_max]
inp_list[pos_of_max] =temp
returninp_list
""" Insertion Sort: It always maintains a sorted sub list in the lower positions of
the list. Each new item is then inserted back into the previous sub list such that
the sorted sub list is one item larger
"""
definsertion_sort(inp_list):
foriinrange(1, len(inp_list)):
current_val=inp_list[i]
position=i
whileposition>0andinp_list[position-1] >current_val:
inp_list[position] =inp_list[position-1]
position-=1
inp_list[position] =current_val
returninp_list
""" Merge sort
"""
defmerge_sort(inp_list):
iflen(inp_list) >1:
mid=len(inp_list) //2
left_half=inp_list[:mid]
right_half=inp_list[mid:]
merge_sort(left_half)
merge_sort(right_half)
i, j, k=0, 0, 0
whilei<len(left_half) andj<len(right_half):
ifleft_half[i] <right_half[j]:
inp_list[k] =left_half[i]
i+=1
else:
inp_list[k] =right_half[j]
j+=1
k+=1
whilei<len(left_half):
inp_list[k] =left_half[i]
i+=1
k+=1
whilej<len(right_half):
inp_list[k] =right_half[j]
j+=1
k+=1
returninp_list
""" Quick sort
"""
importrandom
defquick_sort(inp_list):
iflen(inp_list) <1:
returninp_list
else:
pivot=random.randint(0, len(inp_list)-1)
pivot_ele=inp_list[pivot]
return (quick_sort([xforxininp_listifx<pivot_ele]) +
[xforxininp_listifx==pivot_ele] +
quick_sort([xforxininp_listifx>pivot_ele]))
fromrandomimportrandint
inp_list= [randint(-2, i) foriinrange(10)]
print"Bubble sort:", bubble_sort(inp_list)
inp_list= [randint(-2, i) foriinrange(10)]
print"Selection sort:", selection_sort(inp_list)
inp_list= [randint(-2, i) foriinrange(10)]
print"Insertion sort:", insertion_sort(inp_list)
inp_list= [randint(-2, i) foriinrange(10)]
print"Merge sort:", merge_sort(inp_list)
inp_list= [randint(-2, i) foriinrange(10)]
print"Quick sort:", quick_sort(inp_list)
print"*"*56
print("Let's check the running time of these sorting techniques")
print"*"*56
importtimeit
fromtimeitimportTimer
inp_list1= [randint(-10000, 10000) foriinrange(1000)]
qs=Timer("quick_sort(inp_list1)", "from __main__ import quick_sort, inp_list1")
print"time taken for quick_sort: ", qs.timeit(number=1000), "ms"
inp_list2= [randint(-10000, 10000) foriinrange(1000)]
ms=Timer("merge_sort(inp_list2)", "from __main__ import merge_sort, inp_list2")
print"time taken for merge_sort: ", qs.timeit(number=1000), "ms"
inp_list3= [randint(-10000, 10000) foriinrange(1000)]
inss=Timer("insertion_sort(inp_list3)", "from __main__ import insertion_sort, inp_list3")
print"time taken for insertion_sort: ", inss.timeit(number=1000), "ms"
inp_list4= [randint(-10000, 10000) foriinrange(1000)]
ss=Timer("selection_sort(inp_list4)", "from __main__ import selection_sort, inp_list4")
print"time taken for selection_sort: ", ss.timeit(number=1000), "ms"
inp_list5= [randint(-10000, 10000) foriinrange(1000)]
bs=Timer("bubble_sort(inp_list5)", "from __main__ import bubble_sort, inp_list5")
print"time taken for bubble_sort: ", bs.timeit(number=1000), "ms"