Lazarus

Programming => General => Topic started by: tinytim33 on February 16, 2013, 10:01:33 pm

Title: How to perform a bubble sort on a list box (of names)
Post by: tinytim33 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
Title: Re: How to perform a bubble sort on a list box (of names)
Post by: Blaazen on February 16, 2013, 10:13:03 pm
Try simply
Code: [Select]
lstName.items[count]
Title: Re: How to perform a bubble sort on a list box (of names)
Post by: theo 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; 
Title: Re: How to perform a bubble sort on a list box (of names)
Post by: tinytim33 on February 16, 2013, 10:20:54 pm
Hi THanks,

It still won't compile for me.  Error of type  '' Out of Bounds ". ?
Title: Re: How to perform a bubble sort on a list box (of names)
Post by: Blaazen 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.  :)
Title: Re: How to perform a bubble sort on a list box (of names)
Post by: tinytim33 on February 16, 2013, 10:59:22 pm
then

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

Title: Re: How to perform a bubble sort on a list box (of names)
Post by: Blaazen 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
....
Title: Re: How to perform a bubble sort on a list box (of names)
Post by: User137 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.
Title: Re: How to perform a bubble sort on a list box (of names)
Post by: tinytim33 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
Title: Re: How to perform a bubble sort on a list box (of names)
Post by: Scoops 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