Question: Implementing Merge Sort with Vectors and Exception Handling Introduction to Merge Sort: Merge Sort is a divide - and - conquer sorting algorithm that divides
Implementing Merge Sort with Vectors and Exception Handling
Introduction to Merge Sort:
Merge Sort is a divideandconquer sorting algorithm that divides an array or vector into two
halves, recursively sorts each half, and then merges the sorted halves to produce a fully sorted
array. This algorithm is efficient for large datasets.
In this exercise, you will implement the Merge Sort algorithm while working with vectors and
handling exceptions. The exercise includes several functions to manage vector operations and
the implementation of Merge Sort for sorting a vector of integers.
Your Tasks:
Create a function populateVectorstd::vector& vec, int n:
This function should take a reference to a std::vector and an integer n
Populate the vector with n random integers between and
Create a function printVectorconst std::vector& vec:
Takes a constant reference to a std::vector.
Prints its elements.
Create a function int getElementconst std::vector& vec, int index:
Tries to access the element at the specified index in the vector.
Throws an std::outofrange exception with a suitable error message if the index is out
of bounds.
Write a function mergeSortstd::vector& vec, char direction:
Implement the Merge Sort method to sort the vector in the specified direction a for
ascending, d for descending Use the at function to access elements and include try
catch blocks to handle exceptions. Then, display the result.
In your main function:
a Read numbers from a file, ask the user how they would like to sort the numbers in
ascending or descending order sort them, and display the result.
b Prompt the user to enter the size for creating a new vector and display it
c Ask the user to enter an index to retrieve an element value from this new vector. Use
trycatch blocks to handle any potential exceptions thrown when accessing the vector
elemen
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
