#include <iostream> #include <string> using namespace std; struct person { strin
ID: 3650781 • Letter: #
Question
#include <iostream>#include <string>
using namespace std;
struct person
{
string name;
double height;
};
//merge function
void mergeBack(person a[], int start, int mid, int end);
void mergeSortDouble(person a[], int start, int end)
{
int middle;
if(end > start)
{
middle = (start+end)/2;
mergeSortDouble(a, start, middle);
mergeSortDouble(a,middle + 1, end);
mergeBack(a,start, middle, end);
}
}
void mergeBack(person a[], int start, int mid, int end)
{
person*temp = new person[end-start+1];
int position = start;
int position2 = mid + 1;
int t = 0;
while((position<=(mid))&&(position2<=end))
{
if(a[position].height<=a[position2].height){
temp[t].height=a[position].height;
temp[t].name = a[position].name;
position++;
t++;
}
else
{
temp[t].height=a[position2].height;
temp[t].name = a[position2].name;
position2++;
t++;
}
}
while (position <= (mid))
{
temp[t].height=a[position].height;
temp[t].name = a[position].name;
position++;
t++;
}
while(position2 <= end)
{
temp[t].height=a[position2].height;
temp[t].name = a[position2].name;
position2++;
t++;
}
for(int i = 0; i <= 0; i++)
{
a[start+i].height = temp[i].height;
a[start+i].name = temp[i].name;
}
//delete temp;
}
int main()
{
const int num_people = 10;
person people[num_people];
people[0].name = "Mulan";
people[0].height = 157.5;
people[1].name = "Tiana";
people[1].height = 165;
people[2].name = "Aladdin";
people[2].height = 177.5;
people[3].name = "Esmeralda";
people[3].height = 170;
people[4].name = "Gaston";
people[4].height = 190;
people[5].name = "Donald";
people[5].height = 137;
people[6].name = "Daisy";
people[6].height = 135;
people[7].name = "Baloo";
people[7].height = 188;
people[8].name = "Goofy";
people[8].height = 185.5;
people[9].name = "Mary";
people[9].height = 167.5;
cout << "Unsorted: ";
for (int i=0; i<num_people; i++)
{
cout << " " << people[i].name
<< " " << people[i].height << endl;
}
//insert your mergesort function call here!!
// and dont forget to define what it does!!
mergeSortDouble(people, 0, num_people-1);
cout << "Sorted by height: ";
for (int i=0; i<num_people; i++)
{
cout << " " << people[i].name
<< " " << people[i].height << endl;
}
//insert your mergesort function call here!!
// and dont forget to define what it does!!
cout << "Sorted alphabetically: ";
for (int i=0; i<num_people; i++)
{
cout << " " << people[i].name
<< " " << people[i].height << endl;
}
system ("pause");
return 0;
}
Explanation / Answer
#include #include using namespace std; struct person { string name; double height; }; //merge function void mergeBack(person a[], int start, int mid, int end); void mergeBackString(person a[], int start, int mid, int end); void mergeSortDouble(person a[], int start, int end) { int middle; if(end > start) { middle = (start+end)/2; mergeSortDouble(a, start, middle); mergeSortDouble(a,middle + 1, end); mergeBack(a,start, middle, end); } } void mergeBack(person a[], int start, int mid, int end) { person*temp = new person[end-start+1]; int position = start; int position2 = mid + 1; int t = 0; while((positionRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.