×
Reviews 4.9/5 Order Now

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

July 15, 2024
Professor Marcus Tan
Professor Marcus
🇸🇬 Singapore
C++
Professor Marcus Tan is a seasoned programmer with a master's degree in computer science from the National University of Singapore. With 850 assignments under his belt, he specializes in error handling mechanisms in file operations. Professor Tan's thorough understanding of exception handling techniques ensures reliable and resilient file processing solutions for C++ assignments.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Break your NetLogo model into simple procedures using functions for better readability. Use the ask command efficiently to control agents and optimize performance by minimizing unnecessary computations in the go procedure. Always test your model with small agent populations before scaling up.
News
LFortran Advances: LFortran, an open-source Fortran compiler, achieved compatibility with the PRIMA library in March 2025, enhancing support for numerical computing.

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

Similar Sample

Explore our C++ assignment sample to see the quality and depth of our work. This example showcases our expertise in C++ programming, providing clear, well-commented code solutions and thorough explanations. Trust Programming Homework Help to deliver top-notch assistance for your C++ projects and assignments.