-
Notifications
You must be signed in to change notification settings - Fork 0
/
40-sort-array.sb
44 lines (44 loc) · 1.06 KB
/
40-sort-array.sb
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
# $Id: 40-sort-array.sb,v 1.1 2019-01-18 11:47:25-08 - - $
#
# sort numbers
#
# Input is a sequence of numbers ending with end of file.
# User is assumed to have not more than 100 numbers.
# Note that nan <> nan, other was x = x for all x that is not nan.
#
let size = 100
dim a[size]
let max = 0
read: input x
if eof <> 0 goto eof
if x <> x goto error
let max = max + 1
let a[max] = x
if max < size goto read
eof:
print ""
print "unsorted"
let i = 1
prtlp: print "a[", i, "]=", a[i]
let i = i + 1
if i <= max goto prtlp
let i = max
outer: let j = 1
inner: if a[j] <= a[j + 1] goto noswap
let t = a[j]
let a[j] = a[j+1]
let a[j+1]=t
noswap:
let j = j + 1
if j <= i - 1 goto inner
let i = i - 1
if i >= 2 goto outer
print ""
print "sorted"
let i = 1
sortlp: print "a[", i, "]=", a[i]
let i = i + 1
if i <= max goto sortlp
goto stop
error: print "Invalid input"
stop: