-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.py
90 lines (70 loc) · 2.1 KB
/
Sample.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
84
85
86
87
88
89
90
def selectionSort(arr):
for i in range(len(arr) - 1):
for j in range(i + 1, len(arr)):
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[i]
return arr
def bubbleSort(arr):
for i in range(len(arr)):
swapped = False
for j in range(len(arr) - 1 - i):
if arr[j] > arr[j + 1]:
swapped = True
arr[j], arr[j + 1] = arr[j + 1], arr[j]
if not swapped:
break
return arr
def insertionSort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
def quickSort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
mid = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quickSort(left) + mid + quickSort(right)
def mergeSort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = mergeSort(arr[:mid])
right = mergeSort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
arr = [3, 2, 4, 5, 67, 1, 8, 1]
print(selectionSort(arr))
print(bubbleSort(arr))
print(insertionSort(arr))
print(quickSort(arr))
print(mergeSort(arr))
list = [3, -2, 5, 6, 7, -5, -4, -2]
for i in range(len(list)):
if i % 2 == 0 and not list[i] > 0:
for j in range(i + 1, len(list)):
if list[j] > 0:
list[j], list[i] = list[i], list[j]
elif i % 2 == 1 and not list[i] < 0:
for k in range(i + 1, len(list)):
if list[k] < 0:
list[k], list[i] = list[i], list[k]
print(list)