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......................
This is a discussion on divide and conquer within the C Programming forums, part of the General Programming Boards category; How do I divide an array. In my book there is nothing about dividing up an array. I just need ...
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......................
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:
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.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; }
-Prelude
My best code is written with the delete key.