AIM: Write A Program To Do Linear Search Using A Given Search Key In An Un- Sorted Linear Array A Which Has N Values In It. Description: The Linear Search Compares Each Element Of The Array With The Search Key Until The Search Key Is Found. To Determine T

匿名用户 最后更新于 2021-12-01 19:36 计算机类Computing

in c++

AIM: Write a program to do Linear Search using a given Search Key in an un- sorted Linear Array A which has N values in it. Description: The linear search compares each element of the array with the search key until the search key is found. To determine that a value is not in the array, the program must compare the search key to every element in the array A'. It is also called "Sequential Search" because it traverses the data sequentially to locate the element. Algorithm: (Linear Search) LINEAR_SEARCH (A, N, SKEY) Here A is a Linear Array with N elements and SKEY is a given item of information to search. This algorithm finds the location of SKEY in A and if successful, it returns its location otherwise it returns -1 for unsuccessful. 1. Repeat step-2 for K = 0 to N-1 2. if(A [K] = SKEY) return K [Successful Search] [ End of loop of step-1 ] 3. return -1 [Un-Successful] 4. End.
AIM: _Write a program to do Binary Search using a given Search Key in a Sorted Linear Array A which has N values in it. Description: The binary search algorithm can only be used with sorted array and eliminates one half of the elements in the array being searched after each comparison. The binary search algorithm applied to our array A works as follows: Algorithm: (Binary Search) Here is a sorted Linear Array with N elements stored in it in sorted order and SKEY is a given item of information to search. This algorithm finds the location of SKEY in A and if successful, it returns its location otherwise it returns -1 for unsuccessful. Binary Search (A, N, SKEY) [Initialize segment variables.] 1. Set START=0, END=N-1 and MID=int((START+END)/2). 2. Repeat Steps 3 and 4 while START < END and A[MID]#SKEY. 3. If SKEY

已邀请: