sorts 1'000'000 in 0.3 seconds
number of elements is written to file with c:/N.txt or use variable n
array d(n) can be read from a file or synthesized in a program
Code: Select all
; Russian Sorting Halves Danilin
OpenConsole()
Declare RussianSortingHalvesDAV (ab, yz, part, age)
;n=1234567
If ReadFile(0, "c:/N.txt")
n = Val(ReadString(0))
CloseFile(0)
EndIf
age=Int(1+(Log(n)/Log(2)))
Global Dim d(n)
Global Dim a(n)
For i=1 To n
;d(i)=Random(n,1)
d(i)=n-i+1;
Next
PrintN(" First 20")
For k=1 To 20: Print(" "+ d(k)): Next: PrintN("")
PrintN(" Last 10")
For k=n-9 To n : Print(" "+ d(k)): Next: PrintN("")
start=ElapsedMilliseconds()
If age > 0 :
RussianSortingHalvesDAV(1, n, 1, age)
EndIf
finish=ElapsedMilliseconds()
PrintN("RussianSorting First 50")
For k=1 To 50: Print(" "+ d(k)): Next: PrintN("")
PrintN(" Last 20")
For k=n-19 To n : Print(" "+ d(k)): Next: PrintN("")
PrintN( "Time RussianSorting = " + Str(finish-start))
If OpenFile(0, "c:/RSH_DAV.txt")
For k=1 To 1000 :WriteString (0, " " +d(k)): Next
For k=n-1001 To n :WriteString (0, " " +d(k)): Next
CloseFile(0)
EndIf
Input()
End
Procedure RussianSortingHalvesDAV (ab, yz, part, age)
If yz-ab < 1 :ProcedureReturn 0
EndIf
For i=ab To yz
summa=summa+d(i)
Next
middle=summa/(yz-ab+1)
abc=ab-1
xyz=yz+1
For j=ab To yz
If d(j) <= middle:
abc=abc+1: a(abc)=d(j)
Else
xyz=xyz-1: a(xyz)=d(j)
EndIf
Next
For w=ab To yz: d(w)=a(w): Next
If part < age :
If abc >= ab :RussianSortingHalvesDAV(ab, abc, part+1, age)
EndIf
If xyz < yz :RussianSortingHalvesDAV(xyz, yz, part+1, age)
EndIf
EndIf
EndProcedure
https://www.youtube.com/watch?v=UxvSwOtpiuc
https://www.youtube.com/watch?v=9poxfAcbxFQ
feature of the algorithm of the topic:
everyone is able to understand this algorithm
unlike certain incomprehensible algorithms machine sorting.
especially considering: my algorithm is pretty quick.
means using for example sorting of goods
or sorting the way people can be sure:
algorithm and quick and understandable to people.
and another feature: speeds up slow sorts
in 2 ... 4 ... 8 times dividing the bubble sort array
which makes my algorithm even more human.