Results 1 to 9 of 9

Thread: Home Made sort code.

  1. #1

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Home Made sort code.

    This SEEMS to work, but I feel like i'm missing something.

    VB Code:
    1. Private Sub SortCollection(ByRef collectionname As EntryCollection)
    2.         For i As Integer = 1 To collectionname.Count
    3.             For j As Integer = 2 To collectionname.Count
    4.                 If CType(collectionname(j), Entry).Filesize >= CType(collectionname(j - 1), Entry).Filesize Then
    5.                     collectionname.Swap(j, j - 1)
    6.                 End If
    7.             Next
    8.         Next
    9. End Sub

    (and here is the swap function out of my EntryCollection Class)

    VB Code:
    1. Public Sub Swap(ByVal FirstMember As Integer, ByVal SecondMember As Integer)
    2.             Dim temp As Object = List(FirstMember - 1)
    3.             List(FirstMember - 1) = List(SecondMember - 1)
    4.             List(SecondMember - 1) = temp
    5. 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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
    1. For i As Integer = collectionname.Count To 2 Step -1
    2.             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.

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  4. #4

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  6. #6

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  7. #7

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  9. #9

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Home Made sort code.

    Damn it.

    OK, I'm dedicating this week to understanding interfaces better.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width