How to implement various data structures with python
This article mainly shows you "how to use python to achieve a variety of data structures", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use python to achieve a variety of data structures" this article.
Quick sort def quick_sort (_ list): if len (_ list)
< 2: return _list pivot_index = 0 pivot = _list(pivot_index) left_list = [i for i in _list[:pivot_index] if i < pivot] right_list = [i for i in _list[pivot_index:] if i >Pivot] return quick_sort (left) + [pivot] + quick_sort (right) Select sort def select_sort (seq): n = len (seq) for i in range (NMur1) min_idx = i for j in range
< seq[min_inx]: min_idx = j if min_idx != i: seq[i], seq[min_idx] = seq[min_idx],seq[i]插入排序def insertion_sort(_list): n = len(_list) for i in range(1,n): value = _list[i] pos = i while pos >0 and value
< _list[pos - 1] _list[pos] = _list[pos - 1] pos -= 1 _list[pos] = value print(sql)归并排序 def merge_sorted_list(_list1,_list2): #合并有序列表 len_a, len_b = len(_list1),len(_list2) a = b = 0 sort = [] while len_a >An and len_b > b: if _ List1 [a] > _ list2 [b]: sort.append (_ List2 [b]) b + = 1 else: sort.append (_ List1 [a]) a + = 1 if len_a > a: sort.append (_ List1 [a:]) If len_b > b: sort.append (_ List2 [b:]) return sort def merge_sort (_ list): if len (list1) _ list [mid]: BinarySearch (_ ListMid:] Num) elif num < _ list [mid]: BinarySearch (_ list [: mid], num) else: return _ list.index (num) these are all the contents of the article "how to implement various data structures with python" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!