Create a second array which contains indecies into your array of strings. For example:
ArrayList= "0hello", "1axe", "2help", "3nice", "4camper", "5ocean"
ArrayPntr= 0,1,2,3,4,5
Now as you test ArrayList use ArrayPntr to get at the values:
for i=0 to 5
if ArrayList[ArrayPntr[i]] .GT. ArrayList[ArrayPntr[i+1]]
swap only ArrayPntr[i] and ArrayPntr[i+1]
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36
Thanks for the fast response! That's a one way to do it but I was looking for something of the nature of traditional bubble sort. Comparing and swapping words next to each other and go through all the passes while there's a same character being compared, move on to the 2nd position of char and do sorting there. What I understand from the code you provided, it's comparing entire word to word, rather than individual characters of each positions-compare 1st char of words, if the char is the same, compare 2nd char and so on.
So in the standard BSort form, you need a K loop to look at individual characters. No biggie.
for i=0...
for j=i...
for k=.... ;; Loop through each character.
But, my way shows you a new and useful concept rather than simply adding to the known sort algorithm.
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36
You can use a modified bubble sort if you make two changes:
When you find a char that needs to be swapped, you move the entire row of char's (the whole word).
Instead of comparing one column's char with the column next to it, like the regular bubble sort, you need to compare char's in the same column, but in adjacent rows.
IOW's, you "tilt" the array, over by 90 degrees, onto it's face, and make your comparisons, that way.
The sort through an index logic that WaltP mentioned above, is a great trick for any programmer to have in their toolbox, btw. It allows you to do some amazing tricks, since it keeps the original order of the input, yet allows you to SHOW the input, as if it was sorted, perfectly well (fast, too!).
Indexing is a powerful tool, any time you have a bunch of data, and need to organize it.
Adak
Posting Virtuoso
1,640 posts since Jun 2008
Reputation Points: 456
Solved Threads: 196
Skill Endorsements: 6
Question Answered as of 2 Years Ago by
WaltP
and
Adak