Recent

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

tinytim33

  • Jr. Member
  • **
  • Posts: 61
  • 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: 3237
  • 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 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
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: 61
  • 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: 3237
  • 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 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

tinytim33

  • Jr. Member
  • **
  • Posts: 61
  • 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: 3237
  • 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 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

User137

  • Hero Member
  • *****
  • Posts: 1791
    • 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.

tinytim33

  • Jr. Member
  • **
  • Posts: 61
  • 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

  • Full Member
  • ***
  • Posts: 100
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;


 

TinyPortal © 2005-2018