Merge Sort

Hello dear Community

I'm learning C++, so I thought to implement MergeSort. I've written this code, but the problem is that some array elements are double and some are missing, so I must be overwriting myArray in some wrong way...but I can't find it :-(
Thanks for help ;-)

PS: for those who don't understand german: rechts= rigth, links=left, mitte=center


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
void mergeSort(int* myArray, int l, int r)
{
	//more than one Element
	if(l<r)
	{
	   int mitte= (l+r)/2;
	   mergeSort(myArray,l,mitte); 
	   mergeSort(myArray,mitte+1,r); 
	   merge(myArray,l,mitte,r);
	}
}


void merge(int* myArray, int links, int mitte, int rechts)
{
   int length=rechts-links+1; 
   int* helper=new int[length];
   int lengthLeftArr=mitte-links+1;
   int lengthRightArr=rechts-mitte; 
   
   int copiedL=0, copiedR=0, copiedGes=0; 
    
   while(copiedL <= lengthLeftArr && copiedR<= lengthRightArr)
   {
      if(myArray[links+copiedL] < myArray[mitte+1+copiedR])
      {
      	helper[copiedGes]=myArray[links+copiedL];
      	copiedL++;
      	copiedGes++;
      }
      
      else
      {
      	helper[copiedGes]=myArray[mitte+1+copiedR];
      	copiedR++;
      	copiedGes++;
      }
   }
   
   
   // copy the rest into the helperArray
   while(copiedL <=lengthLeftArr)
   {
   		helper[copiedGes]=myArray[links+copiedL];
   		copiedL++;
   		copiedGes++;
   }
   
   while(copiedR <=lengthRightArr)
   {
   		helper[copiedGes]=myArray[mitte+1+copiedR];
   		copiedR++;
   		copiedGes++;
   }
   
   //copy back to initial array
   
   for(int i=0; i<length ;i++)
   {
   		myArray[links]=helper[i];
   		links++;
   		
   }
   
   delete[] helper;
}
Topic archived. No new replies allowed.