T
Trent
Still have problems with this thing.
Seems my results are not matching the "correct" example given.
The three sets of numbers below the last 3 columns is suppose to be the
number of comparisons made by eahc sorting function.
I have having trouble getting the number of comparisons to match. One
comment was that "the counts are in the wrongs place," but I have moved them
all over the function and still they do not match.
Thanks to all have given advice so far.
Trent
Output and code below:
================"CORRECT" OUTPUT============
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
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
1900 1900 1900 1900
9 9 45
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
1600 231 231 231
1444 231 231 231
1325 678 678 678
1222 850 850 850
1050 999 999 999
999 1050 1050 1050
850 1222 1222 1222
678 1325 1325 1325
231 1444 1444 1444
231 1600 1600 1600
81 45 45
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
1444 678 678 678
850 850 850 850
999 999 999 999
678 1050 1050 1050
1222 1050 1050 1050
1050 1222 1222 1222
1325 1325 1325 1325
1600 1444 1444 1444
1900 1600 1600 1600
1050 1900 1900 1900
54 21 45
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
999 231 231 231
850 231 231 231
678 678 678 678
231 850 850 850
1222 999 999 999
1325 1222 1222 1222
1444 1325 1325 1325
1600 1444 1444 1444
1900 1600 1600 1600
231 1900 1900 1900
81 20 45
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
850 231 231 231
999 678 678 678
1050 850 850 850
1222 999 999 999
1325 1050 1050 1050
1444 1222 1222 1222
1600 1325 1325 1325
1900 1444 1444 1444
678 1600 1600 1600
231 1900 1900 1900
90 24 45
================MY OUTPUT================
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 9
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 18
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 27
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 36
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 45
================CODE================
#include <iostream>
#include <fstream>
#include<iomanip>
#include<cstdlib>
using namespace std;
void bubbleSort(int array[], int, int &); //function prototypes
void selectionSort(int [], int, int &);
void insertionSort(int [], 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,sSortCounter, 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("input.txt"); //opening input file
if (!inFile)
{
cerr << "Error Opening File" << endl;
system("pause");
EXIT_FAILURE; //stop program on failure
}
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(bsort, arraySize, bsortCounter);// call the sort functions
selectionSort(ssort, arraySize,sSortCounter);
insertionSort(isort, arraySize,iSortCounter);
print(numArray, arraySize, bsort, ssort, isort, bsortCounter,sSortCounter,
iSortCounter);\
}
cout << endl;
system("pause");
inFile.close();// close files
outFile.close();
return 0;
}
// Funtions below
void bubbleSort(int array[], int size, int &count)
{
int i, pass, flag;
for (pass =0; pass < size - 1; pass++) //# of passes
{
for (i= 0; i < size - 1; i++) // one pass
{
if (array > array[i+1]) //one comparison
{
swap(array, array[i+1]); //one swap
}
}count++;
}
}// end of bubble Sort function
void selectionSort(int array[], int size, int &count)
{
int i, j, tmp;
for (i = 0; i < size - 1; i++)
{
tmp = i;
count = count++;
for (j = i+1; j < size; j++)
if (array[j] < array[tmp])
{
tmp = j;
}
swap(array, array[tmp]); //call swap funtion
}
}//end of selection sort function
void insertionSort(int array[],int size, int &count)
{
int tmp,i;
for(int j=1;j<size;j++)
{
tmp=array[j];
i=j-1;
while(i>=0 && array>tmp)
{
array[i+1]=array;
i--;
}
array[i+1]=tmp;
count++;
}
}
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)<<scount
<<setw(19)<< icount << endl;
}// end of print function
void swap(int &element1, int &element2)
{
int tmp = element1;
element1 = element2;
element2 = tmp;
}
Seems my results are not matching the "correct" example given.
The three sets of numbers below the last 3 columns is suppose to be the
number of comparisons made by eahc sorting function.
I have having trouble getting the number of comparisons to match. One
comment was that "the counts are in the wrongs place," but I have moved them
all over the function and still they do not match.
Thanks to all have given advice so far.
Trent
Output and code below:
================"CORRECT" OUTPUT============
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
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
1900 1900 1900 1900
9 9 45
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
1600 231 231 231
1444 231 231 231
1325 678 678 678
1222 850 850 850
1050 999 999 999
999 1050 1050 1050
850 1222 1222 1222
678 1325 1325 1325
231 1444 1444 1444
231 1600 1600 1600
81 45 45
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
1444 678 678 678
850 850 850 850
999 999 999 999
678 1050 1050 1050
1222 1050 1050 1050
1050 1222 1222 1222
1325 1325 1325 1325
1600 1444 1444 1444
1900 1600 1600 1600
1050 1900 1900 1900
54 21 45
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
999 231 231 231
850 231 231 231
678 678 678 678
231 850 850 850
1222 999 999 999
1325 1222 1222 1222
1444 1325 1325 1325
1600 1444 1444 1444
1900 1600 1600 1600
231 1900 1900 1900
81 20 45
Unsorted List Bubble Sorted Insertion Sorted Selection Sorted
850 231 231 231
999 678 678 678
1050 850 850 850
1222 999 999 999
1325 1050 1050 1050
1444 1222 1222 1222
1600 1325 1325 1325
1900 1444 1444 1444
678 1600 1600 1600
231 1900 1900 1900
90 24 45
================MY OUTPUT================
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 9
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 18
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 27
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 36
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 45
================CODE================
#include <iostream>
#include <fstream>
#include<iomanip>
#include<cstdlib>
using namespace std;
void bubbleSort(int array[], int, int &); //function prototypes
void selectionSort(int [], int, int &);
void insertionSort(int [], 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,sSortCounter, 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("input.txt"); //opening input file
if (!inFile)
{
cerr << "Error Opening File" << endl;
system("pause");
EXIT_FAILURE; //stop program on failure
}
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(bsort, arraySize, bsortCounter);// call the sort functions
selectionSort(ssort, arraySize,sSortCounter);
insertionSort(isort, arraySize,iSortCounter);
print(numArray, arraySize, bsort, ssort, isort, bsortCounter,sSortCounter,
iSortCounter);\
}
cout << endl;
system("pause");
inFile.close();// close files
outFile.close();
return 0;
}
// Funtions below
void bubbleSort(int array[], int size, int &count)
{
int i, pass, flag;
for (pass =0; pass < size - 1; pass++) //# of passes
{
for (i= 0; i < size - 1; i++) // one pass
{
if (array > array[i+1]) //one comparison
{
swap(array, array[i+1]); //one swap
}
}count++;
}
}// end of bubble Sort function
void selectionSort(int array[], int size, int &count)
{
int i, j, tmp;
for (i = 0; i < size - 1; i++)
{
tmp = i;
count = count++;
for (j = i+1; j < size; j++)
if (array[j] < array[tmp])
{
tmp = j;
}
swap(array, array[tmp]); //call swap funtion
}
}//end of selection sort function
void insertionSort(int array[],int size, int &count)
{
int tmp,i;
for(int j=1;j<size;j++)
{
tmp=array[j];
i=j-1;
while(i>=0 && array>tmp)
{
array[i+1]=array;
i--;
}
array[i+1]=tmp;
count++;
}
}
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)<<scount
<<setw(19)<< icount << endl;
}// end of print function
void swap(int &element1, int &element2)
{
int tmp = element1;
element1 = element2;
element2 = tmp;
}