QuickSort using Class & Events (αγγλικές εντολές)

Επισκόπηση προηγούμενης Θ.Ενότητας Επισκόπηση επόμενης Θ.Ενότητας Πήγαινε κάτω

QuickSort using Class & Events (αγγλικές εντολές)

Δημοσίευση από m2000 Την / Το Τρι Ιουν 21, 2016 9:03 pm

\\ Quick Sort - M2000 code - Version 2
\\ using local calls, class and events
Flush
Class SortMe {
      Event Comp {Read New j, r, &w}
      Event SwapItem {Read New i, j}
      many=0
      Module QuickSort {
            \\ A(), M and N are visible with call local
            Function QuickSort {
                   Read New p,r
                   Local q
                   if p < r Then {
                        Call local partition(p, r, &q)
                        Call local Quicksort(p, q - 1)
                        Call local Quicksort(q + 1, r)
                      }
            }
            Function partition {
                   Read New p, r, &q
                   local i, j, w
                     i = p-1
                     for j=p to r-1 {
                         i++
                         call event .Comp j, r, &w
                         select case w
                         case N
                                    i--
                         case M
                                 Call Event .SwapItem i, j
                          end select
                         }
                  Call Event .SwapItem i+1, r
                  q=i+1
            }
           way$="Up"
            Read ? way$
           way$=ucase$(way$)
            If way$="UP" then {
                  N=1
            } else.if way$="DOWN" then {
                  N=-1
            } else {
                  Error "not known option"
            }
            M=-N
            Call Local QuickSort(0,.many-1)
      }
}
Form 80,50
Dim arr(10), old()
\\ we make one object from SortMe() constructor
SortMe=SortMe()
\\ here we define event listeners
\\ We pass as lazy$() functions
\\ so the call are local to this module
\\ so arr() is visible
\\ Now Quick Sort can be used for any array, and for strings
Function Comp1 {
        Read New j , r , &w
        w=compare(arr(j),arr(r))
}
Function Swap1 {
        Read New i, j
       swap arr(i), arr(j)
}
\\ now we feed SortMe Events with listeners
For SortMe {
      Event .Comp New lazy$(&Comp1())
      Event .SwapItem New lazy$(&Swap1())
      .many=10
}
arr(0)=23,21,1,4,2,3,102,54,26,9
old()=arr()
Profiler
      SortMe.Quicksort
Print str$(timecount,"0")
Profiler
      SortMe.Quicksort
Print str$(timecount,"0") , " time to sort already sorted array"
For i=0 to 9
      Print arr(i), old(i)
next i
old()=arr()
Profiler
      SortMe.Quicksort  "down"
Print str$(timecount,"0")
Profiler
      SortMe.Quicksort  "down"
Print str$(timecount,"0") , " time to sort already sorted array"
For i=0 to 9
      Print arr(i), old(i)
next i
Dim arr(10)=1
old()=arr()
Profiler
      SortMe.Quicksort  "down"
Print str$(timecount,"0") , " time to sort all items equal"
For i=0 to 9
      Print arr(i), old(i)
next i
avatar
m2000
Admin

Posts : 26
Join date : 10/06/2016
Age : 50
Location : Kanallaki Greece

http://m2000.forumgreek.com

Επιστροφή στην κορυφή Πήγαινε κάτω

Επισκόπηση προηγούμενης Θ.Ενότητας Επισκόπηση επόμενης Θ.Ενότητας Επιστροφή στην κορυφή


 
Δικαιώματα σας στην κατηγορία αυτή
Δεν μπορείτε να απαντήσετε στα Θέματα αυτής της Δ.Συζήτησης