Page 59 - DCAP407_DATA_STRUCTURE
P. 59

Data Structure



                          Algorithm for Linear search
                          Let  A  be a  linear array with  N  elements and  ITEM  be the given item of  information. The search
                          algorithm will find the location LOC of ITEM in A or sets LOC :=0 if the search fails. The algorithm is
                          as follows:
                          1.   Start
                          2.   [Insert ITEM at the end of A.] Set A[N+1] :=ITEM
                          3.   [Initialize counter.] Set LOC :=1

                          4.   [Search for ITEM.]
                                      (a) Repeat while A[LOC] ≠ ITEM:
                                      (b)  Set LOC := LOC + 1
                              [End of loop]
                          5.   [Successful?] If LOC = N + 1, then: Set LOC := 0
                          6.   Exit
                          The following example illustrates the concept of searching an element in a linear array.


                                            #include<stdio.h>
                                            #include<conio.h>
                                            #include<stdlib.h>

                                            void main()
                                            {
                                            /* Declaring array M that can store 20 integers and the variables ele, i and num
                                            as integer variables */

                                            int M[20], i, ele, num;01
                                            clrscr();                                       //Clears the previously entered data
                                            printf("\nEnter the number of elements to insert in an array: ");

                                            /*Accepts the number of elements from the user to store in the array M*/
                                            scanf("%d", &num);

                                            /*Accepts input from the user until i value is less than the num entered*/
                                            for(i=0;i<num; i++)
                                            {
                                            printf("\nEnter Element %d: ", i+1);
                                            scanf("%d",&M[i]); //accepts the input from the user and stores in array M
                                            }
                                            printf("\nEnter the element to be searched: ");
                                            scanf("%d", &ele);  //Accepts the element to be searched from the user
                                            for(i=0;i<num;i++){
                                            /* Check if the element to be searched is equal to the value stored in the array */
                                            if(M[i] == ele){
                                            /* Display the position of the element in the array */
                                            printf("\nElement is found at position %d",i+1);
                                            getch(); //wait until a key is pressed
                                            exit(1);
                                            }
                                            }
                                            printf("\nElement not found");
                                            getch();
                                            }



                          52                      LOVELY PROFESSIONAL UNIVERSITY
   54   55   56   57   58   59   60   61   62   63   64