Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Alphabetical Order

Author
Message
Sinani201
19
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 2nd Jan 2009 19:10 Edited at: 2nd Jan 2009 19:11
Does anyone know how to sort strings into alphabetical order? I think I saw TDK put up a code snippet some other time, but when I did a search, all I got were DBPro snippets.

So... ummm... can someone help me? Thanks.


"I reveal my trap card, GEORGE DUBYA BUSH!
America loses 2000 Life Points! America loses." -Deucalion2
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 2nd Jan 2009 19:56 Edited at: 2nd Jan 2009 19:58
This example was taken out of my FileScan Function so the vaiable names are as is.



You can manually place in a string name instead of using that Generating Random String loop.

The SortFileNames subroutine will check through each filename in the list two at a time and check each string characters. If the 2nd filename which is further down the list has a lower ascii value, the two filenames swaps places. If any filenames are changed within the loop, the loop is forced to start over from the beginning until the loop runs through without having to swap any filenames in the list.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Jan 2009 01:15 Edited at: 3rd Jan 2009 01:15
a couple of tips:

Look up Select Sort on the internet. Much faster and efficient than a bubble sort.

Also, you can compare whole strings without looping through individual characters:



This compares the entire strings by ascii values. So you don't get strings with capitals sorting before lowercase, like Ashingda demonstrated, use upper$()



Enjoy your day.
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 3rd Jan 2009 01:41
Quote: "Also, you can compare whole strings without looping through individual characters:"

You're kidding... This is awsome! You the bomb Latch!
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 3rd Jan 2009 05:40 Edited at: 3rd Jan 2009 08:24
Ok I updated my bubble sort and tested it's speed against a select sort and there's a Super Large difference. BTW, my old bubble was horribly slow!


[edit]

hmm.. I'm trying to get a comb sort to work but it's not working right.

Ok got Comb Sort working and ready for testing.

I added Insertion sort, I dont know if I did it totally correct but it seems to work. So far SelectSort is the fastest one.




Quick Sort is rather tempting to try, but I don't think I'm mentally prepared for that yet. Has anyone coded a Quick Sort in DBC?
I'm looking at the pseudocode and it's carrying arrays into functions and returning it, we can't do that with DBC can we?
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 3rd Jan 2009 17:26 Edited at: 3rd Jan 2009 17:35
Ok I managed to make a Quick Sort, it's not as hard as I first thought. It was confusing at first cause the function would call itself, although I'm not completly new to that method I've always thought it was funny.

Quick Sort is blazing fast! I did a comparison between the different sorting algorithm I got so far, also found a speed difference amongst different machines, although not by much.

The Comb Sort is suprisingly stable.


These are the results I got from my laptop.

Random filenames 500


Random filenames 1000


Random filenames 2000


Random filenames 3000



Here's the codes.
Sinani201
19
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 3rd Jan 2009 20:04
@Ashingda (last post you made thus far)
Your first code was good... but how am I supposed to use the last one you made? There's a lot of codes. I don't know where to input the strings.

@Latch
Is Select Sort supposed to be SQL select sort? That's all I got by Googling it.

I'm not so concerned about time. As long as it takes up to 10 seconds, that should be good enough.


"I reveal my trap card, GEORGE DUBYA BUSH!
America loses 2000 Life Points! America loses." -Deucalion2
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Jan 2009 22:52
Quote: "Is Select Sort supposed to be SQL select sort? That's all I got by Googling it."


The algorithm I'm talking about is general. I mention it because it's easy to implement in just about any programming language and it's generally pretty fast. Ashingda has an example of it in his post about 4 posts up. He listed a bunch of different sorting methods so you can choose which one you like best.

Enjoy your day.
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 4th Jan 2009 01:47 Edited at: 4th Jan 2009 01:49
@Sinani
To use the Quick Sort the Keys are:
ArrayName$() - The list pretty much.
IndexUsed - The amount of Names to sort in the list.

To call the function:
QuickSort(0,IndexUsed) - The 1st parameter is the Left end of the partition and the 2nd is the Right end of the partition.


You'll have to change the Array and Variable names to adapt to your own use.


For more info, search "Quick Sort Wiki". It also has a visual animation to help understand too . While you're at it there's alot of other algorithms there, I'm going to continue learning them just for kicks.
Sinani201
19
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 4th Jan 2009 23:05
Err... OK. Thanks guys. I'll be on now...


"I reveal my trap card, GEORGE DUBYA BUSH!
America loses 2000 Life Points! America loses." -Deucalion2
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 6th Jan 2009 19:56
I select each string in turn and compare it to the others and keep a tally of how many strings it is greater than, then I place the string in a separate sorting array (needed so you don't overwrite strings).
Something like that its a while since I did it.
Is that a select sort?


Goke of the day: [Q]What's a coders' favourite part of an orchestra?
[A]The strings.
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 6th Jan 2009 20:35 Edited at: 6th Jan 2009 20:36
Seems the Select Sort algorithm would be to scan through the list from (x) "x = 0" to (end of list) and find the lowest value and swap it's position with the string in the (x) position. Then repeats it with a "x = x + 1" or "next x" etc.

As it progress the scan loops becomes significantly shorter.

This was taken from the wikipedia that show's a great example of how it's sorted.

Login to post a reply

Server time is: 2026-07-05 01:47:47
Your offset time is: 2026-07-05 01:47:47