1 / 13

HOME

HOME. NEXT. Subject Name : Data Structure Using C Unit Title : Searching Methods . Searching Methods. Recap Searching Basics Searching Techniques Linear Search Linear Search (Algorithm) Linear Search (Analysis) Binary Search Binary Search (Algorithm) Binary Search (Analysis)

wyome
Download Presentation

HOME

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HOME NEXT Subject Name : Data Structure Using C Unit Title : Searching Methods

  2. Searching Methods Recap Searching Basics Searching Techniques Linear Search Linear Search (Algorithm) Linear Search (Analysis) Binary Search Binary Search (Algorithm) Binary Search (Analysis) Class summary NEXT PREVIOUS

  3. Recap HOME NEXT PREVIOUS • 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

  4. 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. Basics Searching Techniques Linear Search Binary Search Searching Methods HOME NEXT PREVIOUS

  6. 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. 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. 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. 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. 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. 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. Binary Search (Analysis) In general, the binary search method needs no; more than [Iog2n] + 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. Class summary Searching Basics Searching Techniques Linear Search Linear Search (Algorithm) Linear Search (Analysis) Binary Search Binary Search (Algorithm) Binary Search (Analysis) HOME PREVIOUS

More Related