* * *

Author Topic: How to perform a bubble sort on a list box (of names)  (Read 1737 times)

tinytim33

  • Jr. Member
  • **
  • Posts: 55
  • Looking for Slick Mode
How to perform a bubble sort on a list box (of names)
« on: February 16, 2013, 10:01:33 pm »
Hi,
I've created a listbox and am trying to use a bubble sort to output the list of names in sorted order.
I've tried various methods on the listbox to get the 'names' entered... but not compiling.

Code: [Select]
procedure TForm1.btnSortClick(Sender: TObject);
var NoMoreSwaps:boolean;
Temp:string;
count:integer;
begin
  Repeat
    For count:= 1 to lstName.items.count - 1
      DO
        If lstName.items.Names[count] > lstName.items.names[count+1]
          THen
            Begin
              NoMoreSwaps:=false;
              Temp:=lstName.items.Names[count];
              lstName.items.names[count]:=lstName.items.names[count+1];
              lstName.items.names[count + 1]:=temp;
            End;
  Until NoMoreSwaps = true;
end;

end.
   

I'm guessing lstName.items.names  is the wrong method to access a value in the listbox. 
Any ideas?
thanks, tiny

Blaazen

  • Hero Member
  • *****
  • Posts: 2013
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to perform a bubble sort on a list box (of names)
« Reply #1 on: February 16, 2013, 10:13:03 pm »
Try simply
Code: [Select]
lstName.items[count]
Lazarus 1.3 r46997M FPC 2.7.1 r28729 x86_64-linux-qt Chakra 2013.5, Qt 4.8.6, KDE4.14.2
Lazarus 1.2.6 r46529 FPC 2.6.4 i386-win32-win32/win64 Wine 1.7.31
Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1878
Re: How to perform a bubble sort on a list box (of names)
« Reply #2 on: February 16, 2013, 10:15:10 pm »
Is there a reason you want a bubblesort?

To sort the ListBox, it is enough to write

ListBox1.Sorted:=true; 

tinytim33

  • Jr. Member
  • **
  • Posts: 55
  • Looking for Slick Mode
Re: How to perform a bubble sort on a list box (of names)
« Reply #3 on: February 16, 2013, 10:20:54 pm »
Hi THanks,

It still won't compile for me.  Error of type  '' Out of Bounds ". ?

Blaazen

  • Hero Member
  • *****
  • Posts: 2013
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to perform a bubble sort on a list box (of names)
« Reply #4 on: February 16, 2013, 10:45:50 pm »
Code: [Select]
count:integer;
begin
  Repeat
    For count:= 1 to lstName.items.count - 1
      DO
        If lstName.items.Names[count] > lstName.items.names[count+1]
Reason is here. Maximal index can be "items.count-1". And you try to read ...items[count+1] which is in fact items[items.count].

Also, choose another name for indexing variable, "i" is enough. When its name is "count", it is difficult to explain where is the problem.  :)
Lazarus 1.3 r46997M FPC 2.7.1 r28729 x86_64-linux-qt Chakra 2013.5, Qt 4.8.6, KDE4.14.2
Lazarus 1.2.6 r46529 FPC 2.6.4 i386-win32-win32/win64 Wine 1.7.31
Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

tinytim33

  • Jr. Member
  • **
  • Posts: 55
  • Looking for Slick Mode
Re: How to perform a bubble sort on a list box (of names)
« Reply #5 on: February 16, 2013, 10:59:22 pm »
then

for count := 1 to max no. of items in list box?


Blaazen

  • Hero Member
  • *****
  • Posts: 2013
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to perform a bubble sort on a list box (of names)
« Reply #6 on: February 16, 2013, 11:02:52 pm »
No. I mean:
Code: [Select]
var i: integer;
begin
  Repeat
    For i:= 1 to lstName.items.count - 1 do
....
Lazarus 1.3 r46997M FPC 2.7.1 r28729 x86_64-linux-qt Chakra 2013.5, Qt 4.8.6, KDE4.14.2
Lazarus 1.2.6 r46529 FPC 2.6.4 i386-win32-win32/win64 Wine 1.7.31
Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

User137

  • Hero Member
  • *****
  • Posts: 1408
    • Nxpascal home
Re: How to perform a bubble sort on a list box (of names)
« Reply #7 on: February 16, 2013, 11:36:54 pm »
Many arrays, such as listbox items start from 0, not 1. For example array which item count is 10, would go from 0..9.
Win7, FPC and Lazarus 32-bit SVN version.

tinytim33

  • Jr. Member
  • **
  • Posts: 55
  • Looking for Slick Mode
Re: How to perform a bubble sort on a list box (of names)
« Reply #8 on: February 16, 2013, 11:38:19 pm »
Am still getting lots of compile errors...but will try and source bug. Thanks for that...that helps me and makes sense. Will b back soon probably....
thanks, tiny

Scoops

  • Jr. Member
  • **
  • Posts: 65
Re: How to perform a bubble sort on a list box (of names)
« Reply #9 on: February 17, 2013, 08:01:45 am »
Code: [Select]
procedure TForm1.btnSortClick(Sender: TObject);
var NoMoreSwaps:boolean;
Temp:string;
count:integer;
begin
  Repeat
   NoMoreSwaps:=true;
  For count:= 0 to lstName.items.count - 2
     DO
       If lstName.items [count] > lstName.items [count+1]
         THen
           Begin
              NoMoreSwaps:=false;
              Temp:=lstName.items [count];
              lstName.items [count]:=lstName.items [count+1];
              lstName.items [count + 1]:=temp;
            End;
  Until NoMoreSwaps = true;
end;


 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus