Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

SelectionSort - Short, simple and sloooow

Total Hit ( 2285)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' SelectionSort. Short, simple and sloooow. This is another O(N^2) sort that
' should never be used on long lists. Like InsertionSort,
' it needs no extra memory (in place) and preserves the existing order of
' records with equal keys. Unfortunately, it's slower,
' and there's no reason to use it instead of InsertionSort. Works by looking
' thru the list for the smallest key and placing it first,
' then looking for the next largest, and so on. If the slow linear search for
' each key is replaced by a faster "priority queue" approach,
' a reasonably fast algorithm can be created (see HeapSort). Two versions are
' given. pSelSortS is an indirect (pointerized) version for strings,
' which can be adapted to doubles by changing the declaration of A(). 
' SelSortL is a direct version for longs, which can be adapted to integers.

' Speed: Don't ask.
'
' Bottom line: Included because it's a clear and simple approach,
' but too slow to be useful.

' Usage: 

Dim S1(L To R) As String
Dim P1(L To R) As Long
Dim L1(L To R) As Long

For I = L To R
  S1(I) = GetRandomString()
  P1(I) = I
  L1(I) = GetRandomLong()
Next I

pSelSortS L, R, S1, P1
SelSortL L, R, L1

' CODE:

Sub pSelSortS(L As Long, R As Long, A() As String, P() As Long)
  Dim I As Long
  Dim J As Long
  Dim Min As Long
  Dim TMP As Long

  For I = L To R - 2
  'Start with the first key.
    Min = I
  'Compare to each key to the right
    For J = I + 1 To R
    'Every time we find a lower value, keep that pointer.
      If A(P(J)) < A(P(Min)) Then
        Min = J
      End If
    Next J
  'When we've scanned all keys, swap the low key's pointer to the end of the
  ' sorted list.
    TMP = P(I)
    P(I) = P(Min)
    P(Min) = TMP
  Next I
End Sub

Sub SelSortL(L As Long, R As Long, A() As Long)
  Dim I As Long
  Dim J As Long
  Dim Min As Long
  Dim TMP As Long

  For I = L To R - 2
    Min = I
    For J = I + 1 To R
      If A(J) < A(Min) Then
        Min = J
      End If
    Next J
    TMP = A(I)
    A(I) = A(Min)
    A(Min) = TMP
  Next I
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.