« Got a new book: Rambl… | Home | MBS REALbasic plug-in… »

Sorting?


I was asked why the plugin has no sort class? Well, the answer is simply: Once you have the data somewhere in an array, in a listbox or a database, you have built in sort functions. And once I tried to write a plugin class for sorting, I got two issues: Well first if you split the sort code between plugin and the events you'd need to fill like compare and swap, the sort code in the plugin is basicly empty. And as the plugin needs to call to RB code for this events, we have a little performance problem. It is much faster to do it directly in REALbasic.
So basicly the solution is: If you need code, take the following code and adjust it to your datatype:
Sub QuickSort(list() as integer, lo as integer,hi as integer)
  #pragma disablebackgroundtasks
  
  dim i,j as integer
  dim x as integer
  dim y as integer
  
  i=lo
  j=hi
  x=list((lo+hi)/2)
  
  while (i<=j)
    
    while list(i)x
      j=j-1
    wend
    
    if i<=j then
      
      // swap values
      y=list(i)
      list(i)=list(j)
      list(j)=y
      
      i=i+1
      j=j-1
    end if
  wend
  
  if lo < j then
    QuickSort list,lo,j
  end if
  
  if i < hi then
    QuickSort list,i,hi
  end if
End Sub

Sub Open()
  // some test code:
  dim list(-1) as integer
  
  for i as integer = 1 to 10
    List.Append rnd*100
  next
  
  'for i as integer = 0 to UBound(List)
  '  Listbox1.AddRow str(List(i))
  'next
  
  QuickSort list, 0, UBound(List)
  
  for i as integer = 0 to UBound(List)
    Listbox1.AddRow str(List(i))
  next
  
End Sub
17 01 10 - 23:41
one comment

Hi Christian,
Deine Source ist an der Stelle “while list(i)x” kaputt.
Ich habe x,y,i,j mit sinnigen Namen versehen und dann den Algo repariert. Du Kannst den so nehmen oder wieder in x,y,i,j tauschen.
So funktionierts(Preview Comment schneidet den Text ab – mail mir wenn Post das auch macht):

Private Sub QuickSort(list() as integer, lo as integer,hi as integer)
#pragma disablebackgroundtasks

dim links,rechts as integer
dim PivotWert as integer ‘Pivotelement
dim changeBuffer as integer

links=lo
rechts=hi
PivotWert=list((lo+hi)/2)

while (links PivotWert
rechts = rechts-1
wend

while list(links) < PivotWert
links =links+1
wend

if links
Raimund Nisius - 01 01 16 - 18:59


  
Remember personal info?

Emoticons / Textile


Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.