-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort Algorithims.py
More file actions
72 lines (72 loc) · 1.88 KB
/
Copy pathSort Algorithims.py
File metadata and controls
72 lines (72 loc) · 1.88 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
####QuickSort####
def quickSort(arr):
less = []
pivotList = []
more = []
if len(arr) <= 1:
return arr
else:
pivot = arr[0]
for i in arr:
if i < pivot:
less.append(i)
elif i > pivot:
more.append(i)
else:
pivotList.append(i)
less = quickSort(less)
more = quickSort(more)
print (less + pivotList + more)
return less + pivotList + more
####MergeSort####
def mergeSort(arr):
result = []
if len(arr) < 20:
return sorted(arr)
mid = int(len(arr)/2)
y = msort4(arr[:mid])
z = msort4(arr[mid:])
i = 0
j = 0
while i < len(y) and j < len(z):
if y[i] > z[j]:
result.append(z[j])
j += 1
else:
result.append(y[i])
i += 1
result += y[i:]
result += z[j:]
return result
####CocktailSort####
def cocktailSort(arr):
up = range(len(arr)-1)
while True:
for indices in (up, reversed(up)):
swapped = False
for i in indices:
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
swapped = True
if not swapped:
return
print(arr)
####InsertionSort
def insertionSort(arr):
for i in range(1, len(arr)):
key = arr[i]
low, up = 0, i
while up > low:
middle = (low + up) // 2
if arr[middle] < key:
low = middle + 1
else:
up = middle
arr[:] = arr[:low] + [key] + arr[low:i] + arr[i + 1:]
print(arr)
####SelectionSort####
def selectionSort(arr):
for i in range(0,len(arr)-1):
mn = min(range(i,len(arr)), key=arr.__getitem__)
arr[i],arr[mn] = arr[mn],arr[i]
return arr