|
-
Nov 7th, 2005, 11:35 PM
#1
Thread Starter
Frenzied Member
Home Made sort code.
This SEEMS to work, but I feel like i'm missing something.
VB Code:
Private Sub SortCollection(ByRef collectionname As EntryCollection)
For i As Integer = 1 To collectionname.Count
For j As Integer = 2 To collectionname.Count
If CType(collectionname(j), Entry).Filesize >= CType(collectionname(j - 1), Entry).Filesize Then
collectionname.Swap(j, j - 1)
End If
Next
Next
End Sub
(and here is the swap function out of my EntryCollection Class)
VB Code:
Public Sub Swap(ByVal FirstMember As Integer, ByVal SecondMember As Integer)
Dim temp As Object = List(FirstMember - 1)
List(FirstMember - 1) = List(SecondMember - 1)
List(SecondMember - 1) = temp
End Sub
The type Entrycollection contains various enums and structures, the one being used in this case is the "Entry" type (which is where the Ctype statements come in)
Also, this seems like it would get REALLY slow with lots of members to sort, since it basically runs through the loop (number of members)^2 times.
Any other sorting methods that are faster, or theories in general?
Bill
-
Nov 8th, 2005, 07:07 AM
#2
Re: Home Made sort code.
Calling a Sub or function is slower than having the same code directly in your routine, but as it is in a class you may not want to change that.
Refering to a property/method is slower than refering to a variable, so you could try replacing the "collectionname.Count" for the loops with a variable which is set beforehand.
With the current method you will have the largest item at the end after the "j" loop has run, the next time you will have two items at the end - but you are still checking those items. This could be eliminated by using loops like this:
VB Code:
For i As Integer = collectionname.Count To 2 Step -1
For j As Integer = 2 To i
(assuming Step is available)
It could be a wise idea (depending on how well sorted the data is) to count how many items are Swapped using the J loop, if none then the job is done - so just exit the i loop, rather than doing unneeded work.
By the way, this is what is known as a Bubble sort - which isn't particularly good for 'unsorted' data.
-
Nov 8th, 2005, 09:26 AM
#3
Re: Home Made sort code.
Well you've taken the already slow "bubble sort" algorithm and tied lead weights to it and buried it in treacle j/k
Why are you sorting a collection?
I don't live here any more.
-
Nov 8th, 2005, 04:11 PM
#4
Thread Starter
Frenzied Member
Re: Home Made sort code.
Well, it's an inherited class from CollectionBase, which, it seems works more like an array (and is zero based internally). Basically, consider it an array of a custom type that is simply a structure, containing Filesize, filename, and a few other properties that I need elsewhere in the program. For display purposes, I want to sort these items by filesize.
It's funny that the way to sort things that I came up with in my head has a name and is considered crap What's a better sorting algorithm to use? I will definately go with the small optimizations SI mentioned, with the exception that eventually I'll be moving the sort method into the class (so that I can just call CustomCollectionName.sort to prepare data for displaying.) However, I can probably put the swap function inline with the sorting routine, that makes sense.
Bill
-
Nov 8th, 2005, 05:02 PM
#5
Re: Home Made sort code.
There are dozens of 'popular' sorting algorithms, and I am certainly no expert (I usually let SQL Server do the actual sorting!). There are several examples on the forums you can probably learn from tho, such as these:
http://www.vbforums.com/showthread.php?t=232425
http://www.vbforums.com/showthread.php?t=231925
-
Nov 8th, 2005, 06:46 PM
#6
Thread Starter
Frenzied Member
Re: Home Made sort code.
Hmm, thanks for those links.. I will have to dig a bit through the VB6 code to understand it (All these "Object" variables ). but, the math seems to make sense, and I'll get the concept.
Bill
-
Nov 9th, 2005, 02:37 AM
#7
Thread Starter
Frenzied Member
Re: Home Made sort code.
I did some research and found the Quicksort algorithm described on several sites, and made a .NET version of it to put in the codebank. it's roughly one zillion times faster than my bubble sort above.
<-- no longer a sorting n00b.
Bill
-
Nov 9th, 2005, 08:51 AM
#8
Re: Home Made sort code.
Sorry, you may not be able to revoke your noobdom just yet. There is no need to code the sorting yourself, just implement the IComparable in your objects and then you can use Array.Sort() on your internal array.
I don't live here any more.
-
Nov 9th, 2005, 09:15 PM
#9
Thread Starter
Frenzied Member
Re: Home Made sort code.
Damn it.
OK, I'm dedicating this week to understanding interfaces better.
Bill
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|