<jupyter_start><jupyter_text>**Sequential Search** **Unordered List**<jupyter_code>def seq_search(arr,element):
  
  for i in range(len(arr)):
    if arr[i] == element:
      return True
        
  return False
arr = [1,9,2,8,3,4,7,5,6]
seq_search(arr,8)
seq_search(arr,2.5)<jupyter_output><empty_output><jupyter_text>**Ordered List**<jupyter_code>def ordered_seq_search(arr,element):
  
  for i in range(len(arr)):
 
    if arr[i]==element:
      return True
      break
    elif arr[i]>element:
      return False
      break
  
ordered_seq_search(sorted(arr),4)
ordered_seq_search(sorted(arr),4.5)<jupyter_output><empty_output><jupyter_text>**Binary Search**<jupyter_code>def binary_search(arr,element):
  
  first=0
  last=len(arr)-1
  
  found=False
  
  while first <= last and not found:
    
    mid=(first+last)//2
    
    if arr[mid]==element:   #best case
      found=True
      
    else:
      
      if element<arr[mid]:  #check in the LHS of the mid
        last=mid-1
        
      else:                 #check in RHS of the mid
        first=mid+1
        
  return found
arr=[1,9,2,8,3,4,7,5,6]
 binary_search(sorted(arr),8)  #input array must be sorted
binary_search(sorted(arr),4.5)<jupyter_output><empty_output><jupyter_text>**Recursive Implementation**<jupyter_code>def binary_search_recursive(arr,element):
  
  if len(arr)==0:               #base case
    return False
  
  else:
    
    mid=len(arr)//2
    
    if arr[mid]==element:
      return True
    
    else:
      
      if element>arr[mid]:                     #check in the LHS of mid
        return binary_search_recursive(arr[mid+1:],element)
      
      else:                                    #check in the RHS of mid
        return  binary_search_recursive(arr[:mid],element)
  
binary_search_recursive(sorted(arr),8)
binary_search_recursive(sorted(arr),4.5)<jupyter_output><empty_output><jupyter_text>**Hash Table**<jupyter_code>class HashTable(object):
  
  def __init__(self,size):
    self.size = size               #defining the size of the hash table
    self.slots = [None]*self.size  #initializing the slots as a list with None values
    self.data = [None]*self.size   #initializing the data as a list with None values
    
  def hashfunction(self,key,size):
    return key%size                #the remainder method
  
  def rehashfunction(self,oldhash,size):       #for collision resolution
    return (oldhash+1)%size      
    
  def insert(self,key,data):                   #function to insert elements into a hash table
    
    if type(key) == str:
      s=0
      for i in key:
          s=s+ord(i)
      hashvalue = self.hashfunction(s,len(self.slots))
      
    else:
      hashvalue = self.hashfunction(key,len(self.slots))
    
    if self.slots[hashvalue] == None:
      self.slots[hashvalue] = key
      self.data[hashvalue]  = data
      
    else:                                      #incase of collision
      
      if self.slots[hashvalue] ==  key:        #if the key already exists, replace the data
        self.data[hashvalue]  = data
        
      else:
        
        nextslot = self.rehashfunction(hashvalue,len(self.slots))
        
        while self.slots[nextslot] != None and self.slots[nextslot] !=key:    #loop till we find an empty slot or a slot where the key exists
          nextslot=self.rehashfunction(nextslot,len(self.slots))
          
        if self.slots[nextslot] == None:
          self.slots[nextslot] = key
          self.data[nextslot] = data
        
        else:                                                  #if the key already exists
          
          self.data[nextslot] = data
          
          
  def get(self,key):                                           #function to get the data, given a key
    
    if type(key) == str:
      s=0
      for i in key:
          s=s+ord(i)
      startslot = self.hashfunction(s,len(self.slots))
    
    else:
      startslot = self.hashfunction(key,len(self.slots))
    
    data = None
    found = False
    stop = False
    position = startslot
    
    while self.slots[position]!=None and not found and not stop:
      
      if self.slots[position] == key:                           #if key is found, retrieve the data
        found = True
        data = self.data[position]
        
      else:  
        position=self.rehashfunction(position,len(self.slots))
        
        if position == startslot:                               #if after searching the whole hashtable we end up in the startslot again, then stop searching
          stop = True
          
    if data == None:
      print('Invalid Key!')
    
    else:
          
      return data 
  
  
  def delete(self,key):                                           #function to delete the key and data for a given key
    
    if type(key) == str:
      s=0
      for i in key:
          s=s+ord(i)
      startslot = self.hashfunction(s,len(self.slots))
    
    else:
      startslot = self.hashfunction(key,len(self.slots))
    
    found = False
    stop = False
    position = startslot
    
    while self.slots[position]!=None and not found and not stop:
      
      if self.slots[position] == key:                           #if key is found, set the data and key to None
        found = True
        self.slots[position] = None
        self.data[position] = None
        
      else:  
        position=self.rehashfunction(position,len(self.slots))
        
        if position == startslot:                               #if after searching the whole hashtable we end up in the startslot again, then stop searching
          stop = True
          
    if found == False:
      print('Invalid Key!')
    
    else:
      print('Key {} deleted!'.format(key))
h = HashTable(4)   #creating a hashtable of size 4
h.insert(1,5)    #here, the key is 1 and the data at the key is 5
h.insert(2,'two') 
h.get(1)
h.get(2)
h.get(10)
h.insert('apple',5)
h.insert('mango','tasty')
h.get('apple')
h.get('mango')
h.delete('apple')
h.get('apple')<jupyter_output>Invalid Key!
{'fraction_non_alphanumeric': 0.10335703654704813, 'fraction_numerical': 0.008930047957664958, 'mean_word_length': 1.9749139203148056, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '493433', 'license': 'Unlicense', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1902, 'n_words': 466, 'path': 'Searching_Algorithms.ipynb', 'repo_name': 'manashpratim/Algorithms-and-Data-Structures', 'size': '6047'}