**the code works perfectly, just i got something i dont understand...**

i'v been trying to get a hold of recursion and sorting for about 3-4 days now, im getting some of it, but still not happy enough..

i searched for some good code for binary search and merge sort on the net, found some, took the one i liked most, and have been editing it to my needs. it would have felt a lot better had i been able to write both on my own,

but....

i cant..... :(

(hence all the downloading, editing and testing and comparing ... :| )

well , i have two codes:

- the main binary search code
- the merge sort one, saved as a header file and called from binary search one...

works perfectly, even after all my edits... this is the output......

here is the part of the code that generates it:

```
void partition(int arr[],int low,int high){
int mid;
static int count=0;
printf("inside partition, for this step>> low : %3d & high : %3d , step number: %d\n",low,high,count++);
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
```

the array consists of random numbers (generated via `srand()`

, and `rand()`

inside a `for()`

loop, thus saving me time), and this array is passed to the `partition()`

function contained in my merge-sort header file. everythings fine, just **one** confusion regarding the output:

i dont understand why i get output for cases (step number: 3,4,5 and 8,9,10) when "`low`

" and "`high`

" are equal, where as the `if()`

condition in the `partition()`

block states recursion only when `low<high`

...

i worte the `printf()`

line in `partition()`

just to understand how the recursion was taking place, and this thing... i tried , but couldnt understand...

this is my full mergesort header file:

```
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
void partition(int arr[],int low,int high){
int mid;
static int count=0;
printf("inside partition, for this step>> low : %3d & high : %3d , step number: %d\n",low,high,count++);
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
void mergeSort(int arr[],int low,int mid,int high){
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
```

**also**,

links to the original codes: binary search , merge sort

link to my edited code: binary search ( i didnt want to clutter up the post too much, so just put the link here instead of pasting it ... i hope im not violating any rules...)

learned lots from daniweb in these few weeks... hoping to learn more once again :)

thanks..

somjit :)

p.s: i ended up adding two attachments by mistake, both are same, one's just a crop version of the other... i apologize for any trouble it may cause.

*Edited 4 Years Ago by somjit{}*