printfThe sorted array is n fork0 ksize k printfdnyk return 0 End of main What

# Printfthe sorted array is n fork0 ksize k printfdnyk

This preview shows page 2 - 5 out of 7 pages.

printf("The sorted array is :\n"); for(k=0; k<size; k++) printf("%d\n",y[k]); return (0); } // End of main What is searching? Searching means scanning though a list of items or records to find a particular one exists. It usually requires the user to specify the target item or target key. If the target item is found, the record or its location is returned, otherwise, an appropriate message or flag is returned. Basic searching algorithms are: Linear search (or Sequential Search) and Binary Search. Linear Search can be used for sorted and unsorted data both . Binary Search can be used only for sorted data . If we have sorted list of data use of binary search is more efficient in comparison to linear search. Page 2 of 7
Linear Search: This involves searching through the list sequentially until the target item is found or the list is exhausted. If the target is found, its location is returned, otherwise a flag such as –1 is returned . Algorithm for Linear Search: Let given a sorted or unsorted array, determine whether a given value or item or data is present in the array or not: int lin_search (int list[], int size, int target) { int loc; for (loc=0;loc<size ;loc++) { if(list[loc]==target) return loc; } return -1; } Solved Problem#2: (For Linear Search) #include<stdio.h> #include<conio.h> int lin_search (int list[], int size, int target) { int loc; for (loc=0;loc<size ;loc++) { if(list[loc]==target) return loc; } return -1; } // end of lin_search function Page 3 of 7
void main ( ) { int size, key, LOC, i; int list[10]; clrscr(); // it is used to clear screen printf("Enter size of list(array) : "); scanf("%d", &size); printf ("Enter % d elements of the list from key board : \n", size); printf("-------------------------------------------------------------------\n"); for(i=0; i<size; i++) { scanf("%d", &list[i]); // to read array from key board } printf("-------------------------------------------------------------------\n");

#### You've reached the end of your free preview.

Want to read all 7 pages?

• Spring '10
• zaman
• #include, #define, Search algorithm