Presentation is loading. Please wait.

Presentation is loading. Please wait.

C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods.

Similar presentations


Presentation on theme: "C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods."— Presentation transcript:

1 C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods

2 C o n f i d e n t i a l Searching Methods Recap Searching Basics Searching Techniques – Linear SearchLinear Search – Linear Search (Algorithm)Linear Search (Algorithm) – Linear Search (Analysis)Linear Search (Analysis) – Binary SearchBinary Search – Binary Search (Algorithm)Binary Search (Algorithm) – Binary Search (Analysis)Binary Search (Analysis) Class summary NEXT PREVIOUS

3 C o n f i d e n t i a l Recap Definition of Graph Type of Graphs Graph Representation An adjacency List Representation An adjacency Matrix Representation Graph Traversal Breath First Search (BFS) - Algorithm Depth First Search (DFS) - Algorithm Spanning Tree Kruskal’s Algorithm Prim’s Algorithm HOME NEXT PREVIOUS

4 C o n f i d e n t i a l Searching: Suppose DATA is a collection of data maintained in memory by a table using some types of data structure. Searching is the operation which finds the location LOC in memory of some given ITEM of information or sends some message that ITEM does or does not belong to DATA. The search algorithm that is used depends mainly on the type of the data structure that is used to maintain DATA in memory. The search is said to be successful or unsuccessful according to weather ITEM does or does not belong to DATA. Searching Methods HOME NEXT PREVIOUS

5 C o n f i d e n t i a l Basics Searching Techniques Linear Search Binary Search Searching Methods HOME NEXT PREVIOUS

6 C o n f i d e n t i a l Linear Search Start at the beginning of the list and continues until it reaches to the search value or to the end of the list which comes earlier. Searching Methods HOME NEXT PREVIOUS

7 C o n f i d e n t i a l Linear Search (Algorithm) LINEAR(DATA,N,ITEM,LOC) Step –I : Set DATA[N+1] = ITEM Step – II: LOC:=1 Step – III: While DATA[LOC] ≠ ITEM Set LOC :=LOC +1 [End of Loop] Step –IV: IF LOC:=N+1 THEN Set LOC:=0 Step –V : Exit [ DATA is a linear array, with N elements and ITEM is a given search key. LOC is the location of ITEM in DATA. If unsuccessful Search LOC =0] Searching Methods HOME NEXT PREVIOUS

8 C o n f i d e n t i a l Linear Search (Analysis)  Hence the average number of comparisons done by sequential search is = 1+2+3.....+N N = N (N+1) 2*N = (N + 1)/2  Very slow [order O(n) ] but works on an unsorted list Searching Methods HOME NEXT PREVIOUS

9 C o n f i d e n t i a l Binary Search The most efficient method of searching a sequential table without the use of auxiliary indices or tables is the binary search. Basically, the argument is compared with the key of the middle element of the table. If they are equal, the search ends successfully; otherwise, either the upper or lower half of the table must be searched in a similar manner. Searching Methods HOME NEXT PREVIOUS

10 C o n f i d e n t i a l Binary Search (Algorithm): BINARY_SEARCH(DATA,LB,UB,ITEM,LOC) DATA  It is the sorted array, LB  Lower bound of array, UB  Upper bound of array ITEM  Search Key, LOC  Location of the search key if successful search otherwise it is assigned to NULL Step-I : Set BEG:=LB, END:=UB, MID=((BEG+END)/2) Step-II: Repeat Step III and Step IV while BEG ≤ END and DATA[MID] ≠ ITEM Step-III: If ITEM<DATA[MID] then Set END:=END-1 Else Set BEG:=MID+1 [End of if structure] Searching Methods HOME NEXT PREVIOUS

11 C o n f i d e n t i a l Binary Search (Algorithm Contd.): Step –IV: Set MID:=INT((BEG+END)/2) [End of Step –II Loop] Step-V: If DATA[MID] =ITEM then Set LOC:=MID Else Set LOC:=NULL [End of If structure] Step-VI: Exit Searching Methods HOME NEXT PREVIOUS

12 C o n f i d e n t i a l Binary Search (Analysis) In general, the binary search method needs no; more than [Iog 2 n] + 1 comparisons. This implies that for an array of a million entries, only about twenty comparisons will be needed. Contrast this with the case of sequential search which on the average will need (n+1)/2 comparisons. This is the Binary Tree for 31 numbers Searching Methods HOME NEXT PREVIOUS

13 C o n f i d e n t i a l Class summary Searching Basics Searching Techniques – Linear Search – Linear Search (Algorithm) – Linear Search (Analysis) – Binary Search – Binary Search (Algorithm) – Binary Search (Analysis) HOME PREVIOUS


Download ppt "C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods."

Similar presentations


Ads by Google