Repository navigation
Expand file tree
/
Copy pathiterative_sorting.py
More file actions
76 lines (62 loc) · 1.86 KB
/
Copy pathiterative_sorting.py
File metadata and controls
76 lines (62 loc) · 1.86 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
def selection_sort( arr ):
# loop through n-1 elements
for i in range(0, len(arr) - 1):
cur_index = i
smallest_index = cur_index
# find next smallest element
for j in range(cur_index, len(arr)):
if arr[j] < arr[smallest_index]:
smallest_index = j
# swap
temp = arr[smallest_index]
arr[smallest_index] = arr[cur_index]
arr[cur_index] = temp
return arr
# TO-DO: implement the Insertion Sort function below
def insertion_sort( arr ):
# loop through n-1 elements
for i in range(1, len(arr)):
temp = arr[i]
j = i
while j > 0 and temp < arr[j - 1]:
# shift left until correct position found
arr[j] = arr[j - 1]
j -= 1
# insert at correct position
arr[j] = temp
return arr
# STRETCH: implement the Bubble Sort function below
def bubble_sort( arr ):
swaps_occurred = True
while swaps_occurred:
swaps_occurred = False
for i in range(0, len(arr)-1):
if arr[i] > arr[i+1]:
# swap
temp = arr[i]
arr[i] = arr[i+1]
arr[i+1] = temp
swaps_occurred = True
return arr
# STRETCH: implement the Count Sort function below
def count_sort( arr, maximum = -1 ):
if len(arr) == 0:
return arr
if maximum == -1:
maximum = max(arr)
# count the number of each element in original arr
count = [0] * (maximum+1)
for value in arr:
if value < 0:
return "Error, negative numbers not allowed in Count Sort"
else:
count[value] += 1
# reinsert values into original array using counts
j = 0
for i in range(0, len(count)):
while count[i] > 0:
arr[j] = i
j += 1
count[i] -= 1
# return sorted array
return arr