Τετάρτη 1 Φεβρουαρίου 2023

Universal QuickSort

 This code adapted for Version 12 (in info file is the old one)


Module Universal_QuickSort {
const yellow=14, cyan=11
Class Quick {
Private:
module quicksort (&a()) {
do If Stackitem()>=Stackitem(2) Then Drop 2:if empty then exit else continue
over 2,2
Read p, r : i = p-1 : x=a(r)
For j=p to r-1:If .LE(a(j), x) Then i++:Swap a(i),a(j) //: print "Swap",
Next j : Swap a(i+1), a(r) : Push i+2, i:shift 3 //: print "Swap": print a()
always
}
Public:
// a and b can be strings or numbers
LE=Lambda (a, b)->a<=b
// this is final, we can't change it
Function Final Sort(&a(), a_min, a_max){
stack new {
.quicksort &a(), a_min, a_max
}
}
}
print("Numeric Sort")
Quick=Quick()
Dim a(10)<<Random(50, 100)
print "", a()#str$(", ")
Call Quick.Sort(&a(), 0, Len(a())-1)
result()
print "", a()#str$(", ")
print("OIDS, object identifiers SORT")
Quick=Quick()
integer i
Stack New {
Data "1.3.6.1.4.1.11.2.17.19.3.4.0.4" , "1.3.6.1.4.1.11.2.17.19.3.4.0.1"
Data "1.3.6.1.4.1.11150.3.4.0.1", "1.3.6.1.4.1.11150.3.4.0"
Data "1.3.6.1.4.1.11.2.17.19.3.4.0.10", "1.3.6.1.4.1.11.2.17.5.2.0.79"
Dim arr(0 to Stack.Size-1)
While not Empty
arr(i)=piece$(letter$, ".")
i++
End While
}
// change comparison function
// Version 12 can make comparisons with strings in a() and b()
// a and b are pointer to array
// so we link to a() and b()
// if we use (a(), b()) we get copies of whatever array have in stack
// so here we don't want copies.
// We can use until Val(a(i))<>Val(b(i)) if numbers may have leading zeroes
Quick.LE=lambda (a as array, b as array)->{
Link a, b to a(), b()
long i, k=min.data(len(a), len(b))-1

do i++:if i>k then exit
when a(i)=b(i)
If i>=len(b) Then =i>=len(a):Exit
If i>=len(a) Then =true:Exit
=Val(a(i))<=Val(b(i))
}
For i=0 to len(arr())-1
print "", arr(i)#str$(".")
Next
result()
Call Quick.Sort(&arr(), 0, Len(arr())-1)
For i=0 to len(arr())-1
print "", arr(i)#str$(".")
Next
print("String Sort")
Quick=Quick()
Dim a$()
a$()=("one","two", "three","four", "five")
print "", a$()
Call Quick.Sort(&a$(), 0, Len(a$())-1)
result()
print "", a$()
Enum Language {English=0, Greek}
Enum(English)
Enum(Greek)
italic:print : print "press any key":italic
push key$:drop
end
sub Enum(a)
if a=English then
print("Sort string enumarates (english version)")
Local num: Enum num {one="one",two="two", three="three",four="four", five="five"}
else
print("Sort string enumarates (greek version)")
Local num: Enum num {one="ένα",two="δύο", three="τρία",four="τέσσερα", five="πέντε"}
end if
Local a()
a()=(five, three, one, two, four)
local Quick=Quick()
Quick.LE=lambda (a, b)->len(a)<len(b)
print "", a()#str$(", ")
Call Quick.Sort(&a(), 0, Len(a$())-1)
result()
print "", a()#str$(", ")
end sub
sub print(a$)
print:bold:Pen yellow {print a$}: bold: print
end sub
sub result()
print:bold:Pen cyan {print "Result:"}:bold
end sub
}
const white=15, black=0, tabwidth=6, formatprint$=""
Cls black
Back {Cls black}
Bold 0
Pen white
form 68, 56
Print $(formatprint$,tabwidth)
Double: Bold
Print "Universal QuickSort"
Normal: Bold
Print
Universal_QuickSort



Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.