Sei sulla pagina 1di 5

Assignment-2

On

Advanced Problem Solving Technique

Date: February 21th, 2012

Q1.Binary search algorithm


In computer science, a binary search or half-interval search algorithm finds the position of a specified value (the input "key") within a sorted array. At each stage, the algorithm compares the input key value with the key value of the middle element of the array. If the keys match, then a matching element has been found so its index, or position, is returned. Otherwise, if the sought key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the input key is greater, on the sub-array to the right. If the remaining array to be searched is reduced to zero, then the key cannot be found in the array and a special "Not found" indication is returned. A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time. A binary search is a dichotomic divide and conquer search algorithm

Algorithm
1. Get the middle element; 2. if the middle element equals to the searched value, the algorithm stops; 3. otherwise, two cases are possible: o Searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element. o Searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element. We should define when iterations should stop. First case is when searched element is found. Second one is when sub array has no elements. In this case, we can conclude, that searched value doesn't present in the array.

Example
Find 6 in {-1, 5, 6, 18, 19, 25, 46, 78, 102, 114}. Step 1 (middle element is 19 > 6): Step 2 (middle element is 5 < 6): Step 3 (middle element is 6 == 6): -1 5 6 18

19

25 46 78 102 114

-1 5 6 18 19 25 46 78 102 114 -1 5 6 18 19 25 46 78 102 114

Pseudo-code
procedure bin search(x: integer, a 1,a2,.an: increasing order integer) a=1 ( a is the left endpoint of search interval) b= n( b is the right endpoint of search interval) while ( a<b) begin m = (a+b)/2 if x>am then i=m+1 else j=m end if x=ai then location = i else location = 0

Code in C
main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2;

while( first <= last ) { if ( array[middle] < 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 > last ) printf("Not found! %d is not present in the list.\n", search); return 0; }

Q2. To find the maximum element in the finite sequence


Algorithm 1. 2. 3. 4. Start Define the number of term to find the maximum number, n Assign the initial term of the sequence a1 = max(Variable) Apply for loop to compare the next integer in the sequence to the maximum (temp variable), and if it is larger than the temporary maximum, set the temporary maximum equal to this integer. 5. Repeat step 4 till n 6. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence. Pseudo code procedure max(a1,a2,..an: integer) max:=a1 For i:= 2 to n If max<ai then max:=ai [max is the largest element]

Explanation This algorithm first assigns the initial term of the given sequence a1 to the variable max. The for loop is used to successively examine terms of the sequence. If a term is greater than the current value of max, it is assigned to the new value of max. In this way maximum value will be calculated from the given sequence.

Potrebbero piacerti anche