forked from syedmurad1/OOP-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11oop-Sort.py
83 lines (57 loc) · 1.66 KB
/
11oop-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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# bubble sort
from re import template
from string import Template
def sort(nums):
for i in range(len(nums)-1,0,-1):
for j in range(i):
if nums[j]>nums[j+1]:
temp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = temp
nums = [4,9,8,3,5]
sort(nums)
print(nums)
print("--------------------------------------------------------")
# insertion sort
l = [8,11,3,1,2,5,6,9]
def insertion_sort(l):
for outer_index in range(1,len(l)):
temp = l[outer_index]
inner_index = outer_index
while inner_index > 0 and l[inner_index-1] > temp:
l[inner_index] = l[inner_index-1]
inner_index -= 1
l[inner_index] = temp
insertion_sort(l)
print(l)
print("--------------------------------------------------------")
# merge sort - ref w3s
def mergeSort(nlist):
print("Splitting ",nlist)
if len(nlist)>1:
mid = len(nlist)//2
lefthalf = nlist[:mid]
righthalf = nlist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
nlist[k]=lefthalf[i]
i=i+1
else:
nlist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
nlist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
nlist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",nlist)
nlist = [14,46,43,27,57,41,45,21,70]
mergeSort(nlist)
print(nlist)