-
Nov 26th, 2014, 08:31 PM
#1
Thread Starter
Frenzied Member
szlamany sort routine
This is a conversion to M2000 language for szlamany sort routine that he post here
Code:
function mxstrcmp {
\* read by reference
read &Cmp$, start1,end1, start2,end2
end1=end1-start1+1
end2=end2-start2+1
hlp1$=mid$(Cmp$,start1,end1)
hlp2$=mid$(Cmp$,start2,end2)
=compare(hlp1$,hlp2$)
}
function Getone$ {
read &buffer$, start1, end1
end1=end1-start1+1
=mid$(buffer$,start1,end1)
}
\* strSearch$ = "3ne2wo1hree6ive5ix4orth"
strSearch$ = "OneTwoThreeFiveSixForth"
data 1,4,7,12,16,19,0
i= 0
read startKeyWord
do {
dim smarkerskt(i+1), emarkerskt(i+1) \* this is a redim preserve also
read nextStart
smarkerskt(i)=startKeyWord
if nextStart = 0 then {
emarkerskt(i)=len(strSearch$)
break
} else {
emarkerskt(i)=NextStart-1
startKeyWord=NextStart
}
i++
} always
nKeywordskt=i+1
dim sortptrs(nKeywordskt) ' so we have one more item in position 0
\* general form to fill array with a variable value
module fillme {
read &ar()
k=dimension(ar(),1) : i=0 : do { ar(i)=i : i++ : k-- } until k=0
}
fillme &sortptrs()
for i=1 to nKeywordskt {
print getone$( &strSearch$, smarkerskt(sortptrs(i-1)), emarkerskt(sortptrs(i-1)))
}
? "items:";nKeywordskt
\* this is the OLD code to run
s1 = nKeywordskt
s2 = s1
s3 = 0 : s4 = 0 : s5 = 0 : s6 = 0
a = 0
GP_SP_S1:
s1 = int(s1 / 2)
if s1 = 0 then goto GP_SP_S5
s3 = s2 - s1
s4 = 1
GP_SP_S2:
s5 = s4
GP_SP_S3:
s6 = s5 + s1
a = mxstrcmp( &strSearch$, smarkerskt(sortptrs(s5-1)), emarkerskt(sortptrs(s5-1)), smarkerskt(sortptrs(s6-1)), emarkerskt(sortptrs(s6-1)))
if a = 0 then {
if sortptrs(s5-1) < sortptrs(s6-1) then {
a = -1
} else {
a = 1
}
}
if a <= 0 then goto GP_SP_S4
SWAP sortptrs(s5-1), sortptrs(s6-1)
s5 = s5 - s1
if s5 >= 1 then goto GP_SP_S3
GP_SP_S4:
s4 = s4 + 1
if s4 > s3 then goto GP_SP_S1
goto GP_SP_S2
GP_SP_S5:
s1 = 0 \* dummy line
\* here end the old code
\* so we have to print the words
print "--------------------------------------------------"
for i=1 to nKeywordskt {
print getone$( &strSearch$, smarkerskt(sortptrs(i-1)), emarkerskt(sortptrs(i-1)))
}
print "done"
in M2000 we can't write that and expect to have a jump
if x>0 then {
GOTO label
}
because this jump can't go out of the code block
Last edited by georgekar; Nov 26th, 2014 at 08:34 PM.
-
Nov 27th, 2014, 04:42 AM
#2
Thread Starter
Frenzied Member
Re: szlamany sort routine
To run this program you have to open M2000 Environment and write
edit a
Then open the internal editor and we can copy the above code
we press ESC to exit editor and simple type
a
press enter and get the results.
You can save
save sortme
You can write New to clear modules in memory and load to load from disk
new
load sortme
we can see modules in memory and disk with modules command
modules
or we can see modules only in memory
modules ?
We can define functions too
edit a(
We have to include a = as the return value command see the above code. We can define modules and functions as local in other modules and functions. Also we can use load inside modules and functions to use temporary created code, like using an external library. You can load code any time, not only at the first lines of code.
We can remove the last module or function by Remove
Also Flush empty the stack and Clear clear variables. You can use List to see variables.
There is a step by step execution using Test command. We can open a window with zoom capability and we can enter values to inspect changes, and we see in real time the stack values at module/function level.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|