-
Notifications
You must be signed in to change notification settings - Fork 17
/
really-simple-sort.py
55 lines (37 loc) · 983 Bytes
/
really-simple-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
#!/usr/bin/env python2
from random import randint
m = 12
x = [ randint(0, 1) for i in xrange(0, 100) ]
print x
x_m = [ randint(0, m) for i in xrange(0, 100) ]
print x_m
def really_simple_sort(x):
out = []
for i in xrange(len(x)):
if x[i] == 0:
out += [x[i]]
for i in xrange(len(x) - len(out)):
out += [1]
return out
def count_keys_equal(x, m):
equal = [0] * (m + 1)
for i in xrange(len(x)):
equal[x[i]] = equal[x[i]] + 1
return equal
def count_keys_less(equal):
less = [0] * (len(equal) + 1)
for i in xrange(1, len(equal) + 1):
less[i] += less[i - 1] + equal[i - 1]
return less
def rearrange(A, less):
B = [0] * len(A)
for i in xrange(len(A)):
key = A[i]
pos = less[key]
B[pos] = key
less[key] = less[key] + 1
return B
print really_simple_sort(x)
equal = count_keys_equal(x_m, m)
less = count_keys_less(equal)
print rearrange(x_m, less)