1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need yuors help

Discussion in 'C' started by tzahi, Dec 22, 2007.

  1. tzahi

    tzahi New Member

    Joined:
    Dec 22, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Hello.
    I need to write a program in c that make merge Sort
    the program will be writing in Recursion but i need to write in without loops

    i wrot a program that make make merge in Recursion but with loops
    and now i try to write it without loops and it doest work

    if someone can write me the program i will thank him a lot !!!

    bye,

    here is a link how its need to work (but here is with loops )
    http://en.wikipedia.org/wiki/Merge_sort
     
  2. tzahi

    tzahi New Member

    Joined:
    Dec 22, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    someone???
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Why don't you share your code so that we don't need to be doing it all over again.
     
  4. tzahi

    tzahi New Member

    Joined:
    Dec 22, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    I have only the code with loops
    i delete the the code that i try to write without loops
    Do you whnt me to share the code with the loops ???
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
  6. tzahi

    tzahi New Member

    Joined:
    Dec 22, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    this is my code if you can fix by take out the loops and it still do merge Sort :

    Code:
    void mergeSort(int numbers[], int temp[], int array_size)
    {
      m_sort(numbers, temp, 0, array_size - 1);
    }
    
    
    void m_sort(int numbers[], int temp[], int left, int right)
    {
      int mid;
    
      if (right > left)
      {
        mid = (right + left) / 2;
        m_sort(numbers, temp, left, mid);
        m_sort(numbers, temp, mid+1, right);
    
        merge(numbers, temp, left, mid+1, right);
      }
    }
    
    void merge(int numbers[], int temp[], int left, int mid, int right)
    {
      int i, left_end, num_elements, tmp_pos;
    
      left_end = mid - 1;
      tmp_pos = left;
      num_elements = right - left + 1;
    
      while ((left <= left_end) && (mid <= right))
      {
        if (numbers[left] <= numbers[mid])
        {
          temp[tmp_pos] = numbers[left];
          tmp_pos = tmp_pos + 1;
          left = left +1;
        }
        else
        {
          temp[tmp_pos] = numbers[mid];
          tmp_pos = tmp_pos + 1;
          mid = mid + 1;
        }
      }
    
      while (left <= left_end)
      {
        temp[tmp_pos] = numbers[left];
        left = left + 1;
        tmp_pos = tmp_pos + 1;
      }
      while (mid <= right)
      {
        temp[tmp_pos] = numbers[mid];
        mid = mid + 1;
        tmp_pos = tmp_pos + 1;
      }
    
      for (i=0; i <= num_elements; i++)
      {
        numbers[right] = temp[right];
        right = right - 1;
      }
    }
     
    Last edited by a moderator: Dec 23, 2007
  7. tzahi

    tzahi New Member

    Joined:
    Dec 22, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Please someone can help me i need to submit the program Until tomorrow !!!
     

Share This Page