+1 (315) 557-6473 

Create a Program to Implement Sorting Techniques in C++ Assignment Solution.


Instructions

Objective
Write a C++ assignment program to implement sorting techniques.

Requirements and Specifications

program to implement sorting techniques in C++

Source Code

/*

* Name:

* Description:

* Input:

* Output:

*/

#include

#include

#include "helpers.cpp"

using namespace std;

//---------- YOUR FUNCTIONS HERE ----------

// minimum, maximum, sum, average, reverseVector

/*

* minimum: Finds the minimum in a vector of ints

* parameters: Vector to find minimum of

* return value: Minimum of vector

*/

int minimum(const vector &elements)

{

return elements.front();

}

/*

* maximum: Finds the maximum in a vector of ints

* parameters: Vector to find max of

* return value: Maximum of vector

*/

int maximum(const vector &elements)

{

return elements.back();

}

/*

* sum: Sums a vector of ints

* parameters: Vector to find sum of

* return value: Sum of vector

*/

int sum(const vector &elements)

{

int total = 0;

for (unsigned i = 0, numElements = elements.size(); i < numElements; i++)

total += elements[i];

return total;

}

/*

* average: Finds the average of a vector of ints

* parameters: Vector to find average of

* return value: Average of vector

*/

double average(const vector &elements)

{

return (double)sum(elements) / elements.size();

}

/*

* reverseVector: Reverses a vector of ints

* parameters: Vector to reverse

* return value: Reverse of vector

*/

void reverseVector(vector &elements)

{

int i = 0;

int j = (int)(elements.size() - 1);

while (i < j)

{

int temp = elements[i];

elements[i] = elements[j];

elements[j] = temp;

i++;

j--;

}

}

//---------- END YOUR FUNCTIONS ----------

int main(int argc, char const *argv[]) {

if (argc == 1) cout << "Usage: ./a.out \n";

else if (argc != 2) cout << "Error: Invalid argument count.\n";

else {

int wantedSize = atoi(argv[1]);

//------- SECTION 1 BUBBLE SORT -------

cout << "BUBBLE SORT" << endl;

vector list = createVector(wantedSize);

cout << "Unsorted: ";

printVector(list);

chrono::steady_clock::time_point start = chrono::steady_clock::now();

// YOUR CODE HERE

// Bubble Sort, Statistics, Reverse

// Sort the vector (1)

bubbleSort(list);

cout << "Sorted: ";

printVector(list);

// Statistics on the vector (2-5)

cout << "Minimum: " << minimum(list) << endl;

cout << "Maximum: " << maximum(list) << endl;

cout << "Sum: " << sum(list) << endl;

cout << "Average: " << average(list) << endl;

// Reverse the vector (6)

reverseVector(list);

cout << "Reverse: ";

printVector(list);

cout << endl;

// END OF YOUR CODE

chrono::steady_clock::time_point end = chrono::steady_clock::now();

cout << "Elapsed time in seconds: "

<< chrono::duration_cast(end - start).count()

<< " microseconds.\n\n";

//------- SECTION 2 SELECTION SORT -------

cout << "SELECTION SORT" << endl;

list = createVector(wantedSize);

cout << "Unsorted: ";

printVector(list);

start = chrono::steady_clock::now();

// YOUR CODE HERE

// Selection Sort, Statistics, Reverse

// Sort the vector (1)

selectionSort(list);

cout << "Sorted: ";

printVector(list);

// Statistics on the vector (2-5)

cout << "Minimum: " << minimum(list) << endl;

cout << "Maximum: " << maximum(list) << endl;

cout << "Sum: " << sum(list) << endl;

cout << "Average: " << average(list) << endl;

// Reverse the vector (6)

reverseVector(list);

cout << "Reverse: ";

printVector(list);

cout << endl;

// END OF YOUR CODE

end = chrono::steady_clock::now();

cout << "Elapsed time in seconds: "

<< chrono::duration_cast(end - start).count()

<< " microseconds.\n\n";

//------- SECTION 3 INSERTION SORT -------

cout << "INSERTION SORT" << endl;

list = createVector(wantedSize);

cout << "Unsorted: ";

printVector(list);

start = chrono::steady_clock::now();

// YOUR CODE HERE

// Insertion Sort, Statistics, Reverse

// Sort the vector (1)

insertionSort(list);

cout << "Sorted: ";

printVector(list);

// Statistics on the vector (2-5)

cout << "Minimum: " << minimum(list) << endl;

cout << "Maximum: " << maximum(list) << endl;

cout << "Sum: " << sum(list) << endl;

cout << "Average: " << average(list) << endl;

// Reverse the vector (6)

reverseVector(list);

cout << "Reverse: ";

printVector(list);

cout << endl;

// END OF YOUR CODE

end = chrono::steady_clock::now();

cout << "Elapsed time in seconds: "

<< chrono::duration_cast(end - start).count()

<< " microseconds.\n\n";

}

return 0;

}