# C++ Project, sorting methods?!

*label*Programming

*timer*Asked: Nov 15th, 2013

**Question description**

Okay so I am in a Data Structures class and I am completely stuck. I did the project it is just coming up with errors so can someone please help me with what i'm doing wrong!!

the program should prompt the user to:

• Have the user enter the number of items in the list you wish to sort.

• Have the user enter the number of trials you wish to run.

• Have the user enter the type of sort you wish to run. A menu of options would probably work best for this. It should provide the option of bubble sort, selection sort, merge sort, and quick sort.

The program should then run the requested number of trials on a list of the given size, then report the average amount of time it took to sort a single list of that size. For example, if the user wants a list size of 100 and a number of trial of 10, you should sort a 100 item list 10 times (make it a new randomized list each time!), then report the total time divided by the number of trials.

For example, a run of this program might look like this:

Please enter the size of the list you wish to sort: 1000

Please enter the number of trials to run: 100

Please select a sort:

1. Bubble sort

2. Selection sort

3. Merge sort

4. Quick sort

Choice: 1

The average time to sort 1000 items using bubble sort is: 0.14 seconds

I KNOW IT IS A LITTLE LENGTHY BUT I'M DESPERATE.. PLEASE HELP ME! :)

#include <iostream>;

#include <vector>;

#include <ctime>;

using namespace std;

void bubbleSort( vector<int> &array)

{

int temp;

bool swapped;

for ( int i = 0; i < array.length() -1; i++)

{

swapped = false;

for ( int j = 0; j < array.length() - 1 - i; j++ )

{

if ( array[j] > array [j + 1] )

{

temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

swapped = true;

}

}

if (!swapped)

return;

}

}

void selectionSort( vector<int> &array )

{

int maxPos;

int temp;

for ( int i = array.size() - 1; i > 0; i--)

{

maxPos = 0;

for ( int j = 1; j <= i; j++)

{

if ( array[j] > array[maxPos] )

maxPos = j;

}

temp = array[maxPos];

array[maxPos] = array[i];

array[i] = temp;

}

}

void mergeSort(vector<int> &array, int start, int middle, int end)

{

vector<int> one = new vector<int>(middle - start + 1);

vector<int> two = new vector<int>( end - middle );

for (int i = start; i <= middle; i++)

{

one[i - start] = array[i];

}

for (int i = middle +1; i <+ end; i++)

{

two[i- (middle + 1)] = array[i];

}

int i1 = 0, i2 = 0, j = start;

while ( ( i1 < one.size() ) && ( i2 < two.size() ) )

{

if (one[i1] < two[i2])

{

array[j] = one[i1];

i1++;

}

else

{

array[j] = two[i2];

i2++;

}

j++;

}

while ( i1 < one.size() )

{

array[j] = one[i1];

j++;

i1++;

}

while ( i2 < two.size() )

{

array[j] = two[i2];

j++;

i2++;

}

}

void sortAux( vector<int> &array, int start, int end )

{

if( start >= end )

return;

int pivotLoc = partition(array, start, end );

sortAux( array, start, pivotLoc - 1);

sortAux( array, pivotLoc + 1, end);

}

int partition( vector<int> &array, int start, int end)

{

int pivot = array[start];

vector<int> small;

vector<int> large;

for ( int i = start + 1; i <= end; i++ )

{

if ( array[i] < pivot )

small.add( array[i] );

else

large.add( array[i] );

}

int pivotLoc = small.size() + start;

int j = start;

for (int i = 0; i < small.size(); i++)

{

array[j] = small[i];

j++;

}

array[j] = pivot;

j++;

for ( int i = 0; i < large.size(); i++)

{

array[j] = large[i];

j++;

}

return pivotLoc;

}

void quickSort( vector<int> &array )

{

sortAux( array, 0, array.size() - 1);

}

void randomArray( vector<int> &array)

{

for ( unsigned int i = 0; i < array.size(); i++)

{

array[i] = rand() % 100;

}

}

void printArray( vector<int> array)

{

for (unsigned int i = 0; i < array.size(); i++ )

{

cout << array[i] << " ";

}

cout << endl;

}

int main()

{

vector<int> list;

int choice, start, finish, total, size, trials;

cout << "Please enter size of list you wish to sort: " << endl;

cin >> size;

cout << "Please enter the number of trials to run: " << endl;

cin >> trials;

cout << "Please select a sorting method: " << endl;

cout << "1. Bubble Sort" << endl;

cout << "2. Selection Sort" << endl;

cout << "3. Merge Sort" << endl;

cout << "4. Quick Sort" << endl;

if (choice == 1)

{

bubbleSort;

}

if (choice == 2)

{

selectionSort;

}

if (choice == 3)

{

mergeSort;

}

if (choice == 4)

{

quickSort;

}

list.resize(size);

start = (int)time(NULL);

for( int i = 0; i < trials; i++)

{

randomArray( list);

}

finish = (int)time(NULL);

printArray(list);

total = finish - start;

cout << "the finished list is" << start- finish << endl;

cout << "the average time is" << total/ trials << endl;

**Supplement:**

I keep getting 8 errors:

1. error C2039: 'length' : is not a member of ' std::vector<_Ty>' on line 12

2. error C2039: 'length' : is not a member of ' std::vector<_Ty>' on line 15

3. error C2440: 'initializing' : cannot convert from ' std::vector<_Ty> *' to 'std::vector<_Ty>' on line 51

4. error C2440: 'initializing' : cannot convert from ' std::vector<_Ty> *' to 'std::vector<_Ty>' on line 52

5. error C3861: 'partition': identifier not found on line 101

6. error 32039: 'add' : is not a member of 'std::vector<_Ty>' on line 116

7. error 32039: 'add' : is not a member of 'std::vector<_Ty>' on line 118

8. fatal error C1075: end of file found before the left brace '{' at 'c/users/depaw/documents/visual studio 2008/projects/main/main/main.cpp(168) was matched