answersLogoWhite

0

// answer in C-style pseudo-code

// all division should be rounded up

// search will return the index of the number found, or -1 if it was not found

// nums is the array of numbers we're searching through

// num is the specific number we want to find in nums

// current is the index we are currently inspecting

// start is the first index of nums (typically 0)

// end is the last index of nums (typically the length - 1)

// preconditions: nums must be sorted or function is nondeterministic

int search( int[] nums, int num, int current, int start, int end ) {

if( nums[current] end ) { // target not in list

return -1;

}else if( nums[current] > num ) { // search to the left

return search( nums, num, current - ((current - start)/2), start, current );

}else if( nums[current] < num ) { // search to the right

return search( nums, num, current + ((end - current)/2), current, end );

}

}

// convenience function to start the search

int search( int[] nums, int num ) {

return search(nums, num, (nums.length/2), 0, nums.length - 1);

}

User Avatar

Wiki User

16y ago

What else can I help you with?

Continue Learning about Engineering

What search algorithm locates an item in an array by repeatedly dividing the array in half?

half means 1/2 from the whole (previous), which means 2 of 1/2, and 2 derived into binary. Ha, Binary Search is the term.


A binary search of an orderd set of elements in an array or a sequential search of the elements.Which one is faster?

A binary search is much faster.


Is it true In a binary search if the search fails to find the item on the first attempt then there are 999 elements left to search in an array of 1000 elements?

False. In a binary search, if the search fails on the first trial of an array of 1000 elements, then there are only nine more elements left to search.


Why binary search algorithm did not work on an unsorted array?

The binary search algorithm works by successively halving the array and determining which half the result lies in, or if the half-way point is the result. In order for that to work, the array must be in order, otherwise choosing the half-way point would be meaningless because it would not tell you which half of the array the result is located in.


Write a c program for binary searching?

/* C program for binary search: This code implements binary search in */ /* C language. It can only be used for sorted arrays, but it's fast as */ /* compared to linear search. If you wish to use binary search on an */ /* array which is not sorted then you must sort it using some sorting */ /* technique say merge sort and then use binary search algorithm to */ /* find the desired element in the list. If the element to be searched */ /* is found then its position is printed. */ #include&lt;stdio.h&gt; main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&amp;n); printf("Enter %d integers\n", n); for ( c = 0 ; c &lt; n ; c++ ) scanf("%d",&amp;array[c]); printf("Enter value to find\n"); scanf("%d",&amp;search); first = 0; last = n - 1; middle = (first+last)/2; while( first &lt;= last ) { if ( array[middle] &lt; search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if ( first &gt; last ) printf("Not found! %d is not present in the list.\n", search); return 0; }

Related Questions

What is the best search algorithm to use for a sorted array?

The best search algorithm to use for a sorted array is the binary search algorithm.


What search algorithm locates an item in an array by repeatedly dividing the array in half?

half means 1/2 from the whole (previous), which means 2 of 1/2, and 2 derived into binary. Ha, Binary Search is the term.


A binary search of an orderd set of elements in an array or a sequential search of the elements.Which one is faster?

A binary search is much faster.


Is it true In a binary search if the search fails to find the item on the first attempt then there are 999 elements left to search in an array of 1000 elements?

False. In a binary search, if the search fails on the first trial of an array of 1000 elements, then there are only nine more elements left to search.


How many comparisons are typically made in a binary search algorithm when searching for a specific element in a sorted array?

In a binary search algorithm, typically log(n) comparisons are made when searching for a specific element in a sorted array, where n is the number of elements in the array.


How many comparisons are typically required in a binary search algorithm to find a specific element in a sorted array?

In a binary search algorithm, typically log(n) comparisons are required to find a specific element in a sorted array, where n is the number of elements in the array.


What is the time complexity of an algorithm that utilizes a binary search algorithm to search through a sorted array, where the search time is represented by the function log(n) in terms of the input size n?

The time complexity of an algorithm that uses a binary search on a sorted array is O(log n), where n is the size of the input array.


What is the maximum number of comparisons required in a binary search algorithm to find a specific element in a sorted array?

The maximum number of comparisons required in a binary search algorithm to find a specific element in a sorted array is log(n), where n is the number of elements in the array.


What is the time complexity of an algorithm that uses a binary search to find an element in a sorted array in logn time?

The time complexity of an algorithm that uses binary search to find an element in a sorted array in logn time is O(log n).


What is the running time of binary search algorithm?

The running time of the binary search algorithm is O(log n), where n is the number of elements in the sorted array being searched.


What is the time complexity of a binary search algorithm?

The time complexity of a binary search algorithm is O(log n), where n is the number of elements in the sorted array being searched.


Why binary search algorithm did not work on an unsorted array?

The binary search algorithm works by successively halving the array and determining which half the result lies in, or if the half-way point is the result. In order for that to work, the array must be in order, otherwise choosing the half-way point would be meaningless because it would not tell you which half of the array the result is located in.