Merge sort....with Resursion

This is a discussion on Merge sort....with Resursion within the C Programming forums, part of the General Programming Boards category; /* I am a beginer of C.... It is a merge sort using Resursion.... It can't sort now..... Anyone can ...

  1. #1
    frankiepoon
    Guest

    Merge sort....with Resursion

    /*
    I am a beginer of C....
    It is a merge sort using Resursion....
    It can't sort now.....
    Anyone can tell me where is the problems in the program?
    Thank you very much!!
    */
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define FILENAME "input.dat"
    #define MAXSIZE 200
    int temp[110];
    int readfile(int data[],int size);
    
    void mergesort(int merge[],int base,int size);
    void merges(int merge[],int p,int l_end,int q,int r_end);
    
    
    main(){
        int data[MAXSIZE];
        int insert[MAXSIZE];
    	int merge[MAXSIZE];
    	int quick[MAXSIZE];
    	int size;
    	int i;
    	int base=0;
    
    	/*intilize the array data*/
        for(i=0;i<=MAXSIZE-1;i++){
    	data[i]=0;
    	}
        
    	size=readfile(data,MAXSIZE);
    
    	for(i=0;i<=size-1;i++){
    	insert[i]=data[i];
    	merge[i]=data[i];
    	quick[i]=data[i];
    	}
    
       
        base=0;
    
        mergesort(merge,base,size);
    
       
    	printf("Orignal    New order\n");
    	for(i=0;i<=size-1;i++){
            printf("%d    ",merge[i]);
    	printf("       %d \n",temp[i]);
    	}
        
       	/*for(i=0;i<=size-1;i++){
    	printf("%d \n",temp[i]);
    	}*/
        
    
    }
    
    
    
    /*merge sort*/
    void mergesort(int merge[],int base,int size){
        int mid;
    	if(base>=size) return;
    	
    	mid=(base+size)/2;	
    
    	mergesort(merge,base,mid);
        mergesort(merge,mid+1,size);
        
    	merges(merge,base,mid,mid+1,size);
    }
    
    /*merge*/
    void merges(int merge[],int p,int l_end,int q,int r_end){
    int pos;
    
    pos=0;
    
        while((p<=l_end )&&(q<=r_end))
    		 if (merge[p]<=merge[q])
    	        temp[pos++]=merge[p++];
    		 else
    			temp[pos++]=merge[q++];
    	
    	while(p<=l_end)
    		    temp[pos++]=merge[p++];
    	while(q<=r_end)
    		    temp[pos++]=merge[q++];
    
    
    }
    
    
    
    
    /* Read file from input.dat */
    int readfile(int data[],int size){
      int i;
      FILE *in_file;
      if((in_file=fopen(FILENAME,"r"))==NULL)
      {
    	  printf("ERROR OF FILE OPEN!");
      }else{
    
     
          i=0;
    	  while(fscanf(in_file,"%i",&data[i])!=EOF){
    	  i=i+1;
    	  }
     
    
        fclose(in_file);
      }
    
    
    return i;
    }

    CODE TAGS added by Hammer

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Break your code down to just the merge sort and you can easily figure it out from there, here's my go at it by modifying your code :-)
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAXSIZE 10
    
    void mergesort(int merge[],int base,int size);
    void merges(int merge[],int left,int mid,int right);
    
    int main(void)
    {
      int i;
      int merge[MAXSIZE] = {5,4,6,3,7,2,8,1,9};
    
      printf("Orignal\n");
      for(i = 0; i < 9; i++)
      {
        printf("%d ", merge[i]);
        fflush(stdout);
      }
    
      printf("\n");
      mergesort(merge, 0, 9);
    
      printf("New Order\n");
      for(i = 0; i < 9; i++)
      {
        printf("%d ", merge[i]);
        fflush(stdout);
      }
    
      printf("\n");
    }
    
    void mergesort(int merge[],int left,int right)
    {
      int mid = (right+left)/2;
      
      if(right <= left)
      {
        return;
      }
      
      mergesort(merge, left, mid);
      mergesort(merge, mid+1, right);
      merges(merge, left, mid, right);
    }
    
    void merges(int merge[],int left,int mid,int right)
    {
      int i, j, k;
      int temp[MAXSIZE];
    
      for (i = mid+1; i > left; i--)
      {
        temp[i-1] = merge[i-1];
      }
      for (j = mid; j < right; j++)
      {
        temp[right+mid-j] = merge[j+1];
      }
      for (k = left; k <= right; k++)
      {
        if (temp[j] < temp[i])
        {
          merge[k] = temp[j--];
        }
        else
        {
          merge[k] = temp[i++];
        }
      }
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge Sort the best sorting algorithm?
    By sehr alt in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 06-20-2007, 02:19 PM
  2. Merge Sort Help
    By blindman858 in forum C++ Programming
    Replies: 5
    Last Post: 05-14-2005, 09:47 PM
  3. Merge Sort
    By blindman858 in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2005, 11:14 AM
  4. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  5. help with merge sort
    By maloy in forum C Programming
    Replies: 4
    Last Post: 03-04-2002, 05:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21