posted by Mark on .
Sort an array of ten 30 character items into ascending order of surname. Each line contains a name in the form e.g. Mr J Smith, Dr John R Smith or John Smith. There are no field or record delimiters, qualifications, suffixes, double-barrelled names or similar complications. Define names and values for the items in the array, using any programming language you choose. Using low level string and record manipulation and not higher level functions such as SORT, either draw a flowchart to show how you would perform the sort, or write the program.
Most computer languages have a command that assigns a numerical value to a character variable, such as VAL($A) in Visual Basic.
I used to assign such a problem as you stated and found the "bubble method" to be the easiest to use for most students.
Assign your data into an array of 30 subscripted string variables.
Assign the first string variable as $large.
Set up a loop that runs from 1 to 30, or n, comparing each item to $large.
if the item is less than or equal to VAL($large) keep going in the loop, if the item is greater then assign its value to $large. Keep track of where you found the largest with a variable.
At the end of the first loop, the largest value,(the highest name in your alpha list) will be exchanged with the last entry, and that last entry will be placed in the position where $large was found.
Repeat the loop from 1 to 29, or n-1, the second largest will now be in the second last position etc.