Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,53 @@
|
|||
// This can be run using Cintcode BCPL freely available from www.cl.cam.ac.uk/users/mr10.
|
||||
|
||||
GET "libhdr.h"
|
||||
|
||||
LET quicksort(v, n) BE qsort(v+1, v+n)
|
||||
|
||||
AND qsort(l, r) BE
|
||||
{ WHILE l+8<r DO
|
||||
{ LET midpt = (l+r)/2
|
||||
// Select a good(ish) median value.
|
||||
LET val = middle(!l, !midpt, !r)
|
||||
LET i = partition(val, l, r)
|
||||
// Only use recursion on the smaller partition.
|
||||
TEST i>midpt THEN { qsort(i, r); r := i-1 }
|
||||
ELSE { qsort(l, i-1); l := i }
|
||||
}
|
||||
|
||||
FOR p = l+1 TO r DO // Now perform insertion sort.
|
||||
FOR q = p-1 TO l BY -1 TEST q!0<=q!1 THEN BREAK
|
||||
ELSE { LET t = q!0
|
||||
q!0 := q!1
|
||||
q!1 := t
|
||||
}
|
||||
}
|
||||
|
||||
AND middle(a, b, c) = a<b -> b<c -> b,
|
||||
a<c -> c,
|
||||
a,
|
||||
b<c -> a<c -> a,
|
||||
c,
|
||||
b
|
||||
|
||||
AND partition(median, p, q) = VALOF
|
||||
{ LET t = ?
|
||||
WHILE !p < median DO p := p+1
|
||||
WHILE !q > median DO q := q-1
|
||||
IF p>=q RESULTIS p
|
||||
t := !p
|
||||
!p := !q
|
||||
!q := t
|
||||
p, q := p+1, q-1
|
||||
} REPEAT
|
||||
|
||||
LET start() = VALOF {
|
||||
LET v = VEC 1000
|
||||
FOR i = 1 TO 1000 DO v!i := randno(1_000_000)
|
||||
quicksort(v, 1000)
|
||||
FOR i = 1 TO 1000 DO
|
||||
{ IF i MOD 10 = 0 DO newline()
|
||||
writef(" %i6", v!i)
|
||||
}
|
||||
newline()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue