unit6 - Searching techniques Searching : It is a process to...

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

View Full Document Right Arrow Icon
Searching techniques Searching : It is a process to find whether a particular value with specified properties is present or not among a collection of items. If the value is present in the collection, then searching is said to be successful, and it returns the location of the value in the array. Otherwise, if the value is not present in the array, the searching process displays the appropriate message and in this case searching is said to be unsuccessful. 1) Linear or Sequential Searching 2) Binary Searching Linear_Search (A[ ], N, val , pos ) Step 1 : Set pos = -1 and k = 0 Step 2 : Repeat while k < N Begin Step 3 : if A[ k ] = val Set pos = k print pos Goto step 5 End while Step 4 : print “Value is not present” Step 5 : Exit int main( ) { int arr [ 50 ] , num , i , n , pos = -1; printf ("How many elements to sort : "); scanf ("%d", &n); printf ("\n Enter the elements : \n\n"); for( i = 0; i < n; i++ ) { printf (“arr [%d ] : “ , i ); scanf( "%d", &arr[ i ] ); } printf(“\nEnter the number to be searched : “); scanf(“%d”,&num); for(i=0;i<n;i++) if( arr [ i ] == num ) { pos = i ; break; } if ( pos == -1 ) printf(“ %d does not exist ”,num); else printf(“ %d is found at location : %d”, num , pos); Searches -- for each item one by one in the list from the first, until the match is found. Efficiency of Linear search : -- Executes in O ( n ) times where n is the number of elements in the list.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Binary_Search (A [ ], U_bound, VAL) Step 1 : set BEG = 0 , END = U_bound , POS = -1 Step 2 : Repeat while (BEG <= END ) Step 3 : set MID = ( BEG + END ) / 2 Step 4 : if A [ MID ] == VAL then POS = MID print VAL “ is available at “, POS GoTo Step 6 End if if A [ MID ] > VAL then set END = MID – 1 Else set BEG = MID + 1 End if End while Step 5 : if POS = -1 then print VAL “ is not present “ End if Step 6 : EXIT void binary_serch ( int a [], int n, int val ) { int end = n - 1, beg = 0, pos = -1; while( beg <= end ) { mid = ( beg + end ) / 2; if ( val == a [ mid ] ) { pos = mid; printf(“%d is available at %d”,val, pos ); break; } if ( a [ mid ] > val ) end = mid – 1; else beg = mid + 1; } if ( pos = - 1) printf( “%d does not exist “, val ); } Binary Searching Algorithm: Before searching, the list of items should be sorted in ascending order. We first compare the key value with the item in the position of the array. If there is a match, we can return immediately the position. if the value is less than the element in middle location of the array, the required value is lie in the lower half of the array. if the value is greater than the element in middle location of the array, the required value is lie in the upper half of the array. We repeat the above procedure on the lower half or upper half of the array.
Background image of page 2
Sorting is a technique to rearrange the elements of a list in ascending or descending order, which can be numerical, lexicographical, or any user-defined order. Ranking of students is the process of sorting in descending order.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/15/2011 for the course ECKOW 123 taught by Professor Riuyt during the Spring '11 term at University of Damascus.

Page1 / 17

unit6 - Searching techniques Searching : It is a process to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online