Mergesort using iterators gives nonesense output

Hello together!

I'm supposed to implement a mergesort algorithm using iterators. I've implemented the algorithm as shown in books and on google, but somehow it is deleting elements from my input and returning 2 elements more than once.
Since I don't seem to find the problem, maybe someone of you can help me :)

Here's my code so far:

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
  template <typename srcIterator, typename tempIterator>
void merge(srcIterator left, srcIterator middle, srcIterator right, tempIterator temp){
  auto t = temp;
  auto i = left;
  auto j = ++middle;
  
  // TODO: implement the merge algorithm using auxiliary storage in t
  while(i!=j &&  j!=right){
    if(*i <= *j){
      *t = *i;
      ++i;
    }
    else{
      *t = *j;
      ++j;
    }
    ++t;
  }
  
  while(i!=j){
    *t = *i;
    ++i;
    ++t;}
    
  while(j!=right){
    *t = *j;
    ++j;
    ++t;
  }

  // write back the contents from auxiliary space t to left
  std::copy(temp, t, left);
}

template <typename srcIterator, typename tempIterator>
void merge_sort(srcIterator left, srcIterator right, tempIterator temp){
  
  // can be used in order to get the distance of left and right
  auto dist = std::distance(left, right);
  // can be used in order to traverse forward by half the distance above
  auto m = std::next(left, dist/2);
  
  if(left!=right){
    merge_sort(left, m, temp);
    merge_sort(++m,right, temp);
    merge(left,m,right,temp);
  }
 }


I think maybe the merge_sort function might be working okay and merge is the problem.
(the function declaration isn't supposed to be changed, so I can only work within the function and not change the input or the output type)

as example, i get for this input:
2 3 7 6 4 1
this output:
3 3 2 3 2

Thanks for your help,
Marie
Last edited on
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
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

template <typename srcIterator, typename tempIterator>
void merge(
    srcIterator  left,
    srcIterator  middle,
    srcIterator  right,
    tempIterator temp)
{
    auto t = temp;
    auto i = left;
    auto j = middle;
    while (i != middle && j != right) *t++ = *i <= *j ? *i++ : *j++;
    while (i != middle) *t++ = *i++;
    while (j != right ) *t++ = *j++;
    std::copy(temp, t, left);
}

template <typename srcIterator, typename tempIterator>
void merge_sort(
    srcIterator  left,
    srcIterator  right,
    tempIterator temp)
{
    auto dist = std::distance(left, right);
    if (dist > 1) {
        auto m = std::next(left, dist / 2);
        merge_sort(left, m, temp);
        merge_sort(m, right, temp);
        merge(left, m, right, temp);
    }
}

int main()
{
    std::vector<int> v { 5, 3, 9, 1, 8, 0, 2, 6, 4, 7 };
    std::vector<int> t(v.size());
    merge_sort(v.begin(), v.end(), t.begin());
    for (int n: v) std::cout << n << ' ';
    std::cout << '\n';
}

Thanks a lot for helping out!
Topic archived. No new replies allowed.