DSA Lecture 05 - Part 2 CS201 Searching Algorithms I
This document summarizes different searching algorithms: linear/sequential search has complexity O(n); sentinel search reduces one check; probability search orders by probability; ordered list search stops when target is found; binary search has complexity O(log2n) by splitting the list in half at each step. It provides pseudocode examples for sequential, sentinel, probability, ordered list, and binary searches and compares their time complexities.
DSA Lecture 05 - Part 2 CS201 Searching Algorithms I
This document summarizes different searching algorithms: linear/sequential search has complexity O(n); sentinel search reduces one check; probability search orders by probability; ordered list search stops when target is found; binary search has complexity O(log2n) by splitting the list in half at each step. It provides pseudocode examples for sequential, sentinel, probability, ordered list, and binary searches and compares their time complexities.
Outline • Searching Algorithms • Search algorithms pseudocodes • Linear list search • Hashed list search • Sentinel search • Probability search • Ordered list search • Binary search • Analysis of Search Algorithms • Binary vs Sequential Search Algorithms Searching • Linear Search – Sequential search (Unordered list): • Keep searching until you find it, or reached end of list – Sentinel: • Reduces loop test to one condition by adding target (sentinel) entry at end of list – Probability: • Array is ordered with most probable search elements at beginning of list • Exchange location of element found, with preceding element – Ordered list • Stop searching when target becomes less than or equal to current element examined • Useful for searching through linked lists
• Hashed list Search
– Hashed search • Data may be accessed in one step Sequential Search • Given a target value, find its location in the container Sequential Search (Success) Sequential Search (Failure) Sequential Search Pseudocode Sentinel Search • Sequential search performs two checks – For the end of list – For target not being found
• In sentinel search, we push the target value to
the end of the list first • This ensures that the target value is always in the list removing one condition Sentinel Search Pseudocode Probability Search • Data is arranged with most probable items at the beginning and the least probable items at the end • Useful when relatively few elements are the frequently searched • To ensure that the probability ordering is correct over time, in each search, exchange the located element with the element immediately before it in the array Probability Search Psuedocode Ordered List Search • When searching an ordered list sequentially, however, it is not necessary to search to the end of the list to find a target
• We can stop when the target becomes less
than or equal to the current element we are testing Ordered List Search Pseudocode Binary Search • Complexity of linear/sequential search algorithm is O(n) n is the number of data elements in the list • If we have a sorted list, we can improve performance to O(log2n) by splitting the given list into two sub-list based on the current mid value • If the given value is < the middle value, the left sub-list is taken whereas the right sub-list is discarded • So in each iteration half sub-list is discarded thus the total efficiency is log2n Binary Search Successful Binary Search Failure • Occurs when the value of begin is > end Binary Search Pseudocode Analysis of Searching Algorithms Algorithm Complexity (Big-O Notation) Sequential Search O(n) Sentinel Search O(n) Ordered List Search O(n) Probability Search Not possible as we need to know the probability of each element first Binary Search O(log2n) Comparison of Binary and Sequential Search Next Lecture • Lecture 05-Hashed List Searches