0% found this document useful (0 votes)
18 views2 pages

Linear

This document contains code to demonstrate linear search using both non-recursive and recursive methods. The non-recursive method uses a for loop to iterate through an array and check each element for a target key. If found, it prints the position, otherwise it prints that the key was not found. The recursive method calls itself, decrementing the array position each time, until either the key is found and its position printed, or the first position is reached without a match and prints that the key was not found.

Uploaded by

Vazed Ahmed
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Linear

This document contains code to demonstrate linear search using both non-recursive and recursive methods. The non-recursive method uses a for loop to iterate through an array and check each element for a target key. If found, it prints the position, otherwise it prints that the key was not found. The recursive method calls itself, decrementing the array position each time, until either the key is found and its position printed, or the first position is reached without a match and prints that the key was not found.

Uploaded by

Vazed Ahmed
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* Linear serach using Non Recursion */

#include<stdio.h> #include<conio.h> void nonreclinear(int a[],int ,int ); void main() { int a[20], n,key,i; int ch; clrscr(); printf("Enter the number of elements :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter the %d element",i); scanf("%d",&a[i]); } printf("\n\nElement you want to search:\n\n"); scanf("%d",&key); printf("\n**Non-Recursion method**\n"); nonreclinear(a,n,key); getch(); }/*end main*/ /* Non-Recursive method*/ void nonreclinear(int a[20],int n,int key) { int i, f=0; for(i=0;i<n;i++) { if( a[i] == key) { printf("\nThe element %d is present at position %d in list\n",key,i); f=1; break; } } if(f==0) printf("\nThe element is %d is not present in the list\n",key); }

/* Linear serach using Recursion */


#include<stdio.h> #include<conio.h> void reclinear(int a[],int ,int ); void main() { int a[20], n,key,i; int ch; clrscr(); printf("Enter the number of elements :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter the %d element",i); scanf("%d",&a[i]); } printf("\n\nElement you want to search:\n\n"); scanf("%d",&key); printf("\n**Recursion method**\n"); reclinear(a,n,key); getch(); }/*end main*/ /* Recursive method*/ void reclinear(int a[20],int n,int key) { int f=0; if(a[n]==key) { printf("\nThe element %d is present at position %d in list\n",key,n); f=1; } else { if((n==0) && (f==0)) { printf("The element %d is not found.",key); } else { reclinear(a,n-1,key); } } }

You might also like