459,474 Members | 1,270 Online
Need help? Post your question and get tips & solutions from a community of 459,474 IT Pros & Developers. It's quick & easy.

# O.T. - I'm not sure - Anyway, its a sort algorithm question

 P: n/a Consider an array A(5) consisting of integers 1 through 5 in random order. 3,1,5,2,4. To make things easier to read, I'll eliminate the commas. My objective is to sort 31524 to 12345 by moving left-to-right through the string 31524 examining 2 characters at a time, reversing them to place the smaller of the two on the left. Initialize Flip=False. Set to True if any change req'd. 31524 becomes 13524 then Flip=True 13524 becomes 13524 then 13524 becomes 13254 then Flip=True 13254 becomes 13245 And that is one complete pass through the string. Flip=True Remembering that changes were made in the pass, we'll make one more to see if NO changes have to be made. Set Flip=False... 13245 becomes 13245 then 13245 becomes 12345 then Flip=True 12345 becomes 12345 then 12345 becomes 12345 And that is the second complete pass through the string. Remembering that changes were made in the pass (2nd digit pair), we'll make one more to see if NO changes have to be made. Set Flip=False... 12345 becomes 12345 then 12345 becomes 12345 then 12345 becomes 12345 then 12345 becomes 12345 And that is the 3rd pass thru the string. Flip is still False. Although the third pass was not required for you and I, a machine would need to perform this pass to determine that it could complete the pass without setting Flip to True. There were three Flips in the first pass, one Flip in the second pass and no Flips in the third pass. The machine knows it's done when Flip is False at the end of any pass. Does this sort algorithm have a name? That is my question. Dec 2 '05 #1
6 Replies

 P: n/a "MLH" wondered: Does this sort algorithm have a name? That is my question. http://en.wikipedia.org/wiki/Bubble_sort Dec 2 '05 #2

 P: n/a On Fri, 2 Dec 2005 19:08:11 +0100, "kaniest" wrote: "MLH" wondered: Does this sort algorithm have a name? That is my question.http://en.wikipedia.org/wiki/Bubble_sort Many thx, kaniest. One of the LEAST desirable sort methods, I see. Dec 2 '05 #3

 P: n/a MLH wrote: On Fri, 2 Dec 2005 19:08:11 +0100, "kaniest" wrote:"MLH" wondered:Does this sort algorithm have a name? That is my question.http://en.wikipedia.org/wiki/Bubble_sort Many thx, kaniest. One of the LEAST desirable sort methods, I see. For 5 elements, do you even care? Dec 3 '05 #4

 P: n/a >>>Does this sort algorithm have a name? That is my question.http://en.wikipedia.org/wiki/Bubble_sort Many thx, kaniest. One of the LEAST desirable sort methods, I see. For 5 elements, do you even care? If only 5 elements are involved, then bubblesort could do the job perfectly. However, consider implementing quicksort if the number of elements is much larger: http://en.wikipedia.org/wiki/Quicksort Dec 5 '05 #5

 P: n/a I used 5 elements to demonstrate the sort method in the post only. Dec 6 '05 #6

 P: n/a MLH wrote in news:h4********************************@4ax.com: I used 5 elements to demonstrate the sort method in the post only. Even with a 5-element set, I'd implement the sort with the most efficient sort practical, since you don't know that some day the number of elements might vastly increase. -- David W. Fenton http://www.bway.net/~dfenton dfenton at bway dot net http://www.bway.net/~dfassoc Dec 6 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.