BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to sort Lines in a StringList depending on their length

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Oren (Halvani.de)
Guest





PostPosted: Thu Jul 24, 2003 7:51 pm    Post subject: How to sort Lines in a StringList depending on their length Reply with quote



hi dear builders,

I got a question - is it possible to sort Lines in a StringList depandent
on their length ??

for example:

"Here is test.."
"Here is more text.."
"Here is even more text.."

Is this possible ??



Oren




Back to top
Hans Galema
Guest





PostPosted: Thu Jul 24, 2003 8:12 pm    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote



Oren (Halvani.de) wrote:

Quote:
I got a question - is it possible to sort Lines in a StringList depandent
on their length ??

"Here is test.."
"Here is more text.."
"Here is even more text.."

Traverse the StringList twice. Start with Sorted = false;

In the first pass add the length in front of the line.
It will then look like:


"0014 Here is test.."
"0019 Here is more text.."
"0024 Here is even more text.."

Now set Sorted to true.

In the second pass remove the numbers.

Hans.


Back to top
Oren (Halvani.de)
Guest





PostPosted: Thu Jul 24, 2003 8:14 pm    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote



Thanks Hans, good idea :-)

I'll try your suggestion..



Oren


Back to top
Rhys Sage
Guest





PostPosted: Thu Jul 24, 2003 8:15 pm    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote

Easy as pie.
Create 3 TStringLists.
The first contains your original data.
The second contains nothing.
The third is a temprorary list. (for speed)
run through List1 (your data)
put the index of the longest item into your temprorary list.
if it's longer, clear the list and put it into the list
At the end of the run, put the items listed from List one into List two and
clear the temprorary list. Also delete the items from List1
keep repeating the process until you have no data in List1 or the Temprorary
list

Easy peasy.

--
Yours,

Rhys Sage.
www.sageworld.freeserve.co.uk for
code snippets and software downloads.
--
Vini vidi vici
--
(Team Zip)



"Oren (Halvani.de)" <neroniker (AT) gmx (DOT) de> wrote

Quote:
hi dear builders,

I got a question - is it possible to sort Lines in a StringList depandent
on their length ??

for example:

"Here is test.."
"Here is more text.."
"Here is even more text.."

Is this possible ??



Oren








Back to top
Hans Galema
Guest





PostPosted: Thu Jul 24, 2003 8:33 pm    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote

Rhys Sage wrote:

Quote:
run through List1 (your data)
put the index of the longest item into your temprorary list.
if it's longer, clear the list and put it into the list

Dont understand that. A longer index ? Or do you mean a higher index ?
In the last sentence yoou use twice the word list. Which list's ?

Quote:
At the end of the run, put the items listed from List one into List two and
clear the temprorary list. Also delete the items from List1
keep repeating the process until you have no data in List1 or the Temprorary

Still don't understand the algorithm.
How many passes would this RhysBubbleSort take for 1000 lines ?

Hans.


Back to top
Rhys Sage
Guest





PostPosted: Thu Jul 24, 2003 9:51 pm    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote

It's not a bubble-sort. It's much faster than a bubble sort.
Example
List A starts with 1 2 3 4 3 2 23 23 12 23 23
The temprory list at the end of the first pass would have the indexes of all
the 23s as they're the highest values: thus 7,8,11,12
List B then has 23,23,23,23 added
List A has values 7,8,11,12 deleted.
Now ListA contains 1, 2, 3, 4,3,2,12

The process repeats with an ever-decreasing List length. I did it with a
TStringGrid and sorted 1500 items in less than a second (on a 166Mhz CPU).

I compared that with a true bubble-sort which took a minute or two. I'd say
my method works just fine.

--
Yours,

Rhys Sage.
www.sageworld.freeserve.co.uk for
code snippets and software downloads.
--
Vini vidi vici
--
(Team Zip)
"Hans Galema" <j.m.galema.dontusethis (AT) maartens (DOT) nl> wrote

Quote:
Rhys Sage wrote:

run through List1 (your data)
put the index of the longest item into your temprorary list.
if it's longer, clear the list and put it into the list

Dont understand that. A longer index ? Or do you mean a higher index ?
In the last sentence yoou use twice the word list. Which list's ?

At the end of the run, put the items listed from List one into List two
and
clear the temprorary list. Also delete the items from List1
keep repeating the process until you have no data in List1 or the
Temprorary

Still don't understand the algorithm.
How many passes would this RhysBubbleSort take for 1000 lines ?

Hans.




Back to top
JD
Guest





PostPosted: Thu Jul 24, 2003 11:37 pm    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote


Now now boys. Play nice.

Both methods require accessing the actual strings more than
once. I'll bet ... if you create an array that holds the
length and the index of that length, sort it and then use Move(),
that even a bubble-sort on the array will out perform both
suggested methods.

However, I doubt if one could devise a method more simple than
adding the length to the string and letting the list sort it's
self. Just a note on that method though: the length should
have more leading zeros.

~ JD

Back to top
Rhys Sage
Guest





PostPosted: Fri Jul 25, 2003 12:56 am    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote

An interesting alternative would be a tree sort.

--
Yours,

Rhys Sage.
www.sageworld.freeserve.co.uk for
code snippets and software downloads.
--
Vini vidi vici
--
(Team Zip)


Back to top
JD
Guest





PostPosted: Fri Jul 25, 2003 3:31 am    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote


"Rodolfo Frino" <rodolfofrino (AT) yahoo (DOT) com> wrote:
Quote:
or a sort of tree sort Smile

LOL!! Cute - real cute.

~ JD


Back to top
Hans Galema
Guest





PostPosted: Fri Jul 25, 2003 8:30 am    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote

JD wrote:
Quote:
Now now boys. Play nice.

Don't we ?

Quote:
Both methods require accessing the actual strings more than
once. I'll bet ... if you create an array that holds the
length and the index of that length, sort it and then use Move(),
that even a bubble-sort on the array will out perform both
suggested methods.


Using an array, quicksort and Move(), yes.
Using an array, bubblesort and Move(), I think yes to.
But that depends on the speed of Move().
But now that I think of it: every Move() would
alter the sequence of strings so after one Move()
you could not find the other strings on their index.
Unless you adjusted their index for that. Maybe not using
Move() and just ading to a second StringList ?

But please show the code.

Quote:
However, I doubt if one could devise a method more simple than
adding the length to the string and letting the list sort it's
self. Just a note on that method though: the length should
have more leading zeros.

With only three lines there were to much ;-)

Here are the results of ading the size, sorting and removing
on a 500Mz:

Size: 305658
Lines: 1973
Ading size: 46 ms.
Sorting: 74 ms.
Removing size: 11 ms.
Usedtime: 131 ms.

And here is the used code:

TStringList *StringList = new TStringList;
StringList->Sorted = false;
StringList->Text = RichEdit1->Text;

int starttickcount = GetTickCount();

int nlines = StringList->Count;
int linenr = -1;

while (++linenr < nlines )
{
AnsiString Line = StringList->Strings [linenr];

char linebuf [5000+2];

sprintf ( linebuf, "%06d %s",
Line.Length(), Line.c_str() );

StringList->Strings [linenr] = linebuf;
}

StringList->Sorted = true;
// This does the actual sorting
StringList->Sorted = false;

linenr = -1;

while (++linenr < nlines )
{
AnsiString Line = StringList->Strings [linenr];

StringList->Strings [linenr] = Line.SubString ( 8, 5000 );
}


int elapsedtime = GetTickCount() - starttickcount;
Memo1->Lines->Add
( "Elapsed time: " + IntToStr ( elapsedtime ) + " ms.");

RichEdit4->Clear();
RichEdit4->Text = StringList->Text;

delete StringList;

The credits for this speed go to the sorting algorithm of
the TStringList. Which will be a quick sort I think.

<Rys>
Quote:
The process repeats with an ever-decreasing List length. I did it with a
TStringGrid and sorted 1500 items in less than a second (on a 166Mhz CPU).
/Rys


With a StringGrid ? A GRID ? You said to use three StringLists.

With a cpu speed nearly four times slower and not using quicksort, that's
quite good I think.

Hans.


Back to top
Hans Galema
Guest





PostPosted: Fri Jul 25, 2003 8:34 am    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote

Hans Galema wrote:

Quote:
Rys

Sorry, <Rhys> !

Hans.


Back to top
Todd Brylski
Guest





PostPosted: Fri Jul 25, 2003 10:33 am    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote

"Oren (Halvani.de)" <neroniker (AT) gmx (DOT) de> wrote

Quote:
hi dear builders,

I got a question - is it possible to sort Lines in a StringList depandent
on their length ??

TStringList has a CustomSort method.
You can use it like this:

// define a custom sort function
int __fastcall SortByLength( TStringList* List, int Index1, int Index2 )
{
return List->Strings[Index1].Length() - List->Strings[Index2].Length();
}

void __fastcall TForm2::Sort1Click(TObject *Sender)
{
AStringList->CustomSort( SortByLength );
}

Todd



Back to top
Hans Galema
Guest





PostPosted: Fri Jul 25, 2003 1:42 pm    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote

Todd Brylski wrote:

Quote:
TStringList has a CustomSort method.
You can use it like this:

BCB3 does not implement that.

BCB5 does.

It took 27 ms instead of the 130 ms for the linesize ading as
mentioned in this thread already.

Thanks for the info and code.

Hans.


Back to top
Oren (Halvani.de)
Guest





PostPosted: Fri Jul 25, 2003 11:58 pm    Post subject: Re: How to sort Lines in a StringList depending on their len Reply with quote

Thanks for all the postings...
And a BIG Thanks to Hans Galema !! :-)


Oren


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.