Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have some code here that I need detailed comments on just about every line so

ID: 3640723 • Letter: I

Question

I have some code here that I need detailed comments on just about every line so I can use it to study off of.
The best way is to assume that I don't know anything. Even variable declaration and just add comments to all line. It doesn't have to be long comments just as long as I or anyone can see what each line is doing.


Those that help completely I will give full helpful review
--------------------------------------------------------------------------
Merge Sort: uses inplace_merge() from STL.
*********************************************/

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template <class Itr>
void m_sort(Itr start, unsigned int low, unsigned int high)
{
if (low + 1 < high) {
unsigned int center = (low + high) / 2;
m_sort(start, low, center);
m_sort(start, center, high);
inplace_merge(start+low, start+center, start+high);
}
} // m_sort

template <class T>
void mergeSort(vector<T> & s)
{
m_sort(s.begin(), 0, s.size());
} // mergeSort

main()
{
vector<int> v;
int temp;

while (cin >> temp)
v.push_back(temp);

mergeSort(v);

for (int i = 0; i < v.size(); i++)
cout << v[i] << ' ';
cout << endl;
} // main

------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

template <class Itr>
void inplaceMerge(Itr start, Itr center, Itr stop)
{
int dist = stop - start;
vector<typename iterator_traits<Itr>::value_type> temp(dist);
merge(start, center, center, stop, temp.begin());
copy(temp.begin(), temp.end(), start);
}

template <class Itr>
void m_sort(Itr start, unsigned int low, unsigned int high)
{
if (low + 1 < high) {
unsigned int center = (low + high) / 2;
m_sort(start, low, center);
m_sort(start, center, high);
inplaceMerge(start+low, start+center, start+high);
}
} // m_sort

template <class T>
void mergeSort(vector<T> & s)
{
m_sort(s.begin(), 0, s.size());
} // mergeSort

main()
{
vector<int> v;
int temp;

while (cin >> temp)
v.push_back(temp);

mergeSort(v);

for (int i = 0; i < v.size(); i++)
cout << v[i] << ' ';
cout << endl;
} // main

-------------------------------------------------------------------
Merge Sort: this version illustrates one implementation of Inplace Merge.
*************************************************************************/

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

template<class Itr>
void inplaceMerge(Itr low, Itr mid, Itr high)
{
// int dist = high - low;
// the following line works too:
typename iterator_traits<Itr>::difference_type dist = high - low;
vector<typename iterator_traits<Itr>::value_type> temp(dist);
Itr i = low;
Itr j = mid;
Itr k = temp.begin();

while (i < mid && j < high)
if (*i < *j)
*k++ = *i++;
else
*k++ = *j++;
while (i < mid)
*k++ = *i++;
while (j < high)
*k++ = *j++;

for (k = temp.begin(), i = low; k != temp.end(); k++, i++)
*i = *k;
} // inplaceMerge


template <class Itr>
void m_sort(Itr start, unsigned int low, unsigned int high)
{
if (low + 1 < high) {
unsigned int center = (low + high) / 2;
m_sort(start, low, center);
m_sort(start, center, high);
inplaceMerge(start+low, start+center, start+high);
}
} // m_sort

template <class T>
void mergeSort(vector<T> & s)
{
m_sort(s.begin(), 0, s.size());
} // mergeSort

main()
{
vector<int> v;
int temp;

while (cin >> temp)
v.push_back(temp);

mergeSort(v);

for (int i = 0; i < v.size(); i++)
cout << v[i] << ' ';
cout << endl;
} // main

Explanation / Answer

-------------------------------------------------------------------------- Merge Sort: uses inplace_merge() from STL. *********************************************/ //libs #include #include #include using namespace std; //sorting method between low and high on any type argument start, expressed later // as iterator template void m_sort(Itr start, unsigned int low, unsigned int high) { //calculate center again if (low + 1 returns X-th element position from the // start element, a bit like a pointer to an element in the starting collection. This //method from STL will basically take two lists and merge them together based on //the ordering, so [1,5] and [2,4] would result in [1,2,4,5] inplace_merge(start+low, start+center, start+high); } } // m_sort //simple wrapper method: for a list, sort it from beginning of iterator, for range 0 - //size template void mergeSort(vector & s) { m_sort(s.begin(), 0, s.size()); } // mergeSort main() { vector v; int temp; //read in data from a file while (cin >> temp) v.push_back(temp); //sort mergeSort(v); //print the results for (int i = 0; i