Found an issue in SpiderBasic ? Please report it here !
kingwolf71
 
Posts: 21
Joined: Wed May 01, 2019 10:14 am

SortList() issue

by kingwolf71 Sat Feb 15, 2020 4:21 pm

This is LittleJohn (PB Forum) code for using insertion sort to sort an unsorted list. In PB works well. On SpiderBasic list remains the same - no code changes. What am I missing?

Code: Select all

; tested with PB 5.62

EnableExplicit


Procedure InsertionSort (List x.i())
   Protected.Integer *elementToMove, *precedingElement, *lastSortedElement
   
   *lastSortedElement = FirstElement(x())
   While NextElement(x())
      *elementToMove = @x()                             ; save pointer to current element
      *precedingElement = *lastSortedElement
      While *precedingElement <> #Null And
            *elementToMove\i < *precedingElement\i       ; use '>' if you want to sort descending
         *precedingElement = PreviousElement(x())
      Wend
     
      If *precedingElement <> *lastSortedElement         ; if current element needs to be moved
         ChangeCurrentElement(x(), *elementToMove)       ; restore current element
         If *precedingElement = #Null
            MoveElement(x(), #PB_List_First)
         Else
            MoveElement(x(), #PB_List_After, *precedingElement)
         EndIf
         ChangeCurrentElement(x(), *lastSortedElement)
      Else   
         *lastSortedElement = @x()   
      EndIf
   Wend
EndProcedure


Define i, n
NewList a()

n = 10

For i = 1 To n
   AddElement(a())
   a() = i
Next
RandomizeList(a())

Debug "-- Shuffled list:"
ForEach a()
   Debug a()
Next
Debug ""

InsertionSort(a())

Debug "-- Sorted ascending:"
ForEach a()
   Debug a()
Next




User avatar
Peter
 
Posts: 749
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080

Re: SortList() issue

by Peter Sat Feb 15, 2020 4:48 pm

why don't you use SortList()?

Code: Select all
SortList(a(), #PB_Sort_Ascending)


Greetings ... Peter
kingwolf71
 
Posts: 21
Joined: Wed May 01, 2019 10:14 am

Re: SortList() issue

by kingwolf71 Sat Feb 15, 2020 4:55 pm

I want to use it as a basis to make SortStructuredList()
User avatar
Peter
 
Posts: 749
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080

Re: SortList() issue

by Peter Sat Feb 15, 2020 6:49 pm

ChangeCurrentElement() and MoveElement() doesn't work in SB the way it does in PB.

They seem to me to be bugs.

Greetings ... Peter
MarkOtt
 
Posts: 14
Joined: Thu Sep 14, 2017 12:59 pm

Re: SortList() issue

by MarkOtt Wed Feb 19, 2020 2:58 pm

The bug with *element = @myList() seems to exist with unstructered lists only, structured lists are working well.

https://forums.spiderbasic.com/viewtopic.php?f=11&t=1839#p6857

Regards. Markus
Fred
Site Admin
 
Posts: 1298
Joined: Mon Feb 24, 2014 10:51 am

Re: SortList() issue

by Fred Tue May 19, 2020 8:06 am

Moved for investigation
Return to Bugs Reports

Who is online

Users browsing this forum: No registered users and 3 guests