473,425 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

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

MLH
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 1332
"MLH" <CR**@NorthState.net> wondered:
Does this sort algorithm have a name? That is my question.


http://en.wikipedia.org/wiki/Bubble_sort
Dec 2 '05 #2
MLH
On Fri, 2 Dec 2005 19:08:11 +0100, "kaniest" <ka*****@xs4all.nl>
wrote:
"MLH" <CR**@NorthState.net> 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
MLH wrote:
On Fri, 2 Dec 2005 19:08:11 +0100, "kaniest" <ka*****@xs4all.nl>
wrote:

"MLH" <CR**@NorthState.net> 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
>>>
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
MLH
I used 5 elements to demonstrate
the sort method in the post only.
Dec 6 '05 #6
MLH <CR**@NorthState.net> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hagen | last post by:
Hello, got a short question about the sort algorithm of the list container in the Standard Template Library: Can I use the sort algorithm with another parameter to sort by? Sort uses the...
5
by: Lieven De Keyzer | last post by:
Is the STL sort() algorithm implemented genericly over all the containers or once for each container? And in which file is it implemented?
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
3
by: Bob Dankert | last post by:
Is there any way to maintain the sort order of a sort on a 2D array? For example: I have the array: 1,a 2,a 3,f 4,a 5,s 6,a 7,z 8,b and sort it by the second column, and I...
5
by: Irfan Mumtaz | last post by:
hi, I had posted a question in the group "how to arrange arrays in increasing order" Although i got an answer using IComparable Interface by Harfried ,which is given below. Dim InputArray()() As...
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
30
by: gaoxtwarrior | last post by:
a sort which is stable means it keeps the object's original order. A sort which is in place is stable. does anyone can explain me what is sort in place? thx.
11
by: Jeff Schwab | last post by:
Would std::sort ever compare an object with itself? I'm not talking about two distinct, equal-valued objects, but rather this == &that. The container being sorted is a std::vector. I've never...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.