unordered Table Lookup
def seq_search (LST, key): found = Falsepos = 0while pos < len (LST) and not found:if lst[pos] = = Key:found = Trueelse:pos = pos + 1return found
Sequential table Two-point lookup
def ordered_seq_search (LST, key): If Len (lst) = = 0:return-1mid = Len (LST)//2if lst[mid] = = Key:return midelse:if key < ; Lst[mid]:return Ordered_seq_search (Lst[:mid], key) Else:return Ordered_seq_search (Lst[mid + 1:], key)
Hash
Def hashlibtest (): Import hashlibstr = "Hello,world" Print (HASHLIB.MD5 (str). Hexdigest ()) Print (HASHLIB.SHA1 (str). Hexdigest ()) Print (hashlib.sha224 (str). Hexdigest ()) Print (hashlib.sha256 (str). Hexdigest ()) Print (hashlib.sha512 ( STR). Hexdigest ())
Simple Sorting
def bubblesort (LST): For I in range (len (LST)): for J in Range (Len (LST)-1, I,-1): If LST[J] < Lst[j-1]:lst[j], lst[j- 1] = lst[j-1], lst[j]def selectsort (LST): "From the back forward, from the To sort section selection, each trip to the outer selection, to determine the maximum element position, to Exchange: param Lst::return: ' for Fillslot In range (len (LST)-1, 0,-1):p Ositionofmax = 0for at range (1, Fillslot + 1): if lst[location] > Lst[fillslot]: Positionofmax = Locationlst[positionofmax], Lst[fillslot] = Lst[fillslot], lst[positionofmax]def insertSort (LST): For I In range (1, Len (LST)): CurrentValue = Lst[i]position = Iwhile position > 0 and CurrentValue < lst[position-1]:lst[p Osition] = Lst[position-1]position = position-1lst[position] = currentvaluedef mergesort (LST): If Len (LST) > 1:mid = Len (LST)//2lefthalf = Lst[:mid]righthalf = lst[mid:]# recursive mergesort (lefthalf) mergesort (righthalf) i = j = k = 0while I < ; Len (Lefthalf) and J < Len (righthalf): if Lefthalf[i] < righthalf[j]:lst[k] = Lefthalf[i]i = i + 1else:lst[k] = righth Alf[j]j = j + 1k = k + 1while i < Len (lefthalf): lst[k] = Lefthalf[i]i = i + 1k = k + 1while J < Len (righthalf): lst[k] = Righthalf[j]j = j + 1k = K + 1def QuickSort (LST, left, right): if left < right:i = LEFTJ = RIGHTK = Lst[i]while I < J:while i < J and lst[ J] > K:j = j-1lst[i] = Lst[j]while I < J and Lst[i] < K:I = i + 1lst[j] = lst[i]lst[i] = Kquicksort (LST, left, i-1) QuickSort (LST, i + 1, right) if __name__ = = ' __main__ ': seq = [x for x in range (1, one)]# print (Seq_search (SEQ, 8)) # PR int (seq_search (seq)) # Print (Ordered_seq_search (SEQ, 8)) # Print (Ordered_seq_search (seq)) # hashlibtest () LST = [2 , 0, 3, 6, 1, 4, 9, 7, 5, 8]quicksort (LST, 0, Len (LST)-1) print (LST)
Python Sort Lookup