Record basic algorithm

 

Binary search

lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

def binary_search(lst,item):
    low = 0
    high = len(lst)-1
    while low <= high:
        mid = (low + high)/2
        guess = lst[mid]
        if guess == item:
            return mid
        elif guess > item:
            high = mid - 1   # 注意索引的变更
        else:
            low = mid + 1    # 注意索引的变更
    return None


ret = binary_search(lst,17)
if ret:
    print('Index is {}'.format(ret))
else:
    print('Not found.')

  

Quick Sort

# 基于 divide&conquer 的快速排序的Python实现
 
lst = [19, 4, 0, 7, 14, 8, 2, 12, 11, 17, 13, 3, 18, 10, 6, 1, 15, 5, 16, 9]
 
def quicksort(lst):
    if len(lst) < 2:
        return lst
    pivot = lst[0]
    left = [ ele for ele in lst[1:] if ele < pivot ]
    right = [ ele for ele in lst[1:] if ele >= pivot ]
    return quicksort(left) + [pivot,] + quicksort(right)
 
sorted_lst = quicksort(lst)
print(lst)
print(sorted_lst)

 

Heapsort

Reference:  https://www.cnblogs.com/chengxiao/p/6129630.html

Definitions and basic properties of the stack

Heap is a complete binary tree with the following properties: 
    the value of each node is equal to or greater than the left and right child node values, called the large top stack; 
    or value of each node is less than or equal to its left child node value, known as the small stack top. 

Any node of the parent node index (index K) are (K-1) / 2. 
Left index leaf node to any node (index K) is 2 * K + 1, the right index leaf node is 2 * K + 2. 
Suppose the number of nodes is N, then the last non-leaf node is the index N / 2-1.

The basic idea of ​​the sort heap

Collating sequence to be configured into a large pile top, at this time, the maximum value of the root node is the top of the stack of the entire sequence. 

To be exchanged with the last element on the end of this time the maximum value. 

The remaining n-1 th element of a stack is reconfigured, it would receive the next smallest value of n elements. 

So again executed, it will be able to get an ordered sequence of.

Heap sort code implementation

HeapSort class (Object): 
    DEF the __init __ (Self, LST): 
        self._list LST = 

    DEF ADJUST (Self, parent_idx, length): 
        parent_val = self._list [parent_idx] 
        child_idx. 1 + 2 * = parent_idx 
        the while child_idx <length: 
            # compare the size of the left and right leaf nodes of the leaf node, takes a larger 
            IF child_idx +. 1 <length and self._list [child_idx] <self._list [child_idx +. 1]: 
                child_idx +. 1 = 
            # leaf nodes and the parent node Comparative size, a larger value is assigned to the parent node 
            IF self._list [child_idx]> parent_val: 
                self._list [parent_idx] = self._list [child_idx] # as the next layer may also require replacement, the replacement operation here so only half 
                parent_idx = child_idx  
                child_idx. 1 + 2 * = child_idx
            the else: 
                BREAK
        self._list [parent_idx] = parent_val # cycle the value of the current parent node into the final position 

    DEF heap_build (Self): 
        # Step1: Construction of the large top stack 
        # began to adjust from the last non-leaf node, from the bottom right linear left 
        length = len (self._list) 
        parent_idx length = 2-1 // 
        the while parent_idx> = 0: 
            self.adjust (parent_idx, length) 
            parent_idx -. 1 = 
    
    DEF head_sort (Self): 
        # Step2: heap sort 
        length = len ( self._list) 
        for sub_length in Range (-length. 1, -1, -1): 
            # Step2.1: switching elements and the top end of the pile elements 
            tmp = self._list [0] 
            self._list [0] = self._list [sub_length]  
            self._list [sub_length] = tmp
            # Step2.2: the maximum value to the sink end of the list, re-adjust the stack structure 
            # Note: The adjustment from the top of the heap pile structure is beginning, and the need to rearrange the list is sinking as the maximum gradually reduced, i.e., a maximum value obtained before guaranteed not to participate in this adjustment stack 
            self.adjust (0, sub_length) 


LST = [. 19,. 4, 0,. 7, 14,. 8, 2, 12 is,. 11,. 17, 13 is, . 3, 18 is, 10,. 6,. 1, 15,. 5, 16,. 9] 
obj = HeapSort (LST) 
obj.heap_build () 
obj.head_sort ()

  

 

Guess you like

Origin www.cnblogs.com/standby/p/9570913.html