-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion.py
52 lines (36 loc) · 1.24 KB
/
insertion.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
#-----------------------------------------------------------------------
# insertion.py
#-----------------------------------------------------------------------
import sys
import stdlib.stdio as stdio
#-----------------------------------------------------------------------
# Exchange a[i] and a[j].
def exchange(a, i, j):
a[i], a[j] = a[j], a[i]
#-----------------------------------------------------------------------
# Sort array a into increasing order.
def sort(a):
n = len(a)
for i in range(1, n):
j = i
while (j > 0) and (a[j] < a[j-1]):
exchange(a, j-1, j)
j -= 1
#-----------------------------------------------------------------------
# Read strings from standard input, sort them into
# increasing order, and write them to standard output.
def main():
a = stdio.readAllStrings()
sort(a)
for s in a:
stdio.write(s + ' ')
stdio.writeln()
if __name__ == '__main__':
main()
#------------------------------------------------------------------------
# more tiny.txt
# the and was his you tom but for had him
# python insertion.py < tiny.txt
# and but for had him his the tom was you
# python insertion.py < tomsawyer.txt
# ... (Works, but takes a very long time.)