problem regarding mergesort

hello,
this is my first thread at this site,i was wondering if you could tell mewhy my mergesort wasn't working correctly

here is code:


#include "stdafx.h"
#include <iostream>
using namespace std;
typedef unsigned int uint;

void main(void)
{
int arr1[10]={1,6,4,50,45,23,2,2,77,6};
mergesort_in_place(arr1,10);
printf("\n");
for(int i=0;i<10;i++) {printf(" %d",arr1[i]);}
getch();
}

void rotate_r(int *arr,int start,int end)//working/remember that arr[i1] hold original value
//rotate array right
{
int k;
for(k=0;k<=(end-start);k++) arr[end-k+1]=arr[end-k];
}
void merge(int * arr, uint p, uint q, uint r)
//merge ip,first function
{
int tmp=0;
while((q+1) <= r && p <= q)
{
if(arr[p]>=arr[q+1]) //rotating and replecment segment
{
tmp=arr[q+1];
rotate_r(arr,p,q);
arr[p]=tmp;
p++; q++;
}

else p++;
}

}
void mergesort(int * arr,uint length)
//mergesort with places
{
if(length==1) return;
mergesort(arr,length/2);
mergesort(arr,length-length/2);
merge(arr,1,length/2,length);//p,q,r
}
the result from this code :
1 2 2 4 6 50 45 23 77 6
instead of sorting the whole array is sorted only less then half

Last edited on
[code]
#include <iostream>

int main(){
std::cout <<"Hello, World!"<<std::endl;
return 0;
}
[/code]
becomes
1
2
3
4
5
6
#include <iostream>

int main(){
    std::cout <<"Hello, World!"<<std::endl;
    return 0;
}
Topic archived. No new replies allowed.