Merge Sort

Hey there something is wrong with my code it does not produce the correct output

is merge sort


thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#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:\n";
  for (int i=0; i<num_people; i++)
  {
    cout << " " << people[i].name 
	 << "\t " << 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:\n";
  for (int i=0; i<num_people; i++)
  {
    cout << " " << people[i].name 
	 << "\t " << people[i].height << endl;
  }

  //insert your mergesort function call here!!
  // and dont forget to define what it does!!

  cout << "Sorted alphabetically:\n";
  for (int i=0; i<num_people; i++)
  {
    cout << " " << people[i].name 
	 << "\t " << people[i].height << endl;
  }

  system ("pause");
  return 0;
}

Topic archived. No new replies allowed.