5 Replies - 5673 Views - Last Post: 14 February 2014 - 11:29 AM

#1 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 664
  • View blog
  • Posts: 2,931
  • Joined: 03-December 12

Sort three names without using an array

Posted 20 October 2013 - 08:36 AM

After reading a topic in the general board on this subject I started working on a solution myself the hardest part I have to admit was thinking through the logic process of doing the app. Huge If statements were shown to the OP and I thought there had to be a better way. Rules are no arrays for the sorting of values, ( I used an array for the entry and selection of names because I got tired of coming up with names and typing them in individually and having to randomize them myself.)


So, here is what I came up with...
Spoiler



Anyone else?

Is This A Good Question/Topic? 0
  • +

Replies To: Sort three names without using an array

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3448
  • View blog
  • Posts: 11,693
  • Joined: 12-December 12

Re: Sort three names without using an array

Posted 20 October 2013 - 10:20 AM

It shouldn't require more than 3 comparisons (cf Bubble Sort).

Module Module1

    Sub Main()
        Dim sOrig1 As String = "Ted"
        Dim sOrig2 As String = "Bob"
        Dim sOrig3 As String = "Mary"
        Dim sTemp As String = String.Empty
        Dim sFirst, sSecond, sThird As String

        If String.Compare(sOrig1, sOrig2, True) > 0 Then
            'first is greater than the second, swap them (effectively)
            sFirst = sOrig2
            sSecond = sOrig1
        Else
            sFirst = sOrig1
            sSecond = sOrig2
        End If
        If String.Compare(sSecond, sOrig3, True) > 0 Then
            sThird = sSecond
            sSecond = sOrig3
        Else
            sThird = sOrig3
        End If
        If String.Compare(sFirst, sSecond, True) > 0 Then
            sTemp = sFirst
            sFirst = sSecond
            sSecond = sTemp
        End If
        Console.WriteLine("1: {0}", sFirst)
        Console.WriteLine("2: {0}", sSecond)
        Console.WriteLine("3: {0}", sThird)
        Console.ReadKey()
    End Sub

End Module

I haven't tested it thoroughly (erm, at all..). Should work though ;) Added: Tested a little bit, seems good.

I don't think that this qualifies (yet) as an Advanced Discussion though ;)

This post has been edited by andrewsw: 20 October 2013 - 10:20 AM

Was This Post Helpful? 1
  • +
  • -

#3 thecoat  Icon User is offline

  • D.I.C Regular

Reputation: 80
  • View blog
  • Posts: 287
  • Joined: 07-December 13

Re: Sort three names without using an array

Posted 07 December 2013 - 02:46 AM

I used the OP's list creation but replaced the sorting. This is an alternative way to approach the problem that is a little more extensible compared to a bunch of conditional statements. I added two more names to the list to illustrate how easy it would be to add more names to the list.

Module Module1
    Private objRandom As New System.Random( _
    CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

    Public s1, s2, s3, s4, s5 As String

    Sub Main()
        'Generate random names to test program

        Dim name() As String = {"tom", "dick", "harry", "adam", "Eve", "derek", "steve", "vic", "melissa", "brad", "stephanie", _
                                "ashley", "bobby", "margaret", "cloe", "karen", "brandon", "clair", "zoe", "sara", "lisa", _
                                "bill"}
        Dim intSortCount As Integer = 1

        s1 = name(GetRandomNumber(0, name.Length - 1))
        s2 = name(GetRandomNumber(0, name.Length - 1))
        s3 = name(GetRandomNumber(0, name.Length - 1))
        s4 = name(GetRandomNumber(0, name.Length - 1))
        s5 = name(GetRandomNumber(0, name.Length - 1))

        Do Until intSortCount = 0
            intSortCount = 0
            intSortCount += Compare(s1, s2)
            intSortCount += Compare(s2, s3)
            intSortCount += Compare(s3, s4)
            intSortCount += Compare(s4, s5)
        Loop

        Console.WriteLine("1 = {0}", s1)
        Console.WriteLine("2 = {0}", s2)
        Console.WriteLine("3 = {0}", s3)
        Console.WriteLine("4 = {0}", s4)
        Console.WriteLine("5 = {0}", s5)
        Console.ReadKey()
    End Sub

    'Generate random order of names to insert into name values
    Public Function GetRandomNumber( _
        ByVal Low As Integer, _
        ByVal High As Integer) As Integer
        ' Returns a random number,
        ' between the optional Low and High parameters
        Return objRandom.Next(Low, High + 1)
    End Function

    'Note the "ByRef" in the function declaration causes the Function variables to reference the same memory address of the passed variable
    'So changing the value of strStringA and strStringB inside the function will change the original variables
    'Doing ByVal would result in an infinite loop in the main procedure if the variables were not already sorted.
    Private Function Compare(ByRef strStringA As String, ByRef strStringB As String) As Integer
        Dim stemp As String 'temporary container
        Select Case String.Compare(strStringA, strStringB, True)
            Case Is > 0 ' A > B swap them return 1
                stemp = strStringB
                strStringB = strStringA
                strStringA = stemp
                Return 1
            Case Else ' A < B  or A = B leave them return 0
                Return 0
        End Select
    End Function
End Module


Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Sort three names without using an array

Posted 13 February 2014 - 08:44 AM

Not all languages have a nice array.sort to do the work for them. this is perhaps a good reading if you want to learn about sorting algorithms though its in C++ the code is not that hard and they do use arrays.
Was This Post Helpful? 0
  • +
  • -

#5 thecoat  Icon User is offline

  • D.I.C Regular

Reputation: 80
  • View blog
  • Posts: 287
  • Joined: 07-December 13

Re: Sort three names without using an array

Posted 14 February 2014 - 11:25 AM

View Postraziel_, on 13 February 2014 - 08:44 AM, said:

Not all languages have a nice array.sort to do the work for them. this is perhaps a good reading if you want to learn about sorting algorithms though its in C++ the code is not that hard and they do use arrays.


Reminded me of this cool youtube video showing a graphical representation of some of the various sorting algorithms.
https://www.youtube....h?v=kPRA0W1kECg
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3448
  • View blog
  • Posts: 11,693
  • Joined: 12-December 12

Re: Sort three names without using an array

Posted 14 February 2014 - 11:29 AM

The visualizations here are much more informative ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1