{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

homework3-fa2008 - Computer Science and Engineering 331...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science and Engineering 331 Computer Organization and Design Fall 2008 Homework #3. MIPS Programming Assignment Points: 15 (Pair programming assignment) Due: Thursday, Sept. 18, 2008 by 11:55pm You are to implement an iterative version of the binary search algorithm. The high level code for binary search is given below. It takes as input a sorted array of numbers and an integer called the search_value . If the search_value exists as a number in the array, then the position of the search_value is reported. Otherwise, failure is reported. //start = index of the first element of the array //end = index of the last element of the array //search_value = the value that we are looking for in the array while (start < end) { if(start > end) { printf("\n Not Found"); break; } else { mid = (start+end) / 2; if (A[mid] == search_value) { printf("\n Found at %d", mid); break; } else { if (A[mid] > search_value) { end = mid - 1 ; } else { start = mid + 1 ; } } } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Explanation of the Code During each iteration of the loop, the middle element of the array is compared with the search_value. If the middle element is the value that is being searched for, the loop terminates. On the other hand, if the middle element is not equal to search_value, a decision is made on which 'half' of the array to continue the search in. Remember that
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}