-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortSortedLists.sce
More file actions
75 lines (65 loc) · 2.75 KB
/
SortSortedLists.sce
File metadata and controls
75 lines (65 loc) · 2.75 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//----------------------------------------------------------------------------//
// THIS FUNCTION TAKES IN TWO SORTED LISTS AND SORTS THEM AS WELL. SO, IF THE //
// SIZE OF THE FIRST LIST IS N AND THE OTHER IS M THE NEW LIST WILL BE OF //
// SIZE N+M. //
function lstC = SortSortedLists1(lstA, lstB)
// GET EVERYONE'S SIZE! //
szA = length(lstA); szB = length(lstB)
szC = szA + szB;
// CREATE THE CONTAINER WHERE OUR NEW LIST WILL COME OUT. //
lstC = zeros(1, szC);
// SET A LIMIT FOR THESE GUYS. //
lim = min(szA, szB);
// START SORTING! //
// TODO : MODIFY WITHOUT HAVING TO UPDATE LISTS! //
cc = 1;
while isempty(lstA) == %F & isempty(lstB) == %F
if lstA(1)< lstB(1) then
lstC(cc) = lstA(1);
lstA = lstA(2:$);
else
lstC(cc) = lstB(1);
lstB = lstB(2:$);
end
cc = cc + 1;
end
if isempty(lstA) then
lstC(cc:$) = lstB;
else
lstC(cc:$) = lstA;
end
endfunction
//----------------------------------------------------------------------------//
//----------------------------------------------------------------------------//
// THIS FUNCTION TAKES IN TWO SORTED LISTS AND SORTS THEM AS WELL. SO, IF THE //
// SIZE OF THE FIRST LIST IS N AND THE OTHER IS M THE NEW LIST WILL BE OF //
// SIZE N+M. //
function lstC = SortSortedLists2(lstA, lstB)
// GET EVERYONE'S SIZE! //
szA = length(lstA); szB = length(lstB)
szC = szA + szB;
// CREATE THE CONTAINER WHERE OUR NEW LIST WILL COME OUT. //
lstC = zeros(1, szC);
// SET A LIMIT FOR THESE GUYS. //
lim = min(szA, szB);
uLim = max(szA, szB);
// START SORTING! //
// TODO : MODIFY WITHOUT HAVING TO UPDATE LISTS! //
cc = 1; aa = 1; bb = 1;
while aa <= lim & bb <= lim
if lstA(aa) < lstB(bb) then
lstC(cc) = lstA(aa)
aa = aa + 1;
else
lstC(cc) = lstB(bb);
bb = bb + 1
end
cc = cc + 1;
end
if aa > lim then
lstC(cc:$) = lstB(bb:$)
else
lstC(cc:$) = lstA(aa:$)
end
endfunction
//----------------------------------------------------------------------------//