Skip to content

Commit 22a1d6f

Browse files
author
robertDurst
committed
code now works for a mergesort
1 parent 01454e6 commit 22a1d6f

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

examples/intListHandler.fish

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Ufn{
3030
return getAtIndexInt(own.list, 0)
3131
})
3232

33+
(fun peek(int i)(int){
34+
return getAtIndexInt(own.list, i)
35+
})
36+
3337

3438
(fun removeByIndex(int i)(void){
3539
Tree (

examples/mergesort.fish

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import intListHandler : "../examples/intListHandler.fish"
22

3-
(fun merge(intListHandler a, intListHandler b, intListHandler c) (intListHandler) {
3+
(fun merge(intListHandler a, intListHandler b) (intListHandler) {
4+
# create another empty list for merging
5+
dec intListHandler c = new intListHandler { list: [], size: 0}
6+
47
Tree (
58
( | a.size == 0 | {
69
c.list = appendListInt(c.list, b.list, c.size, b.size)
@@ -30,21 +33,49 @@ import intListHandler : "../examples/intListHandler.fish"
3033
return merge(a,b,c)
3134
})
3235

33-
start {
34-
dec intListHandler a = new intListHandler { list: [1, 3, 5], size: 3}
35-
dec intListHandler b = new intListHandler { list: [2, 4, 7], size: 3}
36-
dec intListHandler c = new intListHandler { list: [], size: 0}
36+
(fun sublist(intListHandler a, intListHandler b, int front, int back)(intListHandler) {
37+
Tree (
38+
( | front == back | {
39+
dec int j = a...peek(front)
40+
b...push(j)
41+
return b
42+
} )
43+
( | true | {
44+
dec int j = a...peek(front)
45+
b...push(j)
46+
sublist(a, b, front+1, back)
47+
})
48+
)
49+
50+
return b
51+
})
3752

38-
dec intListHandler d = new intListHandler { list: [7, 11, 12], size: 3}
39-
dec intListHandler e = new intListHandler { list: [2, 4, 13, 14, 15], size: 5}
53+
(fun mergesort(intListHandler a, int front, int end)(intListHandler) {
54+
Tree (
55+
( | (front == end) or ((end - front) == 1) | {
56+
dec int i = a...peek(front)
57+
dec intListHandler b = new intListHandler { list: [i], size: 1}
58+
return b
59+
})
60+
61+
# find middle
62+
dec int middle = (end - front) / 2
63+
64+
# generate two empty lists for recursively dividing
4065
dec intListHandler f = new intListHandler { list: [], size: 0}
66+
dec intListHandler b = new intListHandler { list: [], size: 0}
4167

42-
dec intListHandler g = new intListHandler { list: [], size: 0}
68+
f = sublist(a, f, front, middle-1)
69+
b = sublist(a, b, middle, end-1)
4370

44-
c = merge(a,b,c)
45-
f = merge(d,e,f)
46-
g = merge(c,f,g)
47-
71+
return merge(
72+
mergesort(f, 0, f.size),
73+
mergesort(b, 0, b.size)
74+
)
75+
})
4876

49-
g...display(void)
77+
start {
78+
dec intListHandler a = new intListHandler { list: [5, 10, 12, 3, 4, 2, 11, 1], size: 8}
79+
a = mergesort(a, 0, a.size)
80+
a...display(void)
5081
}

0 commit comments

Comments
 (0)