Thread: divide and conquer

  1. #1
    Unregistered
    Guest

    divide and conquer

    How do I divide an array. In my book there is nothing about dividing up an array. I just need that part the rest I can figure out. It for dividing up an array and then putting it back together sorted. Can anyone help. THanx......................

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Use indices. A simple binary search will create a variable that points to the middle of the array, if the item being searched for is greater than that element, the middle of the array becomes the low end and a new middle is found:
    Code:
    int search ( int a[], int key, size_t size )
    {
      int low = 0,
          high = size - 1,
          mid;
      while ( low <= high ) {
        mid = ( high + low ) / 2;
        if ( key < a[mid] )
          high = mid - 1;
        else if ( key > a[mid] )
          low = mid;
        else
          return mid;
      }
      return NOT_FOUND;
    }
    The mid variable is what divides the array. Another alternative is to place half of the array in another array and then zero out that half of the original array and perform your operations. Though this is a bit silly since an array is only as big as you tell the compiler when performing operations such as sorting.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is the difference between Recursion and Iteration?
    By neo_phyte in forum C Programming
    Replies: 12
    Last Post: 09-13-2006, 02:13 AM
  2. Divide and Conquer: array in the reverse order
    By Steamer in forum C Programming
    Replies: 11
    Last Post: 03-08-2004, 07:31 PM