Merge Sort

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • computerfox
    Contributor
    • Mar 2010
    • 276

    Merge Sort

    Hello everyone,

    I'm having some trouble with the code for merge sort. I get what is it but the code doesn't seem to work. Here is my code:


    Code:
    ////.h
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    string merge(string,string);
    string mergeSort(string);
    string rest(int,int,string);
    
    string rest(int from,int to,string phrase){
    	string temp;
    	
    	for(int i=from;i<=to;i++){
    		temp[i-1]=phrase[i];
    	}
    	return temp;
    }
    string merge(string left,string right){
    	string result;
    * * while (left.length() > 0 || right.length() > 0){
    * * * * if (left.length() > 0 && right.length() > 0){
    			if (left[0] <= right[0]){
    * * * * * * * * result+=left[0];
    * * * * * * * * left= rest(1,left.length(),left);
    			}
    			if(left[0]>right[0])
    			* * {
    				result+=right[0];
    				right= rest(1,right.length(),right);
    			}
    				if (left.length() > 0){
    					result+= left[0];
    					left= rest(1,left.length(),left);
    				}
    				if (right.length() > 0){
    					
    				* * result+=right[0];
    					right= rest(1,right.length(),right);
    				}
    			}
    		}
    	return result;	
    }
    string mergeSort(string a){
    	if(a.length()<=1)
    		return a;
    	
    	string left,right,result;
    	int middle=(a.length())/2;
    	
    	for(int i=0;i<a.length();i++){
    		if(i<middle)
    		* left+=a[i];
    		else*
    			if(i>=middle)
    			right+=a[i];
    
    	}
    	left = mergeSort(left);
    * * right = mergeSort(right);
    * * result = merge(left, right);
    * * return result+left;
    }
    
    	
    
    
    ////.cpp
    
    #include "mergeSort.h"
    using namespace std;
    
    int main () {
    * * string a,b;
    	int again=1;
    	while(again==1){
    		
    	* * cout<<"Enter string to sort: ";
    	* * cin>>a;		
    		cout<<"Before the sort: "<<a<<endl;
    		cout<<"After sort: ";
    		cout<<mergeSort(a)<<endl;
    	* * cout<<"1-Again* 2-Close: ";
    		cin>>again;
    	}
    	
    * * return 0;
    }
    Any ideas?
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    Easy fix-

    Code:
    ////.h
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    char *mergeSort(char letters[], char temp[], int array_size);
    char *mSort(char letters[], char temp[], int left, int right);
    char *merge(char letters[], char temp[], int left, int mid, int right);
    
    char *mergeSort(char letters[], char temp[], int array_size)
    {
        return mSort( letters, temp, 0, array_size - 1);
    }
    
    
    char *mSort(char letters[], char temp[], int left, int right)
    {
        int mid;
        
        if (right > left)
        {
            mid = (right + left) / 2;
            mSort( letters, temp, left, mid);
            mSort( letters, temp, (mid+1), right);
            
            merge( letters, temp, left, (mid+1), right);
        }
        
        return letters;
    }
    
    char *merge(char letters[], char temp[], int left, int mid, int right)
    {
        int i, leftEnd, elements, tmpPos;
        
        leftEnd = (mid - 1);
        tmpPos = left;
        elements = (right - left + 1);
        
        while ((left <= leftEnd) && (mid <= right))
        {
            if ( letters[left] <=  letters[mid])
            {
                temp[tmpPos] =  letters[left];
                tmpPos += 1;
                left += 1;
            }
            else
            {
                temp[tmpPos] =  letters[mid];
                tmpPos += 1;
                mid += 1;
            }
        }
        
        while (left <= leftEnd)
        {
            temp[tmpPos] =  letters[left];
            left += 1;
            tmpPos += 1;
        }
        while (mid <= right)
        {
            temp[tmpPos] =  letters[mid];
            mid += 1;
            tmpPos += 1;
        }
        
        for (i=0; i < elements; i++)
        {
            letters[right] = temp[right];
            right -= 1;
        }
        
        return temp;
    }
    
    
    ////.cpp
    
    
    #include "mergeSort.h"
    
    int main()
    {
        int max,again=1;
        
        while(again==1){
        cout<<"Word size: ";
        cin>>max;
    
        char arrayOne[max];
        char arrayTwo[max];
            string result;
            
        cout<<"Enter word to sort: ";
    
        //Enter character array
        for(int i=0;i<max;i++){
            cout<<"Letter"<<i+1<<"/"<<max<<": ";
            cin>>arrayOne[i];
        }
       
        //Before the sort    
        cout<<"Before the sort: ";
        for(int i=0;i<max;i++)
            cout<<arrayOne[i]<<" ";
        cout<<endl;
        
        //Sort
    	result=mergeSort(arrayOne, arrayTwo, max);
        
    	//After the sort
        
        cout<<"After the sort: ";
        for(int i=0;i<max;i++)
            cout<<result[i]<<" ";
        cout<<endl;
        
            cout<<"1-Again  2-Close: ";
            cin>>again;
        }
        
        cout<<endl;
    	return 0;
    }
    Merge sort can not be done with strings, you need a list of either characters or arrays. Despite strings being character arrays, it treats it differently.

    Comment

    Working...