By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

How do I alphabetize words?

P: 6
This is what I was given:
The number of passes is one less than the number of items. In the example below, 5 names are being sorted
therefore 4 passes will be required to complete the sort.
The bubble sort compares the 1st and 2nd values and swaps them if not in order, it then compares the 2nd and 3rd
values…..continuing the process until the next-to-last and last values are compared. After one pass the bottom
value is in it’s correct position and will not have to be swapped. The next pass will not have to check the
bottom value – each successive pass will have one less comparison to perform.

Names listed below should be initialized in an array and sorted using a bubble sort.
Initialize your array with the following names and have your program display the sorted list of names.
“Joe”,”Mary”,”Sam”,”Tim”,”Ben”, “Harry”, “Michael”, Anne”, “Kim”, “Lebron”, “Duane”, “Jeff”, “Julie”,
“Karen”, “Kristin”
Initialize array
Dim name( ) as string = {“Joe”,”Mary”,……………..}

So far I've assigned the char to a number
ex: If names(n).char(x) = "a" then
which I used to compare to the same thing for another word
ex: If value1>value2 then
but from there on I can't figure out how to switch the words to go in order
Feb 15 '16 #1
Share this Question
Share on Google+
1 Reply

Expert 5K+
P: 8,619
Here is a more efficient Algorithm for you. The difference would be negligible for small Arrays, but for larger ones it should be quite significant.
  1. Sorting Algorithm:
    Expand|Select|Wrap|Line Numbers
    1. Public Sub SortStringArray(varArray As Variant, intLeft As Integer, intRight As Integer)
    2. Dim i As Integer
    3. Dim j As Integer
    4. Dim varTestVal As Variant
    5. Dim intMid As Integer
    6. Dim varTemp As Variant
    8. If intLeft < intRight Then
    9.   i = intLeft
    10.   j = intRight
    11.   intMid = (intLeft + intRight) \ 2
    12.   varTestVal = varArray(intMid)
    13.     Do
    14.       Do While varArray(i) < varTestVal
    15.         i = i + 1
    16.       Loop
    17.       Do While varArray(j) > varTestVal
    18.         j = j - 1
    19.       Loop
    20.         If i <= j Then
    21.           '----- Swap Elements -----
    22.           varTemp = varArray(j)
    23.           varArray(j) = varArray(i)
    24.           varArray(i) = varTemp
    25.             i = i + 1
    26.             j = j - 1
    27.         End If
    28.     Loop Until i > j
    30.     'To optimize the sort, always sort the
    31.     'smallest segment first.
    32.     If j <= intMid Then
    33.       SortStringArray varArray, intLeft, j
    34.       SortStringArray varArray, i, intRight
    35.     Else
    36.       SortStringArray varArray, i, intRight
    37.       SortStringArray varArray, intLeft, j
    38.     End If
    39. End If
    40. End Sub
  2. Sample Code representing
    1. Initializing of the Array.
    2. Passing the Array along with the Lower and Upper Bounds to the Sorting Logic.
    3. With the Sorted Array now returned, reiterate the Array to conform the results.
    Expand|Select|Wrap|Line Numbers
    1. Dim intCtr As Integer
    3. Dim avarNames As Variant
    5. avarNames = Array("Joe", "Mary", "Sam", "Tim", "Ben", "Harry", "Michael", "Anne", "Kim", "Lebron", "Duane", "Jeff", "Julie", "Karen", "Kristin")
    7. Call SortStringArray(avarNames, LBound(avarNames), UBound(avarNames))
    9. 'Sorted Array now contained in avarNames
    10. For intCtr = LBound(avarNames) To UBound(avarNames)
    11.   Debug.Print avarNames(intCtr)
    12. Next
  3. Playback Results:
    Expand|Select|Wrap|Line Numbers
    1. Anne
    2. Ben
    3. Duane
    4. Harry
    5. Jeff
    6. Joe
    7. Julie
    8. Karen
    9. Kim
    10. Kristin
    11. Lebron
    12. Mary
    13. Michael
    14. Sam
    15. Tim
Feb 15 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.