-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_transform_sort.py
More file actions
60 lines (52 loc) · 2.01 KB
/
linear_transform_sort.py
File metadata and controls
60 lines (52 loc) · 2.01 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
import math
# Global variable so we can add sorted sections
# Saves complexity because we don't need to flatten
sorted_list = []
#supporting functions
#gets min_val and max_val
def getmax_val(lyst):
max_val = -math.inf
for i in range(len(lyst)):
if lyst[i] > max_val:
max_val = lyst[i]
return max_val
def getmin_val(lyst):
min_val = math.inf
for i in range(len(lyst)):
if lyst[i] < min_val:
min_val = lyst[i]
return min_val
# main functions for linear transform sort
def linear_transform(entry, max_val, min_val, num_entries):
ret_val = math.floor(num_entries*(entry-min_val)/(max_val-min_val))
# testing: print("transforming " + str(entry) + " to " + str(ret_val))
return ret_val
def linear_transform_sort_helper(sublyst):
global sorted_list
num_entries = len(sublyst)
if num_entries == 0:
return # this bin has no contents
if num_entries == 1:
sorted_list.append(sublyst[0])
return # sorting complete on this bin
min_val = getmin_val(sublyst)
max_val = getmax_val(sublyst)
if min_val == max_val:
for value in sublyst:
sorted_list.append(value)
return # sorting complete on this bin
else: # more sorting is necessary if min_val != max_val and num_entries > 1
bins = []
# OPTIMIZATION: we sacrifice memory space to save complexity by creating all bins before we know if we need them.
for i in range(num_entries + 1): # create n+1 bins
bins.append([])
for i in range(num_entries): # move members into bins
cast_location = linear_transform(sublyst[i], max_val, min_val, num_entries)
bins[cast_location].append(sublyst[i])
for i in range(num_entries + 1): # recurse on each bin
linear_transform_sort_helper(bins[i])
def lt_sort(lyst):
global sorted_list
sorted_list = []
linear_transform_sort_helper(lyst)
return sorted_list