Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Today's Posts
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 12-03-2005, 06:21 PM   #1
Koen
Game Wizzard
 
Koen's Avatar

 
Join Date: Nov 2004
Location: Nootdorp, Netherlands
Posts: 226
Default

I'm looking for a fast sorting algorythm to alphabetically sort a list from A to Z.

Code:
Dim Title() as String, NumOfTitles as Long
Dim Temp as String
Dim I as Long, J as Long
This algorythm I always use is too slow, can anyone write a better (optimized) algorythm?

Code:
For I = 1 To NumOfTitles
 *For J = 1 To NumOfTitles
 * *If LCase(Title(I)) < LCase(Title(J)) Then
 * * *Temp = Title(I)
 * * *Title(I) = Title(J)
 * * *Title(J) = Temp
 * *End If
 *Next J
Next I
BTW, is there a function left like the old QB's Swap(String1, String2)?
Koen is offline                         Send a private message to Koen
Reply With Quote
Old 12-03-2005, 06:52 PM   #2
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

Did you try "Quick Sort"
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Old 12-03-2005, 07:24 PM   #3
Pangloth
Newbie

 
Join Date: Mar 2005
Location: ,
Posts: 10
Send a message via MSN to Pangloth
Default

Koen, look at your code. You have an outer loop on I and an inner loop on J, both of which run from 1 to the size of your input. Then you have a compare and 3 assignments. These don't matter, but the two nested loops give your code a time complexity of O(n^2), which means that for any input, your algorithm will take the quadratic of the input to complete. This is very bad, save for the smallest of inputs.

You might want to consider implementing Quicksort, which has an average case of O(n log(n)), and a worst case of O(n^2), but this can be made exponentially unlikely with a well chosen pivoting strategy.

You should consult a good book on algorithms and data structures. If your inputs will never be too big, you might also consider Shellsort or Mergesort.

I hope this helps.

EDIT: Recursive algorithms are not very efficient for small datasets due to the cost of recursion, so you might want to switch to a nonrecursive strategy to sort the last 20 - 30 elements in your set.
Pangloth is offline                         Send a private message to Pangloth
Reply With Quote
Old 12-03-2005, 07:29 PM   #4
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

Sorting algorithms. There's Quick Sort in there, with a full explanation and example sourcecode. Besides the Quick Sort algorithm, there're all the others, too.
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 12-03-2005, 07:42 PM   #5
Pangloth
Newbie

 
Join Date: Mar 2005
Location: ,
Posts: 10
Send a message via MSN to Pangloth
Default

Kon-Tiki, the example Quicksort source code you link to uses the leftmost element as the pivot, which could lead to disastrous results.

Imagine feeding sorted input to the algorithm. Then, because of the partitioning strategy, all elements will end up in the same half of the array. Worse, this happens consistently through the recursion, so the algorithm will take quadratic time to do nothing at all! Furthermore, input with largely sorted parts can be quite frequent in real life. This implementation is not recommended.

Median-of-three partitioning should be used instead.

That's why not having some understanding of what's going on can actually harm you.
Pangloth is offline                         Send a private message to Pangloth
Reply With Quote
Old 12-03-2005, 09:18 PM   #6
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

WOW............................................... .................yes.............................. :blink:
Admit Koen that you wasn't expecting this.
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Old 13-03-2005, 01:51 AM   #7
taikara
Abandonia Homie

 
Join Date: Jan 2005
Location: Shella, Kenya
Posts: 710
Default

I am such a geek.

Once, in university, I was working on coding some stupid exercise or another, and my friend was sitting next to me, working on coding a graph that visually displayed the various sorting algorithm efficiencies...

He turned to me and said, "Something's just not right. What do you think?"

I looked at the screen, and the graph was most definitely not displaying correctly. Lines were going out of the margins, were not behaving according to the actual algorithms, etc.

I turned to my friend with a straight face and said:

"Well... it's a graph... of sorts..."

(If you get this, and laugh, you deserve to be locked up just as much as I do.)

Edit: Sorry about the ness... it's been one of those nights, and I couldn't resist
taikara is offline                         Send a private message to taikara
Reply With Quote
Old 13-03-2005, 08:17 AM   #8
Koen
Game Wizzard
 
Koen's Avatar

 
Join Date: Nov 2004
Location: Nootdorp, Netherlands
Posts: 226
Default

Quote:
Originally posted by Kon-Tiki@Mar 12 2005, 09:29 PM
Sorting algorithms. There's Quick Sort in there, with a full explanation and example sourcecode. Besides the Quick Sort algorithm, there're all the others, too.
The link doesn't work.

I'm using it for my random mp3 player to sort all files alphabetically. This can be on title, artist, album, key or bpm. The mp3 player was loading a bit slow, and in fact the only reason was that it was sorting all files, and I didn't expect that to work so slow...

Thanks for the reactions k: , I'll look for quicksort and see how well that's performing.

EDIT.
Found and example in VB, and for those who want to know it too:
http://www.vba-programmer.com/Word_Code/Qu...e_Dimension.txt
Koen is offline                         Send a private message to Koen
Reply With Quote
Old 13-03-2005, 04:53 PM   #9
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

It's strange that that link doesn't work. Just tested it again, and it does work here.
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Reply With Quote
Old 13-03-2005, 05:38 PM   #10
NrmMyth
Hero Gamer

 
Join Date: Jan 2005
Location: ,
Posts: 454
Default

Works for me to.

I could save the page so you could download it if you still can't open the link. k:
__________________
Never mess with me when I have a cougar, Never!
NrmMyth is offline                         Send a private message to NrmMyth
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Game Sorting Sugestion.. LingonKungen Old Suggestions 9 13-06-2007 06:41 AM
Fastest Ever Beated A Game? Mr.Snuggles Gaming Zone 46 14-11-2005 11:08 PM
Sorting Options eric10051981 Old Suggestions 1 17-06-2004 09:18 AM


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 07:34 PM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.