|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
BubbleSort Algorithm..Counting Data Moves?
i have a bubblesort function below, but i have a problem.....i need to know the number of "data moves" which "is movement of an element of data from one position in the array to another, to a hold area or from a hold area back to the array."
so far heres the code: Code:
void bubbleSort (int list [], int last)
{
// Local Definitions
int current;
int walker;
int temp;
bool sorted;
// Statements
// Each iteration is one sort pass
for (current = 0, sorted = false;
current <= last && !sorted;
current++)
for (walker = last, sorted = true; walker > current; walker--)
if (list[walker] < list[walker - 1])
// Any exchange means list is not sorted
{
sorted = false;
temp = list[walker];
list[walker] = list[walker - 1];
list[walker - 1] = temp;
}
return;
}
so how would i know how to calculate the number of times a data move is made? Last edited by Infinite Loop : April 8th, 2004 at 12:19 AM. |
|
#2
|
||||
|
||||
|
I'm presuimng this is C++. Okay:
Code:
oid bubbleSort (int list [], int last)
{
// Local Definitions
int current;
int walker;
int temp;
int move = 0;
bool sorted;
int numDataMoves = 0;
// Statements
// Each iteration is one sort pass
for (current = 0, sorted = false;
current <= last && !sorted;
current++)
for (walker = last, sorted = true; walker > current; walker--)
if (list[walker] < list[walker - 1])
// Any exchange means list is not sorted
// if they need tob be exchanged, increment the numDataMoves counter
{
sorted = false;
temp = list[walker];
list[walker] = list[walker - 1];
list[walker - 1] = temp;
numDataMoves++;
}
cout << " \n |Bubble Sort | " << temp << "number of data moves/swaps:" << numDataMoves;
return;
}
__________________
~~ Peter ~~ ( My Blog: It's exactly like normal nerdiness, but completely different. ) :: ( Supporter of the EFF & FSF ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) |
|
#3
|
|||
|
|||
|
thanks let me give that a try.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > BubbleSort Algorithm..Counting Data Moves? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|