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

    Jan 5, 2012
    Likes Received:
    Trophy Points:
    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.

    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