Skip to content

Commit b0363d7

Browse files
authored
Merge pull request kal179#13 from Hotdogcode/add-hdc
Selection Sort and Insertion Sort
2 parents 2129fb5 + 03a0e2c commit b0363d7

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

algorithms/sorting/insertion_sort.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Insertion Sort
4+
# Ideal sorting algorithm for
5+
# small/small-medium data/array
6+
7+
# temporary parameter tells
8+
# function to sort a copy of
9+
# orignal array, and not the array itself
10+
11+
# reverse parameter tells func
12+
# to sort array in reverse/decending
13+
# order
14+
15+
16+
def sort_(arr,temporary=False,reverse=False):
17+
18+
# Making copy of array if temporary is true
19+
if temporary:
20+
ar = arr[:]
21+
else:
22+
ar = arr
23+
24+
# To blend every element
25+
# in correct position
26+
# length of total array is required
27+
length = len(ar)
28+
29+
# After each iteration left-most
30+
# sub-array is completed sorted
31+
for i in range(1,length):
32+
# In each iteration we place
33+
# the current element to its
34+
# proper position in left sorted
35+
# sub array
36+
tmp = ar[i]
37+
j = i-1
38+
if reverse:
39+
while j>=0 and tmp>ar[j]:
40+
ar[j+1]=ar[j]
41+
j-=1
42+
ar[j+1]=tmp
43+
else:
44+
while j>=0 and tmp<ar[j]:
45+
ar[j+1]=ar[j]
46+
j-=1
47+
ar[j+1]=tmp
48+
49+
50+
# if temporary, then returning
51+
# copied arr's sorted form
52+
# cuz if not returned, then function
53+
# is literally of no use
54+
if temporary:
55+
return ar
56+
57+
58+
# See proper explaination
59+
# at: https://www.hotdogcode.com/insertion-sort/
60+
61+
62+
# Testing
63+
tests = [[7, 8, 9, 6, 4, 5, 3, 2, 1, 15], [1, 90, 1110, 1312, 1110, 98, 76, 54, 32, 10], ] # Add your test cases
64+
65+
for test in tests:
66+
accend, decend = sort_(test, True), sort_(test, True, True)
67+
if accend == sorted(test) and decend == sorted(test, reverse = True):
68+
print("Orignal: {}".format(test))
69+
print("Sorted: {}".format(accend))
70+
print("Sorted(reverse): {}\n".format(decend))
71+
else:
72+
print("Something went wrong!\n")
73+
74+
75+
# Seems our insertion sort works
76+
# however for small/small-medium data/array!

algorithms/sorting/selection_sort.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Selection Sort
4+
# Ideal sorting algorithm for
5+
# small/small-medium data/array
6+
7+
# temporary parameter tells
8+
# function to sort a copy of
9+
# orignal array, and not the array itself
10+
11+
# reverse parameter tells func
12+
# to sort array in reverse/decending
13+
# order
14+
15+
16+
def sort_(arr,temporary =False,reverse=False):
17+
18+
# Making copy of array if temporary is true
19+
if temporary:
20+
ar = arr[:]
21+
else:
22+
ar = arr
23+
24+
# To blend every element
25+
# in correct position
26+
# length of total array is required
27+
length = len(ar)
28+
29+
# After each iteration left-most
30+
# sub-array is completed sorted
31+
for i in range(0,length):
32+
min = i
33+
34+
# In each iteration we compare
35+
# current element with the right
36+
# unsorted sub-array and replace
37+
# the minimum/maximum with current
38+
# element
39+
for j in range(i+1,length):
40+
41+
# Checking for minimum/maximum
42+
if reverse:
43+
if ar[min]<ar[j]:
44+
min = j
45+
else:
46+
if ar[min]>ar[j]:
47+
min = j
48+
49+
50+
# Replacing minimum/maximum
51+
# with current element
52+
tmp = ar[i]
53+
ar[i]=ar[min]
54+
ar[min]=tmp
55+
56+
57+
58+
# if temporary, then returning
59+
# copied arr's sorted form
60+
# cuz if not returned, then function
61+
# is literally of no use
62+
if temporary:
63+
return ar
64+
65+
66+
# See proper explaination
67+
# at: https://www.hotdogcode.com/selection-sort/
68+
69+
70+
# Testing
71+
tests = [[7, 8, 9, 6, 4, 5, 3, 2, 1, 15], [1, 90, 1110, 1312, 1110, 98, 76, 54, 32, 10], ] # Add your test cases
72+
73+
for test in tests:
74+
accend, decend = sort_(test, True), sort_(test, True, True)
75+
if accend == sorted(test) and decend == sorted(test, reverse = True):
76+
print("Orignal: {}".format(test))
77+
print("Sorted: {}".format(accend))
78+
print("Sorted(reverse): {}\n".format(decend))
79+
else:
80+
print("Something went wrong!\n")
81+
82+
83+
# Seems our selection sort works
84+
# however for small/small-medium data/array!
85+

0 commit comments

Comments
 (0)