Description
Requirements
You are allowed to write everything in one file this time, so long it is organized and the file is not too long. If the file gets too long, please split it into multiple .h
 and .cpp
 files, and have a main.cpp
 that #include
s the .h
 files, and can be used to call the functions prototyped in them.
All the files you produce must be able to exist within a single project. You will submit these files via github as normal.
Setup
- Write a function namedÂ
print_vector
 that takes aÂconst std::vector<int> &
 as an argument, and outputs the contents (separated by whitespace) to stdout (followed by a newline). - Write theÂ
main
 function. It doesn’t need to do anything right now, but as you write the functions below, you should put code inÂmain
 to test them.
Selection Sort
Write a function named selection_sort
 that takes a std::vector<int> &
 as an argument, and sorts it in-place. The algorithm is as follows:
- For the index of each element in the array (the “current index”)
- Find the index of the minimum element at or after the current index.
- Swap the element at the current index with the smallest element found.
For a more thorough explanation, google around, or see the relevant Wikipedia entry.
Merge Sort
Write a function named merge_sort
 that takes as an argument a const std::vector<int> &
 and returns a std::vector<int>
containing all the elements of the vector that was passed as an argument, in ascending order. The algorithm (which is recursive) is as follows:
- If the vector contains only 1 element, return the vector unchanged.
- Otherwise, split the vector into two halves, namedÂ
left
 andÂright
. - Recursively sort each half (i.e. callÂ
merge_sort(left);
 andÂmerge_sort(right);
). - MergeÂ
left
 andÂright
 into a new vector namedÂsorted
, in the following manner:- As long asÂ
left
 andÂright
 both have elements not inÂsorted
, compare the smallest such elements of each list, take the smaller of the two, and append it toÂsorted
. - Once all of the elements of eitherÂ
left
 orÂright
 are inÂsorted
, take the leftover elements and append them toÂsorted
.
- As long asÂ
- ReturnÂ
sorted
.
For a more thorough explanation, google around, or see the relevant Wikipedia entry.
You may need to search around for the best way to split a vector into two smaller vectors. Here’s one way from stackoverflow.
Requests
- Please try to do this in groups, and try to use Google, etc., only for answers to questions about the language (as opposed to questions about this problem).
Assumptions
(None)
Style
- Place your solution in aÂ
solution--YOURNAME
 subdirectory (whereÂYOURNAME
 is your GitHub username). - Include your copyright and license information at the top of every file, followed by a brief description of the file’s contents, e.g.
/* ---------------------------------------------------------------------------- * Copyright © 2016 Ben Blazak <[email protected]> * Released under the [MIT License] (http://opensource.org/licenses/MIT) * ------------------------------------------------------------------------- */ /** * A short program to print "Hello World!" to standard output. */
- Use “include guards” in allÂ
.h
 files. Be sure to give the preprocessor variable a name corresponding to the file name. For example, inÂpoint.h
:#ifndef POINT_H #define POINT_H // ---------------------------------------------------------------------------- // ... everything besides the copyright information and file description // ---------------------------------------------------------------------------- #endif // POINT_H
main()
 must have its ownÂ.cpp
 file. I suggest calling itÂmain.cpp
.- Classes must have bothÂ
.h
 andÂ.cpp
 files, with member functions defined in theÂ.cpp
 files unless they are truly trivial. If it makes sense, you may put multiple classes into one pair ofÂ.h
 andÂ.cpp
 files. - Declare member functions and function arguments asÂ
const
 when appropriate (in general, whenever possible). - Document and format your code well and consistently. Be sure to clearly document the source of any code, algorithm, information, etc. that you use or reference while completing your work.
- Wrap lines at 79 or 80 columns whenever possible.
- End your file with a blank line.
- Do not useÂ
using namespace std;
. You may get around typingÂstd::
 in front of things or with, e.g.,Âusing std::cout;
.