Sorting LinkedList with recursion and Protected LinkedLists

Share your advanced PureBasic knowledge/code with the community.
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

Sorting LinkedList with recursion and Protected LinkedLists

Post by Franky »

Code updated For 5.20+

Hi people :)

When I read in the ChangeLog of PB 4.0 yesterday, I found:
Support for Global, Protected, Static and Shared arrays and linkedlist: "Global NewList MyList.l()". NewList alone does not make the list global anymore
After I found
LinkedList and Arrays can now be passed as procedure parameters
today, I ported a little example of how to sort a linked List with Recursion and List as Parameter from Delphi.

The Algorithm itself is not my invention, I know it from School.

I just wanted to show, what is possible with these new features.

Long talking, short sense: Here is the code ;)

Code: Select all

Global NewList Werte()


Procedure MySort(List List1())
  Protected NewList   leftList()
  Protected NewList   rightList()
  FirstElement(List1())
  If ListSize(List1())>1
    mittwert=List1()
    DeleteElement(List1(),1)
    While ListSize(List1())<>0             
      If List1()<mittwert
        AddElement(LeftList())
        leftList()=List1()
      Else
        AddElement(rightList())
        rightList()=List1()
      EndIf
      DeleteElement(List1(),1)
    Wend
    If ListSize(leftList())>1
      FirstElement(leftList())
      mysort(leftList())
      ResetList(leftList())                     
      FirstElement(leftList())         
      While ListSize(leftList())>0           
        AddElement(List1())
        List1()=leftList()
        DeleteElement(leftList(),1)
      Wend 
    EndIf
    AddElement(List1())             
    List1()=mittwert
    
    If ListSize(rightList())>1
      FirstElement(rightList())
      mysort(rightList())
      FirstElement(rightList())
      While ListSize(rightList())>0           
        AddElement(List1())
        List1()=rightList()
        DeleteElement(rightList(),1)
      Wend                     
    EndIf
  EndIf
EndProcedure

If OpenWindow(1,200,200,400,500,"Sortierer",#PB_Window_SystemMenu)
  
  ListViewGadget(1,0,0,400,440)
  ButtonGadget(2,0,450,500,50,"Sort")
  For a=0 To 500
    AddGadgetItem(1,-1,Str(Random(600)))
  Next
  Repeat
    event=WaitWindowEvent()
    If event=#PB_Event_Gadget
      Select EventGadget()
        Case 2
          ClearList(Werte())
          For a=0 To 500
            AddElement(Werte())
            Werte()=Val(GetGadgetItemText(1,a,0))
          Next
          
          ClearGadgetItems(1)
          FirstElement(Werte())
          NextElement(werte())
          PreviousElement(werte())
          mysort(Werte())
          ForEach Werte()
            AddGadgetItem(1,-1,Str(werte()))
            DeleteElement(Werte())
          Next                                     
      EndSelect
    EndIf
  Until event=#WM_CLOSE
EndIf                   


Give Up everything but trying!