C++ sorting method question?!

Anonymous

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
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

`#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] );elselarge.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;`

This question has not been answered.

Create a free account to get help with this and any other question!

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors