Thread: Merge two sorted arrays recursively

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    13

    Merge two sorted arrays recursively

    Hi, the exercise is this:
    I got two sorted arrays (of any length) and I got to merge them into a new array (of correct length, and still sorted), recursively.

    I solved the problem like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void merge(int *v1, int *v2, int l1, int l2, int *v3);
    int *newArray(int n);
    
    int main (void) {
        int *v1, *v2, *v3;
        int i, l1, l2, l3;
    
        v1 = v2 = v3 = NULL;
    
        printf("Enter the numbers in increasing order.\n\n");
    
        printf("First's array length: ");
        do{
            scanf("%d", &l1);
        }while(l1 <= 0);
    
        v1 = newArray(l1);
    
        printf("\nSecond's array length: ");
        do{
            scanf("%d", &l2);
        }while(l2 <= 0);
    
        v2 = newArray(l2);
    
        l3 = l1 + l2;
    
        v3 = malloc(l3 * sizeof(int));
        if(!v3)
            return -1;
    
        merge(v1, v2, l1, l2, v3);
    
        printf("\nResult:\n");
        for(i=0; i<l3; ++i)
            printf("%d ", v3[i]);
    
        return 0;
    }
    
    void merge(int *v1, int *v2, int l1, int l2, int *v3){
        if(l1 && l2){ // verifying if both vectors got elements
            if(*v1 <= *v2){
                *v3 = *v1;
                merge(++v1, v2, --l1, l2, ++v3);
            }
            else{
                *v3 = *v2;
                merge(v1, ++v2, l1, --l2, ++v3); // notare ++v2 e --l2 e ++v3
            }
        }
    
        else if(l1){ // verifying if only v1 got elements
            *v3 = *v1;
            merge(++v1, v2, --l1, l2, ++v3);
        }
    
        else if(l2){ // verifying if only v2 got elements
            *v3 = *v2;
            merge(v1, ++v2, l1, --l2, ++v3);
        }
    }
    
    int *newArray(int n){
        int *v = NULL;
        int i;
    
        v = malloc(n * sizeof(int));
    
        if(!v)
            return NULL;
    
        for(i=0; i<n; ++i){
            printf("[%d]: ", i);
            scanf("%d", &v[i]);
        }
    
        return v;
    }
    My solution seems to work well. I only got a doubt about how I solved it, it is: is there a way to make it in the form "v3 = merge(parameters);" ?
    I would like to know if I can do it with returns... it would be a non-void function.

    Thanks
    Last edited by kr0n1x; 06-04-2012 at 09:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you merge arrays?
    By SeriousTyro in forum C Programming
    Replies: 14
    Last Post: 09-21-2009, 10:38 AM
  2. how to merge two arrays recursevly..
    By transgalactic2 in forum C Programming
    Replies: 117
    Last Post: 01-11-2009, 04:47 PM
  3. Merge sort (sorted arrays)
    By myle in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2007, 02:48 AM
  4. ordered binary tree
    By Ami_01 in forum C Programming
    Replies: 3
    Last Post: 07-05-2005, 06:44 PM
  5. How to merge 2 arrays?
    By planet_abhi in forum C Programming
    Replies: 3
    Last Post: 02-16-2003, 12:23 AM

Tags for this Thread