Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Linear Search in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 558
    Comment on it

    For doing search in C we use linear search a very simple and common searching process used for finding elements inside the array.

     

    Linear search is also called as the sequential search.

     

     

     

    Linear Search Algorithm

    Linear Search ( Array A, Value x)
    
    Step 1: Set i to 1
    Step 2: if i > n then go to step 7
    Step 3: if A[i] = x then go to step 6
    Step 4: Set i to i + 1
    Step 5: Go to Step 2
    Step 6: Print Element x Found at index i and go to step 8
    Step 7: Print element not found
    Step 8: Exit

     

    #include <stdio.h>
    
    int main() {
    	int a[100];
    	int counter, num, numsearch;
    	int boolnum = 0;
    	
    	printf("Enter the number of elements: ");
    	scanf("%d",&num);
    	
    	printf("Enter the elements of the array: ");
    	for(counter=0;counter <= num-1;counter++) {
    		scanf("%d", &a[counter]);
    	}
    	
    	printf("Enter the number to linear search for: ");
    	scanf("%d",&numsearch);
    	
    	for(counter=0;counter <= num-1;counter++) {
    		if(a[counter] == numsearch) {
    			boolnum = 1;
    			break;
    		}
    	}
    	if(boolnum == 0)
    		printf("The number is not in the list.\n");
    	else
    		printf("The number is found.\n");
    	
    	return 0;
    }
    

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: