Sort a list of object identifiers: Difference between revisions

Content added Content deleted
Line 676: Line 676:
=val(a$(i))>val(b$(i))
=val(a$(i))>val(b$(i))
}
}
</lang>

===Using QuickSort===
We can make any OID as array of numbers and we can put all OIDs in an array to sort by a custom Quick Sort, where we place the compare function as a lambda function. Swaps made to pointers of arrays of OIDs. There is no need to use PIECE$() in compare function.

Note that QuickSort need Lower or Equal, not

<lang M2000 Interpreter>
Group Quick {
Private:
Function partition {
Read &A(), p, r
x = A(r)
i = p-1
For j=p to r-1 {
If .LE(A(j), x) Then {
i++
Swap A(i),A(j)
}
}
Swap A(i+1),A(r)
= i+1
}
Public:
LE=Lambda->False
Function quicksort {
Read &A(), p, r
If p < r Then {
q = .partition(&A(), p, r)
Call .quicksort(&A(), p, q - 1)
Call .quicksort(&A(), q + 1, r)
}
}
}
\\ no easy way to join ;
Function join$(a$()) {
n=each(a$(), 1, -2)
k$=""
while n {
overwrite k$, ".", n^:= str$(array(n),"")
}
=k$
}
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", "1.3.6.1.4.1.11150.3.4.0.1"
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", "1.3.6.1.4.1.11150.3.4.0"
Dim Base 0, arr(Stack.Size)
i=0 : While not Empty {let arr(i)=piece$(letter$+".", ".") : i++ }
}

For i=0 to len(arr())-1 {
Print join$(arr(i))
}
Quick.LE=lambda (a, b)->{
Link a, b to a$(), b$()
def i=-1
do {
i++
} until a$(i)="" or b$(i)="" or a$(i)<>b$(i)
if b$(i)="" then =a$(i)="":exit
if a$(i)="" then =true:exit
=val(a$(i))<=val(b$(i))
}
Call Quick.quicksort(&arr(), 0, Len(arr())-1)
For i=0 to len(arr())-1 {
Print join$(arr(i))
}

</lang>
</lang>