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. 6 1332
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.
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?
>>> 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
I used 5 elements to demonstrate
the sort method in the post only.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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?
|
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...
|
by: Mark Kamoski |
last post by:
Hi--
Please help.
I need a code sample for bubble sort.
Thank you.
--Mark
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| | |