1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Recursive Merg Sort c++

Discussion in 'C++' started by luckyali444, Jan 5, 2012.

  1. luckyali444

    luckyali444 New Member

    Joined:
    Jan 5, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I'm trying to do Merg sort recursively. But the problem is, i am unable to find out the base case when i split an integer array in to sub arrays. Below is my that i have written for Merg sort.

    Code:
    void Merg(int A[], int s1, int e1, int s2, int e2) {  	int B[8]; 	int i=0;  	while (A[s1] < A[s2]) 			 		B[i] = B[s1]; 		i++; 		s1++;  		if (s1 == e1) 		{ 			B[i] = A[s2]; 			i++; 			s2++; 		} 			 	while (A[s2] < A[s1]) 		 		 		B[i] = B[s2]; 		i++; 		s2++;  		if (s2 == e2) 		{ 			B[i] = A[s1]; 			i++; 			s1++; 				 		} }    void Split(int A[], int s, int e) { 	int mid = (s+e)/2;  	if (s < e && mid != 0) 	{	 		Split(A, s, mid); 		Split(A, mid+1, e);	 	} 	Merg(A, s, mid, mid+1, e); }     int main() { 	int A[8] = {10,4,8,12,11,2,7,5}; 	 	Split(A, 0, 7);   	return 0; }
    
     

Share This Page