473,661 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fot those with extra time.. sorting question

Running this I see that on first run, both bubble and selection sort have 9
sort counts while insertion sort has ZERO. With a sorted list, should this
be ZERO for all?

Also bsort and Ssort have the same exact sorting counts also..shouldn't they
differ somewhat?

Source and data file below.

Thanks

Trent

#include <iostream>
#include <fstream>
#include<iomani p>

using namespace std;
void bubbleSort(int *array, const int, int &); //function prototypes
void selectionSort(i nt *, const int, int &);
void insertionSort(i nt *, const int, int &);
void swap(int *, int *);
void print(int *array, int size, int *bubbleSort, int *selectionSort, int
*insertionSort, int bcount, int scount, int icount);
// print(&numArray , arraySize, &bsort, &ssort, &isort,
bsortCounter,sS ortCounter, iSortCounter);

ifstream inFile;
ofstream outFile;

int main()
{

const int arraySize = 10;
int numArray[arraySize];
int bsort[arraySize];
int bsortCounter =0;
int isort[arraySize];
int iSortCounter =0;
int ssort[arraySize];
int sSortCounter =0;
// each sort needs array and counter (parallel arrays)
inFile.open("in put.txt"); //opening input file
if (!inFile)
{
cerr << "Error Opening File" << endl;
system("pause") ;
return -1;
}
for (int i =0;i < 5;i++)
{
for (int j=0; j< arraySize;j++)
{
inFile >numArray[j];
bsort[j]=numArray[j];
isort[j]=numArray[j];
ssort[j]=numArray[j];
}

cout << endl;
bubbleSort(bsor t, arraySize, bsortCounter);// call the sort functions
selectionSort(s sort, arraySize,sSort Counter);
insertionSort(i sort, arraySize,iSort Counter);

print(numArray, arraySize, bsort, ssort, isort, bsortCounter,sS ortCounter,
iSortCounter);\

}

cout << endl;
system("pause") ;

inFile.close();// close files
outFile.close() ;
return 0;
}

// Funtions below


void bubbleSort(int *array, const int size, int &count)
{
int i, pass;
for (pass =0; pass < size - 1; pass++) //# of passes
count= count+1;

for (i= 0; i < size - 1; i++) // one pass
if (array[i] array[i+1]) //one comparison
{
swap(array[i], array[i+1]);
}

}// end of bubble Sort function
void selectionSort(i nt *array, const int size, int &count)
{
int i, j, tmp;
for (i = 0; i < size - 1; i++)
{
tmp = i;
count = count + 1;
for (j = i+1; j < size; j++)
if (array[j] < array[tmp])
tmp = j;

swap(array[i], array[tmp]); //call swap funtion
}
}//end of selection sort function

void insertionSort(i nt *array,const int size, int &count)
{
int tmp,i;
for(int j=1;j<size;j++)
{
tmp=array[j];
i=j-1;
while(array[i]>tmp && i>=0)
{
count = count +1;
array[i+1]=array[i];
i--;
}
array[i+1]=tmp;
}
}
void print(int *array, int size, int *bubbleSort, int *selectionSort, int
*insertionSort, int bcount, int scount, int icount)
{
cout << " Unsorted List Bubble Sorted Selection Sorted Insertion
Sorted" << endl;

for (int k =0;k < size;k++)
cout << setw(15) <<array[k] << setw(16) << bubbleSort[k] << setw(20) <<
selectionSort[k] << setw(19) << insertionSort[k] << endl;
cout << endl << "Number: " << setw(23) <<bcount << setw(20)<<scoun t
<<setw(19)<< icount << endl;

}// end of print function

void swap(int *element1, int *element2)
{
int tmp = *element1;
*element1 = *element2;
*element2 = tmp;
}
----------------------
Data Input File
----------------------

231
678
850
999
1050
1222
1325
1444
1600
1900

1900
1600
1444
1325
1222
1050
999
850
678
231

231
1444
850
999
678
1222
1050
1325
1600
1900

1050
999
850
678
231
1222
1325
1444
1600
1900

231
850
999
1050
1222
1325
1444
1600
1900
678

Jun 29 '07 #1
11 1999
Oh..I forgot..here are the results:
Unsorted List Bubble Sorted Selection Sorted Insertion Sorted
231 231 231 231
678 678 678 678
850 850 850 850
999 999 999 999
1050 1050 1050 1050
1222 1222 1222 1222
1325 1325 1325 1325
1444 1444 1444 1444
1600 1600 1600 1600
1900 1900 1900 1900

Number: 9 9 0

Unsorted List Bubble Sorted Selection Sorted Insertion Sorted
1900 231 231 231
1600 678 678 678
1444 850 850 850
1325 999 999 999
1222 1050 1050 1050
1050 1222 1222 1222
999 1325 1325 1325
850 1444 1444 1444
678 1600 1600 1600
231 1900 1900 1900

Number: 18 18 45

Unsorted List Bubble Sorted Selection Sorted Insertion Sorted
231 231 231 231
1444 678 678 678
850 850 850 850
999 999 999 999
678 1050 1050 1050
1222 1222 1222 1222
1050 1325 1325 1325
1325 1444 1444 1444
1600 1600 1600 1600
1900 1900 1900 1900

Number: 27 27 54

Unsorted List Bubble Sorted Selection Sorted Insertion Sorted
1050 231 231 231
999 678 678 678
850 850 850 850
678 999 999 999
231 1050 1050 1050
1222 1222 1222 1222
1325 1325 1325 1325
1444 1444 1444 1444
1600 1600 1600 1600
1900 1900 1900 1900

Number: 36 36 64

Unsorted List Bubble Sorted Selection Sorted Insertion Sorted
231 231 231 231
850 678 678 678
999 850 850 850
1050 999 999 999
1222 1050 1050 1050
1325 1222 1222 1222
1444 1325 1325 1325
1600 1444 1444 1444
1900 1600 1600 1600
678 1900 1900 1900

Number: 45 45 72

Press any key to continue . . .
Jun 29 '07 #2
Trent wrote:
Running this I see that on first run, both bubble and selection sort have
9 sort counts while insertion sort has ZERO. With a sorted list, should
this be ZERO for all?
I guess you by ZERO mean zero? Then please stop screaming. No. Your
algorithms only give zero for the insertion sort on a sorted list.
Also bsort and Ssort have the same exact sorting counts also..shouldn't
they differ somewhat?
for (int i = 0; i < size-1; ++i) {
count = count + 1;
}

should always provide the same answer for count as long as size remains the
same.

Note that how different sorting algorithms work is off topic on this group.
Source and data file below.

Thanks

Trent

#include <iostream>
#include <fstream>
#include<iomani p>

using namespace std;
void bubbleSort(int *array, const int, int &); //function prototypes
void selectionSort(i nt *, const int, int &);
void insertionSort(i nt *, const int, int &);
void swap(int *, int *);
What's wrong with std::swap?
void print(int *array, int size, int *bubbleSort, int *selectionSort, int
*insertionSort, int bcount, int scount, int icount);
// print(&numArray , arraySize, &bsort, &ssort, &isort,
bsortCounter,sS ortCounter, iSortCounter);

ifstream inFile;
ofstream outFile;
Any particular reason for making these global?
>
int main()
{

const int arraySize = 10;
int numArray[arraySize];
int bsort[arraySize];
int bsortCounter =0;
int isort[arraySize];
int iSortCounter =0;
int ssort[arraySize];
int sSortCounter =0;
// each sort needs array and counter (parallel arrays)
inFile.open("in put.txt"); //opening input file
if (!inFile)
{
cerr << "Error Opening File" << endl;
system("pause") ;
I have no pause program on my computer. When posting to this list, please
don't rely upon external programs.
return -1;
There are only three portable values to return from main:
0, EXIT_SUCCESS, or EXIT_FAILURE.

The two names are macros from <cstdlib>. Every other value has
implementation-defined results.
}
for (int i =0;i < 5;i++)
{
for (int j=0; j< arraySize;j++)
{
inFile >numArray[j];
if (!(inFile >numArray[j])) { /*handle*/ }

Even if you did open the file, and you _know_ that it contains what you
want, reading it might fail.
bsort[j]=numArray[j];
isort[j]=numArray[j];
ssort[j]=numArray[j];
}

cout << endl;
bubbleSort(bsor t, arraySize, bsortCounter);// call the sort functions
selectionSort(s sort, arraySize,sSort Counter);
insertionSort(i sort, arraySize,iSort Counter);

print(numArray, arraySize, bsort, ssort, isort, bsortCounter,sS ortCounter,
iSortCounter);\

}

cout << endl;
system("pause") ;
inFile.close();// close files
outFile.close() ;
return 0;
}

// Funtions below


void bubbleSort(int *array, const int size, int &count)
{
int i, pass;
for (pass =0; pass < size - 1; pass++) //# of passes
count= count+1;
a) why not ++count or count++?
b) count is always incremented size-1 times.
>
for (i= 0; i < size - 1; i++) // one pass
Usually, this would be
for (i = 0; i < size - (pass + 1); i++)
You should find out why.
if (array[i] array[i+1]) //one comparison
{
swap(array[i], array[i+1]);
This cannot possibly call your swap function. My theory is that if this
actually compiled, you are calling std::swap: A standard header is allowed
to include another, and since you said "using namespace std;" it might
actually _use_ any member of namespace std. You should generally
avoid "using namespace std;".
}

}// end of bubble Sort function
void selectionSort(i nt *array, const int size, int &count)
{
int i, j, tmp;
for (i = 0; i < size - 1; i++)
{
tmp = i;
count = count + 1;
for (j = i+1; j < size; j++)
if (array[j] < array[tmp])
tmp = j;

swap(array[i], array[tmp]); //call swap funtion
}
I repeat the remarks from bubble sort about swap and count.
}//end of selection sort function

void insertionSort(i nt *array,const int size, int &count)
{
int tmp,i;
for(int j=1;j<size;j++)
{
tmp=array[j];
i=j-1;
while(array[i]>tmp && i>=0)
This is undefined behaviour if array[0] < tmp. Make this:
while (i>=0 && array[i]>tmp)
{
count = count +1;
This time counting is done in the inner loop. What are you actually
counting?
array[i+1]=array[i];
i--;
}
array[i+1]=tmp;
}
}
--
rbh
Jun 29 '07 #3

"Robert Bauck Hamar" <ro**********@i fi.uio.nowrote in message
news:f6******** **@readme.uio.n o...
Trent wrote:
>Running this I see that on first run, both bubble and selection sort have
9 sort counts while insertion sort has ZERO. With a sorted list, should
this be ZERO for all?

I guess you by ZERO mean zero? Then please stop screaming. No. Your
algorithms only give zero for the insertion sort on a sorted list.
>Also bsort and Ssort have the same exact sorting counts also..shouldn't
they differ somewhat?

for (int i = 0; i < size-1; ++i) {
count = count + 1;
}

should always provide the same answer for count as long as size remains
the
same.

Note that how different sorting algorithms work is off topic on this
group.
But, they are written in C++, and understanding how they work in C++,
therefore on topic
>
>Source and data file below.

Thanks

Trent

#include <iostream>
#include <fstream>
#include<ioman ip>

using namespace std;
void bubbleSort(int *array, const int, int &); //function prototypes
void selectionSort(i nt *, const int, int &);
void insertionSort(i nt *, const int, int &);
void swap(int *, int *);

What's wrong with std::swap?
Not in specifications
>void print(int *array, int size, int *bubbleSort, int *selectionSort, int
*insertionSort , int bcount, int scount, int icount);
// print(&numArray , arraySize, &bsort, &ssort, &isort,
bsortCounter,s SortCounter, iSortCounter);

ifstream inFile;
ofstream outFile;

Any particular reason for making these global?
>>
int main()
{

const int arraySize = 10;
int numArray[arraySize];
int bsort[arraySize];
int bsortCounter =0;
int isort[arraySize];
int iSortCounter =0;
int ssort[arraySize];
int sSortCounter =0;
// each sort needs array and counter (parallel arrays)
inFile.open("in put.txt"); //opening input file
if (!inFile)
{
cerr << "Error Opening File" << endl;
system("pause") ;

I have no pause program on my computer. When posting to this list, please
don't rely upon external programs.

You mean like iostream is an external program?
It's part of the C++ library <iomanip>
>
> return -1;

There are only three portable values to return from main:
0, EXIT_SUCCESS, or EXIT_FAILURE.
Okay I went ahead and inplmented that.
The two names are macros from <cstdlib>. Every other value has
implementation-defined results.
> }
for (int i =0;i < 5;i++)
{
for (int j=0; j< arraySize;j++)
{
inFile >numArray[j];

if (!(inFile >numArray[j])) { /*handle*/ }

Even if you did open the file, and you _know_ that it contains what you
want, reading it might fail.
Yes, but this is a learning excercise. I am not trying to make a commercial
package.
As I learn more I will be glad to add more stuff. :-)

> bsort[j]=numArray[j];
isort[j]=numArray[j];
ssort[j]=numArray[j];
}

cout << endl;
bubbleSort(bso rt, arraySize, bsortCounter);// call the sort functions
selectionSort( ssort, arraySize,sSort Counter);
insertionSort( isort, arraySize,iSort Counter);

print(numArray , arraySize, bsort, ssort, isort,
bsortCounter,s SortCounter,
iSortCounter); \

}

cout << endl;
system("pause") ;
inFile.close();// close files
outFile.close() ;
return 0;
}

// Funtions below


void bubbleSort(int *array, const int size, int &count)
{
int i, pass;
for (pass =0; pass < size - 1; pass++) //# of passes
count= count+1;

a) why not ++count or count++?
b) count is always incremented size-1 times.
I can do it anyway..again this is a learning exercise, but I can implement
it.
>
>>
for (i= 0; i < size - 1; i++) // one pass

Usually, this would be
for (i = 0; i < size - (pass + 1); i++)
You should find out why.

Actually I grabbed an example from the Deitel & Deitel "C++ How to Program"
Second Ed.
In reality all my sorts should be like this, but I am out of time to try and
figure the psuedo code out.

Bubble Sort:
Sorted = False

While not Sorted do

Sorted = True

For I = 1 to N-1 do

If A[i] A[I+1] then

Swap(A[i], A[I+1])

Sorted = False

Insertion Sort:

For I = 2 to N do

J = I

Done = False

While J >= 2 and not Done

If A[J-1] A[J] Then

Swap(A[J], A[J-1])

Else

Done = True

J=J-1



Selection Sort:

For I = 1 to N-1 do

Smallest = A[i]

Location = I

For J=I+1 to N do

If A[J] < Smallest then

Smallest = A[J]

Location = J

Swap(A[i], A[Location])

>
> if (array[i] array[i+1]) //one comparison
{
swap(array[i], array[i+1]);

This cannot possibly call your swap function. My theory is that if this
actually compiled, you are calling std::swap: A standard header is allowed
to include another, and since you said "using namespace std;" it might
actually _use_ any member of namespace std. You should generally
avoid "using namespace std;".
It does...The bubble sort and the swap came from the same example.
Actually I am calling the function swap that is at the end of the program,
correct?
>
> }

}// end of bubble Sort function
void selectionSort(i nt *array, const int size, int &count)
{
int i, j, tmp;
for (i = 0; i < size - 1; i++)
{
tmp = i;
count = count + 1;
for (j = i+1; j < size; j++)
if (array[j] < array[tmp])
tmp = j;

swap(array[i], array[tmp]); //call swap funtion
}

I repeat the remarks from bubble sort about swap and count.
Okay, but seems to be functioning to me.
>
>}//end of selection sort function

void insertionSort(i nt *array,const int size, int &count)
{
int tmp,i;
for(int j=1;j<size;j++)
{
tmp=array[j];
i=j-1;
while(array[i]>tmp && i>=0)

This is undefined behaviour if array[0] < tmp. Make this:
while (i>=0 && array[i]>tmp)
I did that and results are still the same.
> {
count = count +1;

This time counting is done in the inner loop. What are you actually
counting?

I am trying to count the actual amount of sorts it takes to sort the array.
> array[i+1]=array[i];
i--;
}
array[i+1]=tmp;
}
}

Thanks for the input

--
rbh

Jun 30 '07 #4
Trent wrote:
"Robert Bauck Hamar" <ro**********@i fi.uio.nowrote in message
news:f6******** **@readme.uio.n o...
>Note that how different sorting algorithms work is off topic on this
group.

But, they are written in C++, and understanding how they work in C++,
therefore on topic
[..]
My cookbook software is written in C++. Should I ask questions about
cooking here too? If you need to know how algorithms work, go ask in
comp.programmin g. If you have questions on the _language_ itself, or
how to turn an algorithm into C++ language constructs, ask here.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 30 '07 #5

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:l-*************** *************** @comcast.com...
Trent wrote:
>"Robert Bauck Hamar" <ro**********@i fi.uio.nowrote in message
news:f6******* ***@readme.uio. no...
>>Note that how different sorting algorithms work is off topic on this
group.

But, they are written in C++, and understanding how they work in C++,
therefore on topic
[..]

My cookbook software is written in C++. Should I ask questions about
cooking here too? If you need to know how algorithms work, go ask in
comp.programmin g. If you have questions on the _language_ itself, or
how to turn an algorithm into C++ language constructs, ask here.
Oh, but I am not asking about cooking..I am asking about constructing
something in C++

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Jun 30 '07 #6

Trent <tr***@nunya.co mwrote in message...
Running this I see that on first run, both bubble and selection sort have
9
sort counts while insertion sort has ZERO. With a sorted list, should this
be ZERO for all?
Also bsort and Ssort have the same exact sorting counts also..shouldn't
they
differ somewhat?
Source and data file below.
Thanks
Trent

#include <iostream>
#include <fstream>
#include<iomani p>

using namespace std;

void bubbleSort(int *array, const int, int &); file://function prototypes
void selectionSort(i nt *, const int, int &);
void insertionSort(i nt *, const int, int &);
void swap(int *, int *);
void print(int *array, int size, int *bubbleSort, int *selectionSort, int
*insertionSort, int bcount, int scount, int icount);
// print(&numArray , arraySize, &bsort, &ssort, &isort,
bsortCounter,sS ortCounter, iSortCounter);

ifstream inFile;
ofstream outFile;

int main(){
// const int arraySize = 10;
Should be an unsigned type (try array[ -1 ], compile?):
std::size_t const arraySize( 10 );
int numArray[arraySize];
int bsort[arraySize];
int bsortCounter =0;
int isort[arraySize];
int iSortCounter =0;
int ssort[arraySize];
int sSortCounter =0;
// each sort needs array and counter (parallel arrays)

inFile.open("in put.txt"); file://opening input file
if (!inFile){
cerr << "Error Opening File" << endl;
system("pause") ;
return -1;
}
for (int i =0;i < 5;i++){
for (int j=0; j< arraySize;j++){
inFile >numArray[j];
bsort[j]=numArray[j];
isort[j]=numArray[j];
ssort[j]=numArray[j];
} // for(j)
cout << endl;
bubbleSort(bsor t, arraySize, bsortCounter);// call the sort functions
selectionSort(s sort, arraySize,sSort Counter);
insertionSort(i sort, arraySize,iSort Counter);
print(numArray, arraySize, bsort, ssort, isort,
bsortCounter,sS ortCounter,
iSortCounter);\
} // for(i)
cout << endl;
system("pause") ;

inFile.close();// close files
outFile.close() ;
return 0;
}
// Funtions below
/* // - style cop -
void bubbleSort(int *array, const int size, int &count){
int i, pass;
for (pass =0; pass < size - 1; pass++) file://# of passes
count= count+1;
for (i= 0; i < size - 1; i++) // one pass
if (array[i] array[i+1]){ file://one comparison
swap(array[i], array[i+1]);
}
}// end of bubble Sort function
*/ // - style cop -

void bubbleSort( int *array, const int size, int &count){
// for( int pass( 0 ); pass < size - 1; ++pass){ // # of passes
// ++count; // count = count+1;
// } // for(pass)
// // but, why not just?: count += size -1;

for( int i( 0 ); i < size - 1; ++i){ // one pass
if( array[ i ] array[ i+1 ] ){ file://one comparison
swap( array[ i ], array[ i+1 ] );
} // if()
// - or put it here: -
++count;
} // for(i)
} // end of bubble Sort function

>
void selectionSort(i nt *array, const int size, int &count){
int tmp( 0 );
for( int i( 0 ); i < size - 1; ++i ){
tmp = i;
++count; // count = count + 1;
for( int j( i+1 ); j < size; ++j )
if( array[ j ] < array[ tmp ] )
tmp = j;
swap( array[ i ], array[ tmp ] ); file://call swap funtion
} // for(i)
}//end of selection sort function

void insertionSort(i nt *array,const int size, int &count){
int tmp( 0 ), i( 0 );
for( int j=1; j < size; ++j ){
tmp = array[ j ];
i = j - 1;
while( array[ i ] tmp && i >= 0 ){
++count; // count = count +1;
array[i+1] = array[ i ];
i--;
} // while()
array[i+1] = tmp;
} // for(i)
} // insertionSort(i nt*,const int,int&)
void print(int *array, int size, int *bubbleSort, int *selectionSort, int
*insertionSort, int bcount, int scount, int icount){
cout << " Unsorted List Bubble Sorted Selection Sorted
Insertion
Sorted" << endl;
for (int k =0;k < size;k++)
cout << setw(15) <<array[k] << setw(16) << bubbleSort[k] << setw(20)
<< selectionSort[k] << setw(19) << insertionSort[k] << endl;
cout << endl << "Number: " << setw(23) <<bcount << setw(20)<<scoun t
<<setw(19)<< icount << endl;
}// end of print function

void swap(int *element1, int *element2){
int tmp = *element1;
*element1 = *element2;
*element2 = tmp;
}
If this is not homework, use 'std::vector'.

{
std::vector<int numArray( arraySize ); // #include <vector>
std::vector<int bsort;
// etc.
// open 'infile'
for( std::size_t i( 0 ); i < 5; ++i){
for( std::size_t j( 0 ); j < numArray.size() ; ++j){
inFile >numArray[ j ];
} // for(j)
bsort = numArray; // std::vector has assignment
// etc.
// rest of stuff
} // for(i)
}

--
Bob R
POVrookie
Jun 30 '07 #7

"BobR" <re***********@ worldnet.att.ne twrote in message
news:mh******** ************@bg tnsc05-news.ops.worldn et.att.net...
>
Trent <tr***@nunya.co mwrote in message...
>Running this I see that on first run, both bubble and selection sort have
9
>sort counts while insertion sort has ZERO. With a sorted list, should
this
be ZERO for all?
Also bsort and Ssort have the same exact sorting counts also..shouldn't
they
>differ somewhat?
Source and data file below.
Thanks
Trent

#include <iostream>
#include <fstream>
#include<ioman ip>

using namespace std;

void bubbleSort(int *array, const int, int &); file://function prototypes
void selectionSort(i nt *, const int, int &);
void insertionSort(i nt *, const int, int &);
void swap(int *, int *);
void print(int *array, int size, int *bubbleSort, int *selectionSort, int
*insertionSort , int bcount, int scount, int icount);
// print(&numArray , arraySize, &bsort, &ssort, &isort,
bsortCounter,s SortCounter, iSortCounter);

ifstream inFile;
ofstream outFile;

int main(){
// const int arraySize = 10;
Should be an unsigned type (try array[ -1 ], compile?):
std::size_t const arraySize( 10 );
> int numArray[arraySize];
int bsort[arraySize];
int bsortCounter =0;
int isort[arraySize];
int iSortCounter =0;
int ssort[arraySize];
int sSortCounter =0;
// each sort needs array and counter (parallel arrays)

inFile.open("in put.txt"); file://opening input file
if (!inFile){
cerr << "Error Opening File" << endl;
system("pause") ;
return -1;
}
for (int i =0;i < 5;i++){
for (int j=0; j< arraySize;j++){
inFile >numArray[j];
bsort[j]=numArray[j];
isort[j]=numArray[j];
ssort[j]=numArray[j];
} // for(j)
cout << endl;
bubbleSort(bsor t, arraySize, bsortCounter);// call the sort functions
selectionSort(s sort, arraySize,sSort Counter);
insertionSort(i sort, arraySize,iSort Counter);
print(numArray, arraySize, bsort, ssort, isort,
bsortCounter,sS ortCounter,
> iSortCounter);\
} // for(i)
cout << endl;
system("pause") ;

inFile.close();// close files
outFile.close() ;
return 0;
}
// Funtions below
/* // - style cop -
>void bubbleSort(int *array, const int size, int &count){
int i, pass;
for (pass =0; pass < size - 1; pass++) file://# of passes
count= count+1;
for (i= 0; i < size - 1; i++) // one pass
if (array[i] array[i+1]){ file://one comparison
swap(array[i], array[i+1]);
}
}// end of bubble Sort function
*/ // - style cop -

void bubbleSort( int *array, const int size, int &count){
// for( int pass( 0 ); pass < size - 1; ++pass){ // # of passes
// ++count; // count = count+1;
// } // for(pass)
// // but, why not just?: count += size -1;

for( int i( 0 ); i < size - 1; ++i){ // one pass
if( array[ i ] array[ i+1 ] ){ file://one comparison
swap( array[ i ], array[ i+1 ] );
} // if()
// - or put it here: -
++count;
} // for(i)
} // end of bubble Sort function

>>
void selectionSort(i nt *array, const int size, int &count){
int tmp( 0 );
for( int i( 0 ); i < size - 1; ++i ){
> tmp = i;
++count; // count = count + 1;
for( int j( i+1 ); j < size; ++j )
> if( array[ j ] < array[ tmp ] )
tmp = j;
swap( array[ i ], array[ tmp ] ); file://call swap funtion
} // for(i)
}//end of selection sort function

void insertionSort(i nt *array,const int size, int &count){
int tmp( 0 ), i( 0 );
> for( int j=1; j < size; ++j ){
tmp = array[ j ];
i = j - 1;
while( array[ i ] tmp && i >= 0 ){
++count; // count = count +1;
array[i+1] = array[ i ];
i--;
} // while()
array[i+1] = tmp;
} // for(i)
} // insertionSort(i nt*,const int,int&)
void print(int *array, int size, int *bubbleSort, int *selectionSort, int
*insertionSort , int bcount, int scount, int icount){
cout << " Unsorted List Bubble Sorted Selection Sorted
Insertion
>Sorted" << endl;
for (int k =0;k < size;k++)
cout << setw(15) <<array[k] << setw(16) << bubbleSort[k] << setw(20)
<< selectionSort[k] << setw(19) << insertionSort[k] << endl;
> cout << endl << "Number: " << setw(23) <<bcount << setw(20)<<scoun t
<<setw(19)<< icount << endl;
}// end of print function

void swap(int *element1, int *element2){
int tmp = *element1;
*element1 = *element2;
*element2 = tmp;
}

If this is not homework, use 'std::vector'.
It is.
Maybe I should include the pseudocode required.
Thanks for the reply Bob.

Bubble Sort:
Sorted = False

While not Sorted do

Sorted = True

For I = 1 to N-1 do

If A[i] A[I+1] then

Swap(A[i], A[I+1])

Sorted = False

Insertion Sort:

For I = 2 to N do

J = I

Done = False

While J >= 2 and not Done

If A[J-1] A[J] Then

Swap(A[J], A[J-1])

Else

Done = True

J=J-1



Selection Sort:

For I = 1 to N-1 do

Smallest = A[i]

Location = I

For J=I+1 to N do

If A[J] < Smallest then

Smallest = A[J]

Location = J

Swap(A[i], A[Location])

{
std::vector<int numArray( arraySize ); // #include <vector>
std::vector<int bsort;
// etc.
// open 'infile'
for( std::size_t i( 0 ); i < 5; ++i){
for( std::size_t j( 0 ); j < numArray.size() ; ++j){
inFile >numArray[ j ];
} // for(j)
bsort = numArray; // std::vector has assignment
// etc.
// rest of stuff
} // for(i)
}

--
Bob R
POVrookie


Jun 30 '07 #8
Trent wrote:
Running this I see that on first run, both bubble and selection sort have 9
sort counts while insertion sort has ZERO. With a sorted list, should this
be ZERO for all?

Also bsort and Ssort have the same exact sorting counts also..shouldn't they
differ somewhat?
Yes they should, but look at your code to see why they don't.
void bubbleSort(int *array, const int size, int &count)
{
int i, pass;
for (pass =0; pass < size - 1; pass++) //# of passes
count= count+1;

for (i= 0; i < size - 1; i++) // one pass
if (array[i] array[i+1]) //one comparison
{
swap(array[i], array[i+1]);
}

}// end of bubble Sort function
void selectionSort(i nt *array, const int size, int &count)
{
int i, j, tmp;
for (i = 0; i < size - 1; i++)
{
tmp = i;
count = count + 1;
for (j = i+1; j < size; j++)
if (array[j] < array[tmp])
tmp = j;

swap(array[i], array[tmp]); //call swap funtion
}
}//end of selection sort function

Look at those loops, both loops go round size-1 times. In both loops
count gets incremented by one each time round the loop. So in both cases
it's not surprising that count==size - 1 at the end of the loop.

This is an example of how the computer does EXACTLY what you tell it to
do, not what you wanted to tell it to do.

You bubble sort routine is wrong. You should stop the loop when the sort
has finished. If at the end of a pass you have not swapped any elements
then the sort has finished and you should quit the loop. I'll leave you
to work out the details.

john
Jun 30 '07 #9
>void bubbleSort(int *array, const int size, int &count)
>{
int i, pass;
for (pass =0; pass < size - 1; pass++) //# of passes
count= count+1;

for (i= 0; i < size - 1; i++) // one pass
if (array[i] array[i+1]) //one comparison
{
swap(array[i], array[i+1]);
}

}// end of bubble Sort function


I see your bubble sort code still has the mistake with missing curly
brackets I pointed out in your last post, but your bubble sort routine
is now sorting correctly. Strange isn't it? You really should post the
code you are actaully compiling if you don't want to end up confusing
everyone.

john
Jun 30 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2235
by: Adam i Agnieszka Gasiorowski FNORD | last post by:
I need help width formulating the most effective (in terms of processing time) SQL query to count all the "new" documents in the repository, where "new" is defined as "from 00:00:01 up to 23:59:59 today". My current query does not give me satisfactory results, it creates a visible delay in rendering of the main page of one of the departments (Drugs) :8https://hyperreal.info > site, see for yourself, notice the delay
3
2154
by: Michael Hoffman | last post by:
Just out of curiosity, I was wondering if anyone has compiled Python 2.4 with the Intel C Compiler and its processor specific optimizations. I can build it fine with OPT="-O3" or OPT="-xN" but when I try to combine them I get this as soon as ./python is run: """ case $MAKEFLAGS in \ *-s*) CC='icc -pthread' LDSHARED='icc -pthread -shared' OPT='-DNDEBUG -O3 -xN' ./python -E ./setup.py -q build;; \ *) CC='icc -pthread' LDSHARED='icc...
137
7062
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very pragmatic - 3) I usually move forward when I get the gut feeling I am correct - 4) Most likely because of 1), I usually do not manage to fully explain 3) when it comes true. - 5) I have developed for many years (>18) in many different environments,...
0
482
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having trouble making it work in SQL Server 2000. I am not a database newbie, but I can't seem to figure this one out so I am turning to the newsgroup. I am thinking that some of the SQL Gurus out there have done this very thing a thousand times before...
44
4706
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the body of the page I put the next code: <BODY onkeydown='notOpenNewWindow();'>
5
12229
by: John Richardson | last post by:
I've been bothered for some time about my DataGrid not populating my rows very quickly. I have about 10K rows loading into the grid. I create a datatable dt with 2 columns, an ID and a display. The ID is a member of the keys array. I then create a DataView dv over the table, and sort it by Display and ID column (in case of duplicate Display). I then set my DataGrid.DataSource = dv; I then load the datatable with my rows, and this is...
4
4279
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event is stored in a double linked list, the whole list is sorted for the next round. My problem appears when subjecting the program to heavy load, that is, when I run the simulation for more than 10,000 miliseconds (every event occurs in...
6
16127
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: struct node { int x; struct node *next; };
3
2834
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one dataset to data in a second dataset, using a common key. I will first describe the problem in words and then I will show my code, which has most of the solution done already. I have built an ASP.NET that queries an Index Server and returns a...
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8762
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.