Insertion Sort for Linked Lists

Just starting out? Need help? Post your questions and find answers here.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Insertion Sort for Linked Lists

Post by Little John »

Hi,

I have written a small InsertionSort() procedure for linked lists.
It seems to work fine. However, I'd like to ask whether anyone sees a bug or other issues.
Any suggestions for improvement are welcome.

P.S.: Please don't tell me about other sorting algorithms. I am aware of them, but this topic is specifically about Insertion Sort for Linked Lists.

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
Last edited by Little John on Fri Oct 12, 2018 8:18 am, edited 1 time in total.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Insertion Sort for Linked Lists

Post by davido »

@Little John,
Thank you.
I've checked it on my MacBook.
It works perfectly, as I expected.

As you don't usually post code without good reason, can you, please, explain in what situations this sort would be beneficial?
DE AA EB
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Insertion Sort for Linked Lists

Post by Fig »

I didn't know "MoveElement()". Thank you :D
Usefull for lists almost already sorted ...
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Insertion Sort for Linked Lists

Post by NicTheQuick »

There is also PushListElement() and PopListElement(). Maybe you can get rid of ChangeCurrentElement() this way. But I don't think that this will make anything better. I wanted just let you know about these commands.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Insertion Sort for Linked Lists

Post by Little John »

Hi, thanks for your replies!
davido wrote:As you don't usually post code without good reason, can you, please, explain in what situations this sort would be beneficial?
It looks like we know each other already for a while. :D
Davido, thank you for testing the code!
Insertion Sort is efficient for lists (or arrays) that are already almost sorted, and also for any small lists/arrays. These kinds of lists/arrays often occur in practice, and they can also be intermediate results of more advanced sorting algorithms (such as Quick Sort or Merge Sort). I'm currently working on a hybrid sorting algorithm for Linked Lists, where the main work is done by Merge Sort, and small lists are sorted by Insertion Sort. I hope that this will be faster than using Merge Sort alone. Since both Insertion Sort and Merge Sort are stable (when implemented the normal way), the complete hybrid sorting algorithm will be stable, too.
NicTheQuick wrote:There is also PushListElement() and PopListElement().
I'm aware of these commands, thank you! When I use them instead of ChangeCurrentElement(), my test code runs at the same speed.
User avatar
skywalk
Addict
Addict
Posts: 3999
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Insertion Sort for Linked Lists

Post by skywalk »

freak said the internal PB sorts for lists and structured lists are stable.
Are you trying to improve on their speed or have you found them not to be stable?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Insertion Sort for Linked Lists

Post by Little John »

skywalk wrote:Are you trying to improve on their speed or have you found them not to be stable?
Neither.
The main problem with PB's built-in sorting functions is, that there are several situations in which they are not applicable. For instance, we can't even use them for sorting a string list accorting to the lengths of its strings. As long as PureBasic does not have built-in "Custom Sort" functions for lists (and arrays), self-written powerful sorting routines are needed. There are already several examples here on the forum.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Insertion Sort for Linked Lists

Post by Josh »

Little John wrote:The main problem with PB's built-in sorting functions is, that there are several situations in which they are not applicable.
An external sort function could also be interesting for sorting pointerlists, because
Pb does not support that.
sorry for my bad english
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Insertion Sort for Linked Lists

Post by Little John »

Made the code in the first post a bit faster and a bit better readable.
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: Insertion Sort for Linked Lists

Post by Derren »

Did you speed-test this?
I tried something like that once, hoping it would be fastest method. But in the end, just appending an element and then using SortList() was much faster.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Insertion Sort for Linked Lists

Post by Little John »

Derren wrote:Did you speed-test this?
Little John wrote:I'm currently working on a hybrid sorting algorithm for Linked Lists, where the main work is done by Merge Sort, and small lists are sorted by Insertion Sort. I hope that this will be faster than using Merge Sort alone.
Yes, of course I'm doing speed tests. The results depend on some details, but so far they are very promising.
Derren wrote:I tried something like that once, hoping it would be fastest method. But in the end, just appending an element and then using SortList() was much faster.
My InsertionSort() is not meant as replacement for PureBasic's built-in SortList().
As I wrote, there are several situations in which PB's built-in sorting functions are not applicable at all. That's why self-written powerful sorting routines are needed.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Insertion Sort for Linked Lists

Post by davido »

@Little John,
Thank you, very much, for the explanation.
Always nice to learn something new. :D
DE AA EB
Post Reply