-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathselection_sort.py
85 lines (62 loc) · 1.98 KB
/
selection_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
84
# -*- coding: utf-8 -*-
# Selection Sort
# Ideal sorting algorithm for
# small/small-medium data/array
# temporary parameter tells
# function to sort a copy of
# orignal array, and not the array itself
# reverse parameter tells func
# to sort array in reverse/decending
# order
def sort_(arr,temporary =False,reverse=False):
# Making copy of array if temporary is true
if temporary:
ar = arr[:]
else:
ar = arr
# To blend every element
# in correct position
# length of total array is required
length = len(ar)
# After each iteration left-most
# sub-array is completed sorted
for i in range(0,length):
min = i
# In each iteration we compare
# current element with the right
# unsorted sub-array and replace
# the minimum/maximum with current
# element
for j in range(i+1,length):
# Checking for minimum/maximum
if reverse:
if ar[min]<ar[j]:
min = j
else:
if ar[min]>ar[j]:
min = j
# Replacing minimum/maximum
# with current element
tmp = ar[i]
ar[i]=ar[min]
ar[min]=tmp
# if temporary, then returning
# copied arr's sorted form
# cuz if not returned, then function
# is literally of no use
if temporary:
return ar
# See proper explaination
# at: https://www.hotdogcode.com/selection-sort/
# Testing
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
for test in tests:
accend, decend = sort_(test, True), sort_(test, True, True)
if accend == sorted(test) and decend == sorted(test, reverse = True):
print("Orignal: {}".format(test))
print("Sorted: {}".format(accend))
print("Sorted(reverse): {}\n".format(decend))
else:
print("Something went wrong!\n")
# Seems our selection sort works
# however for small/small-medium data/array!