├── .gitattributes ├── .gitignore ├── Chapter04 ├── circularly_linked_list.py ├── doubly_linked_list.py └── singly_linked_list.py ├── Chapter05 ├── queue.py ├── queue_application.py └── stack_queue_1.py ├── Chapter06 ├── bintree_tree.py └── rpn.py ├── Chapter07 ├── hash.py ├── hashf.py └── hashtable.py ├── Chapter08 └── heap.py ├── Chapter09 ├── bin_search.py ├── interpolation_search.py ├── ordered_list_search.py └── unordered_list_search.py ├── Chapter10 ├── bubble.py ├── insert_sort.py ├── proper_quick_sort.py └── selection_sortion.py ├── Chapter11 ├── deterministic_search.py └── randomized_search.py ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter04/circularly_linked_list.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data=None): 3 | self.data = data 4 | self.next = None 5 | 6 | class CircularList: 7 | def __init__(self, data=None): 8 | self.head = None 9 | self.tail = None 10 | self.size = 0 11 | 12 | def clear(self): 13 | self.tail = None 14 | self.head = None 15 | 16 | def append(self, data): 17 | node = Node(data) 18 | if self.head: 19 | self.head.next = node 20 | self.head = node 21 | else: 22 | self.head = node 23 | self.tail = node 24 | self.head.next = self.tail 25 | self.size += 1 26 | 27 | def delete(self, data): 28 | current = self.tail 29 | prev = self.tail 30 | while prev == current or prev != self.head: 31 | if current.data == data: 32 | if current == self.tail: 33 | self.tail = current.next 34 | self.head.next = self.tail 35 | else: 36 | prev.next = current.next 37 | self.size -= 1 38 | return 39 | prev = current 40 | current = current.next 41 | 42 | def iter(self): 43 | current = self.tail 44 | while current: 45 | val = current.data 46 | current = current.next 47 | yield val 48 | 49 | words = CircularList() 50 | words.append('eggs') 51 | words.append('ham') 52 | words.append('spam') 53 | 54 | counter = 0 55 | for word in words.iter(): 56 | print(word) 57 | counter += 1 58 | if counter > 1000: 59 | break 60 | 61 | import sys 62 | sys.exit() 63 | 64 | l.append('foo') 65 | l.append('bar') 66 | l.append('bim') 67 | l.append('baz') 68 | l.append('quux') 69 | l.append('duux') 70 | 71 | counter = 0 72 | for item in l.iter(): 73 | print(item) 74 | counter += 1 75 | if counter > 1000: 76 | break 77 | 78 | print("Done iterating. Now we try to delete something that isn't there.") 79 | l.delete('socks') 80 | print('back to iterating') 81 | counter = 0 82 | for item in l.iter(): 83 | print(item) 84 | counter += 1 85 | if counter > 1000: 86 | break 87 | 88 | print('Let us delete something that is there.') 89 | l.delete('foo') 90 | print('back to iterating') 91 | counter = 0 92 | for item in l.iter(): 93 | print(item) 94 | counter += 1 95 | if counter > 1000: 96 | break 97 | -------------------------------------------------------------------------------- /Chapter04/doubly_linked_list.py: -------------------------------------------------------------------------------- 1 | class Node(object): 2 | """ A Doubly-linked lists' node. """ 3 | def __init__(self, data=None, next=None, prev=None): 4 | self.data = data 5 | self.next = next 6 | self.prev = prev 7 | 8 | 9 | class DoublyLinkedList(object): 10 | def __init__(self): 11 | self.head = None 12 | self.tail = None 13 | self.count = 0 14 | 15 | def append(self, data): 16 | """ Append an item to the list. """ 17 | 18 | new_node = Node(data, None, None) 19 | if self.head is None: 20 | self.head = new_node 21 | self.tail = self.head 22 | else: 23 | new_node.prev = self.tail 24 | self.tail.next = new_node 25 | self.tail = new_node 26 | 27 | self.count += 1 28 | 29 | def iter(self): 30 | """ Iterate through the list. """ 31 | current = self.head #note subtle change 32 | while current: 33 | val = current.data 34 | current = current.next 35 | yield val 36 | 37 | def reverse_iter(self): 38 | """ Iterate backwards through the list. """ 39 | current = self.tail 40 | while current: 41 | val = current.data 42 | current = current.prev 43 | yield val 44 | 45 | def delete(self, data): 46 | """ Delete a node from the list. """ 47 | current = self.head 48 | node_deleted = False 49 | if current is None: 50 | node_deleted = False 51 | 52 | elif current.data == data: 53 | self.head = current.next 54 | self.head.prev = None 55 | node_deleted = True 56 | 57 | elif self.tail.data == data: 58 | self.tail = self.tail.prev 59 | self.tail.next = None 60 | node_deleted = True 61 | 62 | else: 63 | while current: 64 | if current.data == data: 65 | current.prev.next = current.next 66 | current.next.prev = current.prev 67 | node_deleted = True 68 | current = current.next 69 | 70 | if node_deleted: 71 | self.count -= 1 72 | 73 | def search(self, data): 74 | """Search through the list. Return True if data is found, otherwise False.""" 75 | for node in self.iter(): 76 | if data == node: 77 | return True 78 | return False 79 | 80 | def print_foward(self): 81 | """ Print nodes in list from first node inserted to the last . """ 82 | for node in self.iter(): 83 | print(node) 84 | 85 | def print_backward(self): 86 | """ Print nodes in list from latest to first node. """ 87 | current = self.tail 88 | while current: 89 | print(current.data) 90 | current = current.prev 91 | 92 | def insert_head(self, data): 93 | """ Insert new node at the head of linked list. """ 94 | 95 | if self.head is not None: 96 | new_node = Node(data, None, None) 97 | new_node.next = self.head 98 | self.head.prev = new_node 99 | self.head = new_node 100 | self.count += 1 101 | 102 | def reverse(self): 103 | """ Reverse linked list. """ 104 | current = self.head 105 | while current: 106 | temp = current.next 107 | current.next = current.prev 108 | current.prev = temp 109 | current = current.prev 110 | 111 | # Now reverse the order of head and tail 112 | temp = self.head 113 | self.head = self.tail 114 | self.tail = temp 115 | 116 | def __getitem__(self, index): 117 | if index > self.count - 1: 118 | raise Exception("Index out of range.") 119 | current = self.head # Note subtle change 120 | for n in range(index): 121 | current = current.next 122 | return current.data 123 | 124 | def __setitem__(self, index, value): 125 | if index > self.count - 1: 126 | raise Exception("Index out of range.") 127 | current = self.head # Note subtle change 128 | for n in range(index): 129 | current = current.next 130 | current.data = value 131 | 132 | 133 | dll = DoublyLinkedList() 134 | dll.append("foo") 135 | dll.append("bar") 136 | dll.append("biz") 137 | dll.append("whew") 138 | print("Items in List : ") 139 | dll.print_foward() 140 | print("List after deleting node with data whew") 141 | dll.delete("whew") 142 | dll.print_foward() 143 | 144 | print("List count: {}".format(dll.count)) 145 | print("Print list backwards") 146 | dll.print_backward() 147 | 148 | print("Reverse list ") 149 | dll.reverse() 150 | dll.print_foward() 151 | 152 | print("Append item to front of list") 153 | dll.insert_head(55) 154 | dll.print_foward() 155 | 156 | print("Get First element: {}".format(dll[0])) 157 | -------------------------------------------------------------------------------- /Chapter04/singly_linked_list.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | """ A singly-linked node. """ 3 | def __init__(self, data=None): 4 | self.data = data 5 | self.next = None 6 | 7 | class SinglyLinkedList: 8 | """ A singly-linked list. """ 9 | def __init__(self): 10 | """ Create an empty list. """ 11 | self.tail = None 12 | self.head = None 13 | self.count = 0 14 | 15 | def iter(self): 16 | """ Iterate through the list. """ 17 | current = self.tail 18 | while current: 19 | val = current.data 20 | current = current.next 21 | yield val 22 | 23 | def append(self, data): 24 | """ Append an item to the list """ 25 | node = Node(data) 26 | if self.head: 27 | self.head.next = node 28 | self.head = node 29 | else: 30 | self.tail = node 31 | self.head = node 32 | self.count += 1 33 | 34 | def delete(self, data): 35 | """ Delete a node from the list """ 36 | current = self.tail 37 | prev = self.tail 38 | while current: 39 | if current.data == data: 40 | if current == self.tail: 41 | self.tail = current.next 42 | else: 43 | prev.next = current.next 44 | self.count -= 1 45 | return 46 | prev = current 47 | current = current.next 48 | 49 | def search(self, data): 50 | """ Search through the list. Return True if data is found, otherwise 51 | False. """ 52 | for node in self.iter(): 53 | if data == node: 54 | return True 55 | return False 56 | 57 | def __getitem__(self, index): 58 | if index > self.count - 1: 59 | raise Exception("Index out of range.") 60 | current = self.tail 61 | for n in range(index): 62 | current = current.next 63 | return current.data 64 | 65 | def __setitem__(self, index, value): 66 | if index > self.count - 1: 67 | raise Exception("Index out of range.") 68 | current = self.tail 69 | for n in range(index): 70 | current = current.next 71 | current.data = value 72 | 73 | words = SinglyLinkedList() 74 | words.append('foo') 75 | words.append('bar') 76 | words.append('bim') 77 | words.append('baz') 78 | words.append('quux') 79 | 80 | print("access by index") 81 | print("here is a node: {}".format(words[1])) 82 | 83 | print("modify by index") 84 | words[4] = "Quux" 85 | print("Modified node by index: {}".format(words[4])) 86 | 87 | print("This list has {} elements.".format(words.count)) 88 | for word in words.iter(): 89 | print("Got this data: {}".format(word)) 90 | 91 | if words.search('foo'): 92 | print("Found foo in the list.") 93 | if words.search('amiga'): 94 | print("Found amiga in the list.") 95 | 96 | print("Now we try to delete an item") 97 | words.delete('bim') 98 | print("List now has {} elements".format(words.count)) 99 | for word in words.iter(): 100 | print("data: {}".format(word)) 101 | 102 | print("delete the first item in the list") 103 | words.delete('foo') 104 | print("size: {}".format(words.count)) 105 | for word in words.iter(): 106 | print("data: {}".format(word)) 107 | 108 | print("delete the last item in the list") 109 | words.delete('quux') 110 | print("size: {}".format(words.count)) 111 | for word in words.iter(): 112 | print("data: {}".format(word)) 113 | -------------------------------------------------------------------------------- /Chapter05/queue.py: -------------------------------------------------------------------------------- 1 | 2 | class ListQueue: 3 | 4 | def __init__(self): 5 | self.items = [] 6 | self.size = 0 7 | 8 | def empty(self): 9 | return self.items == [] 10 | 11 | def enqueue(self, data): 12 | self.items.insert(0, data) 13 | self.size += 1 14 | 15 | def dequeue(self): 16 | data = self.items.pop() 17 | self.size -= 1 18 | return data 19 | 20 | def size(self): 21 | return self.size 22 | 23 | 24 | 25 | list_queue = ListQueue() 26 | 27 | import time 28 | start_time = time.time() 29 | for i in range(100000): 30 | #print(i) 31 | list_queue.enqueue(i) 32 | for i in range(100000): 33 | list_queue.dequeue() 34 | print("--- %s seconds ---" % (time.time() - start_time)) 35 | 36 | 37 | """ 38 | import time 39 | start_time = time.time() 40 | for i in range(1000): 41 | for j in range(100): 42 | array_queue.enqueue(i) 43 | for k in range(10): 44 | array_queue.dequeue() 45 | print("--- %s seconds ---" % (time.time() - start_time)) 46 | """ 47 | -------------------------------------------------------------------------------- /Chapter05/queue_application.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | 4 | 5 | class Node(object): 6 | """ A Doubly-linked lists' node. """ 7 | def __init__(self, data=None, next=None, prev=None): 8 | self.data = data 9 | self.next = next 10 | self.prev = prev 11 | 12 | 13 | class Queue(object): 14 | """ A doubly-linked list. """ 15 | def __init__(self): 16 | self.head = None 17 | self.tail = None 18 | self.count = 0 19 | 20 | def enqueue(self, data): 21 | """ Append an item to the list. """ 22 | 23 | new_node = Node(data, None, None) 24 | if self.head is None: 25 | self.head = new_node 26 | self.tail = self.head 27 | else: 28 | new_node.prev = self.tail 29 | self.tail.next = new_node 30 | self.tail = new_node 31 | 32 | self.count += 1 33 | 34 | def dequeue(self): 35 | """ Remove elements from the front of the list""" 36 | current = self.head 37 | if self.count == 1: 38 | self.count -= 1 39 | self.head = None 40 | self.tail = None 41 | elif self.count > 1: 42 | self.head = self.head.next 43 | self.head.prev = None 44 | self.count -= 1 45 | return current 46 | 47 | 48 | 49 | queue = Queue() 50 | 51 | import time 52 | start_time = time.time() 53 | for i in range(100000): 54 | queue.enqueue(i) 55 | for i in range(100000): 56 | queue.dequeue() 57 | print("--- %s seconds ---" % (time.time() - start_time)) 58 | 59 | 60 | class Track: 61 | 62 | def __init__(self, title=None): 63 | self.title = title 64 | self.length = randint(5, 10) 65 | 66 | 67 | 68 | track1 = Track("white whistle") 69 | track2 = Track("butter butter") 70 | print(track1.length) 71 | print(track2.length) 72 | 73 | import time 74 | class MediaPlayerQueue(Queue): 75 | 76 | def __init__(self): 77 | super(MediaPlayerQueue, self).__init__() 78 | 79 | def add_track(self, track): 80 | self.enqueue(track) 81 | 82 | def play(self): 83 | while self.count > 0: 84 | current_track_node = self.dequeue() 85 | print("Now playing {}".format(current_track_node.data.title)) 86 | time.sleep(current_track_node.data.length) 87 | 88 | track1 = Track("white whistle") 89 | track2 = Track("butter butter") 90 | track3 = Track("Oh black star") 91 | track4 = Track("Watch that chicken") 92 | track5 = Track("Don't go") 93 | media_player = MediaPlayerQueue() 94 | media_player.add_track(track1) 95 | media_player.add_track(track2) 96 | media_player.add_track(track3) 97 | media_player.add_track(track4) 98 | media_player.add_track(track5) 99 | media_player.play() 100 | 101 | -------------------------------------------------------------------------------- /Chapter05/stack_queue_1.py: -------------------------------------------------------------------------------- 1 | class Queue: 2 | def __init__(self): 3 | self.inbound_stack = [] 4 | self.outbound_stack = [] 5 | 6 | def dequeue(self): 7 | if not self.outbound_stack: 8 | while self.inbound_stack: 9 | self.outbound_stack.append(self.inbound_stack.pop()) 10 | return self.outbound_stack.pop() 11 | 12 | def enqueue(self, data): 13 | self.inbound_stack.append(data) 14 | 15 | 16 | queue = Queue() 17 | queue.enqueue(5) 18 | queue.enqueue(6) 19 | queue.enqueue(7) 20 | print(queue.inbound_stack) 21 | queue.dequeue() 22 | print(queue.inbound_stack) 23 | print(queue.outbound_stack) 24 | queue.dequeue() 25 | print(queue.outbound_stack) 26 | 27 | """ 28 | import time 29 | start_time = time.time() 30 | for i in range(100000): 31 | #print i 32 | array_queue.enqueue(i) 33 | for i in range(100000): 34 | #print i 35 | array_queue.dequeue() 36 | print("--- %s seconds ---" % (time.time() - start_time)) 37 | 38 | 39 | 40 | import time 41 | start_time = time.time() 42 | for i in range(10000): 43 | for j in range(100): 44 | array_queue.push(i) 45 | for k in range(10): 46 | array_queue.pop() 47 | print("--- %s seconds ---" % (time.time() - start_time)) 48 | """ -------------------------------------------------------------------------------- /Chapter06/bintree_tree.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python3 2 | 3 | class Node: 4 | def __init__(self, data): 5 | self.data = data 6 | self.right_child = None 7 | self.left_child = None 8 | 9 | class Tree: 10 | def __init__(self): 11 | self.root_node = None 12 | 13 | def insert(self, data): 14 | node = Node(data) 15 | if self.root_node is None: 16 | self.root_node = node 17 | else: 18 | current = self.root_node 19 | parent = None 20 | while True: 21 | parent = current 22 | if node.data < parent.data: 23 | current = current.left_child 24 | if current is None: 25 | parent.left_child = node 26 | return 27 | else: 28 | current = current.right_child 29 | if current is None: 30 | parent.right_child = node 31 | return 32 | 33 | def search(self, data): 34 | current = self.root_node 35 | while True: 36 | if current is None: 37 | return None 38 | elif current.data is data: 39 | return data 40 | elif current.data > data: 41 | current = current.left_child 42 | else: 43 | current = current.right_child 44 | 45 | n1 = Node("root node") 46 | n2 = Node("left child node") 47 | n3 = Node("right child node") 48 | n4 = Node("left grandchild node") 49 | 50 | n1.left_child = n2 51 | n1.right_child = n3 52 | n2.left_child = n4 53 | 54 | current = n1 55 | while current: 56 | print(current.data) 57 | current = current.left_child 58 | 59 | tree = Tree() 60 | tree.insert(5) 61 | tree.insert(2) 62 | tree.insert(7) 63 | tree.insert(9) 64 | tree.insert(1) 65 | 66 | for i in range(1, 10): 67 | found = tree.search(i) 68 | print("{}: {}".format(i, found)) 69 | 70 | -------------------------------------------------------------------------------- /Chapter06/rpn.py: -------------------------------------------------------------------------------- 1 | from stack import Stack 2 | 3 | class TreeNode: 4 | def __init__(self, data=None): 5 | self.data = data 6 | self.right = None 7 | self.left = None 8 | 9 | def calc(node): 10 | if node.data is "+": 11 | return calc(node.left) + calc(node.right) 12 | elif node.data is "-": 13 | return calc(node.left) - calc(node.right) 14 | elif node.data is "*": 15 | return calc(node.left) * calc(node.right) 16 | elif node.data is "/": 17 | return calc(node.left) / calc(node.right) 18 | else: 19 | return node.data 20 | 21 | expr = "4 5 + 5 3 - *".split() 22 | stack = Stack() 23 | 24 | for term in expr: 25 | if term in "+-*/": 26 | node = TreeNode(term) 27 | node.right = stack.pop() 28 | node.left = stack.pop() 29 | else: 30 | node = TreeNode(int(term)) 31 | stack.push(node) 32 | 33 | root = stack.pop() 34 | result = calc(root) 35 | print(result) 36 | -------------------------------------------------------------------------------- /Chapter07/hash.py: -------------------------------------------------------------------------------- 1 | def hash(data): 2 | counter = 1 3 | sum = 0 4 | for d in data: 5 | sum += counter * ord(d) 6 | return sum % 256 7 | 8 | items = ['foo', 'bar', 'bim', 'baz', 'quux', 'duux', 'gnn'] 9 | for item in items: 10 | print("{}: {}".format(item, hash(item))) 11 | 12 | -------------------------------------------------------------------------------- /Chapter07/hashf.py: -------------------------------------------------------------------------------- 1 | def myhash(s): 2 | mult = 1 3 | hv = 0 4 | for ch in s: 5 | hv += mult * ord(ch) 6 | mult += 1 7 | return hv 8 | 9 | for item in ('hello world', 'world hello', 'gello xorld', 'ad', 'ga'): 10 | print("{}: {}".format(item, myhash(item))) 11 | 12 | #print(myhash('hello world')) 13 | #print(myhash('world hello')) 14 | #print(myhash('gello 15 | -------------------------------------------------------------------------------- /Chapter07/hashtable.py: -------------------------------------------------------------------------------- 1 | class HashItem: 2 | def __init__(self, key, value): 3 | self.key = key 4 | self.value = value 5 | 6 | class HashTable: 7 | def __init__(self): 8 | self.size = 256 9 | self.slots = [None for i in range(self.size)] 10 | self.count = 0 11 | 12 | def _hash(self, key): 13 | mult = 1 14 | hv = 0 15 | for ch in key: 16 | hv += mult * ord(ch) 17 | mult += 1 18 | return hv % self.size 19 | 20 | def put(self, key, value): 21 | item = HashItem(key, value) 22 | h = self._hash(key) 23 | 24 | while self.slots[h] is not None: 25 | if self.slots[h].key is key: 26 | break 27 | h = (h + 1) % self.size 28 | if self.slots[h] is None: 29 | self.count += 1 30 | self.slots[h] = item 31 | 32 | def get(self, key): 33 | h = self._hash(key) 34 | while self.slots[h] is not None: 35 | if self.slots[h].key is key: 36 | return self.slots[h].value 37 | h = (h+ 1) % self.size 38 | return None 39 | 40 | def __setitem__(self, key, value): 41 | self.put(key, value) 42 | 43 | def __getitem__(self, key): 44 | return self.get(key) 45 | 46 | ht = HashTable() 47 | ht["good"] = "eggs" 48 | ht["better"] = "ham" 49 | ht["best"] = "spam" 50 | ht["ad"] = "do not" 51 | ht["ga"] = "collide" 52 | 53 | for key in ("good", "better", "best", "worst", "ad", "ga"): 54 | v = ht[key] 55 | print(v) 56 | 57 | print("The number of elements is: {}".format(ht.count)) 58 | -------------------------------------------------------------------------------- /Chapter08/heap.py: -------------------------------------------------------------------------------- 1 | class Heap: 2 | def __init__(self): 3 | self.heap = [0] 4 | self.size = 0 5 | 6 | def float(self, k): 7 | while k // 2 > 0: 8 | if self.heap[k] < self.heap[k//2]: 9 | self.heap[k], self.heap[k//2] = self.heap[k//2], self.heap[k] 10 | k //= 2 11 | 12 | def insert(self, item): 13 | self.heap.append(item) 14 | self.size += 1 15 | self.float(self.size) 16 | 17 | def sink(self, k): 18 | while k * 2 <= self.size: 19 | mc = self.minchild(k) 20 | if self.heap[k] > self.heap[mc]: 21 | self.heap[k], self.heap[mc] = self.heap[mc], self.heap[k] 22 | k = mc 23 | 24 | def minchild(self, k): 25 | if k * 2 + 1 > self.size: 26 | return k * 2 27 | elif self.heap[k*2] < self.heap[k*2+1]: 28 | return k * 2 29 | else: 30 | return k * 2 + 1 31 | 32 | def pop(self): 33 | item = self.heap[1] 34 | self.heap[1] = self.heap[self.size] 35 | self.size -= 1 36 | self.heap.pop() 37 | self.sink(1) 38 | return item 39 | 40 | h = Heap() 41 | for i in (4, 8, 7, 2, 9, 10, 5, 1, 3, 6): 42 | h.insert(i) 43 | 44 | print(h.heap) 45 | 46 | for i in range(10): 47 | n = h.pop() 48 | print(n) 49 | print(h.heap) 50 | -------------------------------------------------------------------------------- /Chapter09/bin_search.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def binary_search(ordered_list, term): 4 | 5 | size_of_list = len(ordered_list) - 1 6 | 7 | index_of_first_element = 0 8 | index_of_last_element = size_of_list 9 | 10 | while index_of_first_element <= index_of_last_element: 11 | mid_point = (index_of_first_element + index_of_last_element)/2 12 | 13 | if ordered_list[mid_point] == term: 14 | return mid_point 15 | 16 | if term > ordered_list[mid_point]: 17 | index_of_first_element = mid_point + 1 18 | else: 19 | index_of_last_element = mid_point - 1 20 | 21 | 22 | store = [] 23 | for i in range(1000000): 24 | store.append(i) 25 | 26 | import time 27 | start_time = time.time() 28 | binary_search(store, 999333333) 29 | print("--- %s seconds ---" % (time.time() - start_time)) 30 | 31 | import time 32 | start_time = time.time() 33 | binary_search(store, 933) 34 | print("--- %s Found: seconds ---" % (time.time() - start_time)) 35 | 36 | 37 | """ 38 | import time 39 | start_time = time.time() 40 | print(search(store, 999)) 41 | print("--- %s seconds ---" % (time.time() - start_time)) 42 | 43 | 44 | """ 45 | 46 | store = [] 47 | for i in range(1000000): 48 | store.append(i) 49 | 50 | import time 51 | start_time = time.time() 52 | results = "Found at {}".format(binary_search(store, 999333333)) 53 | print("--- %s seconds ---: %s" % ((time.time() - start_time), results)) 54 | 55 | store = [] 56 | for i in range(1000000): 57 | store.append(i) 58 | 59 | 60 | 61 | start_time = time.time() 62 | results = "Found at {}".format(binary_search(store, 993)) 63 | print("--- %s seconds --- %s" % ((time.time() - start_time),results)) 64 | 65 | store = [] 66 | for i in range(1000000): 67 | store.append(i) 68 | 69 | 70 | start_time = time.time() 71 | results = "Found at {}".format(binary_search(store, -4999999999995)) 72 | print("--- %s seconds --- %s" % ((time.time() - start_time),results)) 73 | -------------------------------------------------------------------------------- /Chapter09/interpolation_search.py: -------------------------------------------------------------------------------- 1 | 2 | def nearest_mid(input_list, lower_bound_index, upper_bound_index, search_value): 3 | return lower_bound_index + (( upper_bound_index - lower_bound_index)/ (input_list[upper_bound_index] - input_list[lower_bound_index])) * (search_value - input_list[lower_bound_index]) 4 | 5 | def interpolation_search(ordered_list, term): 6 | 7 | size_of_list = len(ordered_list) - 1 8 | 9 | index_of_first_element = 0 10 | index_of_last_element = size_of_list 11 | 12 | while index_of_first_element <= index_of_last_element: 13 | mid_point = nearest_mid(ordered_list, index_of_first_element, index_of_last_element, term) 14 | 15 | if mid_point > index_of_last_element or mid_point < index_of_first_element: 16 | return None 17 | 18 | if ordered_list[mid_point] == term: 19 | return mid_point 20 | 21 | if term > ordered_list[mid_point]: 22 | index_of_first_element = mid_point + 1 23 | else: 24 | index_of_last_element = mid_point - 1 25 | 26 | 27 | store = [] 28 | for i in range(1000000): 29 | store.append(i) 30 | 31 | import time 32 | start_time = time.time() 33 | results = "Found at {}".format(interpolation_search(store, 999333333)) 34 | print("--- %s seconds ---: %s" % ((time.time() - start_time), results)) 35 | 36 | store = [] 37 | for i in range(1000000): 38 | store.append(i) 39 | 40 | 41 | 42 | start_time = time.time() 43 | results = "Found at {}".format(interpolation_search(store, 993)) 44 | print("--- %s seconds --- %s" % ((time.time() - start_time),results)) 45 | 46 | store = [] 47 | for i in range(1000000): 48 | store.append(i) 49 | 50 | 51 | start_time = time.time() 52 | results = "Found at {}".format(interpolation_search(store, -4999999999995)) 53 | print("--- %s seconds --- %s" % ((time.time() - start_time),results)) 54 | -------------------------------------------------------------------------------- /Chapter09/ordered_list_search.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def search(ordered_list, term): 4 | ordered_list_size = len(ordered_list) 5 | for i in range(ordered_list_size): 6 | if term == ordered_list[i]: 7 | return i 8 | elif ordered_list[i] > term: 9 | return None 10 | 11 | return None 12 | 13 | 14 | 15 | scores = [2, 3, 4, 6, 7] 16 | 17 | search_term = 5 18 | position = search(scores, search_term) 19 | 20 | if position is None: 21 | print("{} not found".format(search_term)) 22 | else: 23 | print("{} found at position {}".format(search_term, position)) 24 | 25 | 26 | search_term = 2 27 | position = search(scores, search_term) 28 | if position is None: 29 | print("{} not found".format(search_term)) 30 | else: 31 | print("{} found at position {}".format(search_term, position)) -------------------------------------------------------------------------------- /Chapter09/unordered_list_search.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def search(unordered_list, term): 4 | unordered_list_size = len(unordered_list) 5 | for i in range(unordered_list_size): 6 | if term == unordered_list[i]: 7 | return i 8 | 9 | return None 10 | 11 | 12 | 13 | scores = [60, 1, 88, 10, 11, 600] 14 | 15 | search_term = 65 16 | position = search(scores, search_term) 17 | 18 | if position is None: 19 | print("{} not found".format(search_term)) 20 | else: 21 | print("{} found at position {}".format(search_term, position)) 22 | 23 | 24 | search_term = 600 25 | position = search(scores, search_term) 26 | if position is None: 27 | print("{} not found".format(search_term)) 28 | else: 29 | print("{} found at position {}".format(search_term, position)) -------------------------------------------------------------------------------- /Chapter10/bubble.py: -------------------------------------------------------------------------------- 1 | 2 | unordered_list = [5, 2] 3 | i = 0 4 | first_element = unordered_list[0] 5 | second_element = unordered_list[1] 6 | 7 | 8 | 9 | temp = unordered_list[0] 10 | unordered_list[0] = unordered_list[1] 11 | unordered_list[1] = temp 12 | 13 | print(unordered_list) 14 | """################################################################""" 15 | 16 | def bubble_sort(unordered_list): 17 | iteration_number = len(unordered_list)-1 18 | for i in range(iteration_number): 19 | for j in range(iteration_number): 20 | if unordered_list[j] > unordered_list[j+1]: 21 | temp = unordered_list[j] 22 | unordered_list[j] = unordered_list[j+1] 23 | unordered_list[j+1] = temp 24 | 25 | 26 | 27 | 28 | my_list = [4,3,2,1] 29 | bubble_sort(my_list) 30 | print(my_list) 31 | 32 | my_list = [1,2,3,4] 33 | bubble_sort(my_list) 34 | print(my_list) -------------------------------------------------------------------------------- /Chapter10/insert_sort.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def insertion_sort(unsorted_list): 4 | 5 | for index in range(1, len(unsorted_list)): 6 | search_index = index 7 | insert_value = unsorted_list[index] 8 | 9 | while search_index > 0 and unsorted_list[search_index-1] > insert_value : 10 | unsorted_list[search_index] = unsorted_list[search_index-1] 11 | search_index -= 1 12 | 13 | unsorted_list[search_index] = insert_value 14 | 15 | 16 | 17 | 18 | 19 | 20 | my_list = [10, 11, 12, 1, 2, 3] 21 | print(my_list) 22 | insertion_sort(my_list) 23 | print(my_list) 24 | 25 | my_1 = sorted([ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ]) 26 | my_array = [ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 27 | insertion_sort(my_array) 28 | print(my_array == my_1) -------------------------------------------------------------------------------- /Chapter10/proper_quick_sort.py: -------------------------------------------------------------------------------- 1 | 2 | def partition(unsorted_array, first_index, last_index): 3 | 4 | pivot = unsorted_array[first_index] 5 | pivot_index = first_index 6 | index_of_last_element = last_index 7 | 8 | less_than_pivot_index = index_of_last_element 9 | greater_than_pivot_index = first_index + 1 10 | 11 | while True: 12 | 13 | while unsorted_array[greater_than_pivot_index] < pivot and greater_than_pivot_index < last_index: 14 | greater_than_pivot_index += 1 15 | while unsorted_array[less_than_pivot_index] > pivot and less_than_pivot_index >= first_index: 16 | less_than_pivot_index -= 1 17 | 18 | if greater_than_pivot_index < less_than_pivot_index: 19 | temp = unsorted_array[greater_than_pivot_index] 20 | unsorted_array[greater_than_pivot_index] = unsorted_array[less_than_pivot_index] 21 | unsorted_array[less_than_pivot_index] = temp 22 | else: 23 | break 24 | 25 | unsorted_array[pivot_index] = unsorted_array[less_than_pivot_index] 26 | unsorted_array[less_than_pivot_index] = pivot 27 | 28 | return less_than_pivot_index 29 | 30 | 31 | def quick_sort(unsorted_array, first, last): 32 | if last - first <= 0: 33 | return 34 | else: 35 | partition_point = partition(unsorted_array, first, last) 36 | quick_sort(unsorted_array, first, partition_point-1) 37 | quick_sort(unsorted_array, partition_point+1, last) 38 | 39 | my_array = [43, 3, 77, 89, 4, 20] 40 | print(my_array) 41 | quick_sort(my_array, 0, 5) 42 | print(my_array) 43 | 44 | my_1 = sorted([ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ]) 45 | my_array = [ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 46 | print(my_array) 47 | ss = len(my_array)-1 48 | quick_sort(my_array, 0, ss) 49 | print(my_array == my_1) 50 | 51 | 52 | my_1 = sorted([ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 432322 ]) 53 | my_array = [ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 432322 ] 54 | print(my_array) 55 | ss = len(my_array)-1 56 | quick_sort(my_array, 0, ss) 57 | print(my_array == my_1) 58 | -------------------------------------------------------------------------------- /Chapter10/selection_sortion.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def selection_sort(unsorted_list): 4 | 5 | size_of_list = len(unsorted_list) 6 | 7 | for i in range(size_of_list): 8 | for j in range(i+1, size_of_list): 9 | 10 | if unsorted_list[j] < unsorted_list[i]: 11 | temp = unsorted_list[i] 12 | unsorted_list[i] = unsorted_list[j] 13 | unsorted_list[j] = temp 14 | 15 | 16 | a_list = [3, 2, 35, 4, 32, 94, 5, 7] 17 | selection_sort(a_list) 18 | print(a_list) 19 | 20 | 21 | my_1 = sorted([ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ]) 22 | my_array = [ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 23 | selection_sort(my_array) 24 | print(my_array == my_1) -------------------------------------------------------------------------------- /Chapter11/deterministic_search.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | def median_of_medians(elems): 6 | 7 | sublists = [elems[j:j+5] for j in range(0, len(elems), 5)] 8 | 9 | medians = [] 10 | for sublist in sublists: 11 | medians.append(sorted(sublist)[len(sublist)/2]) 12 | 13 | if len(medians) <= 5: 14 | return sorted(medians)[len(medians)/2] 15 | else: 16 | return median_of_medians(medians) 17 | 18 | 19 | 20 | 21 | def swap(array_list, first, second): 22 | temp = array_list[first] 23 | array_list[first] = array_list[second] 24 | array_list[second] = temp 25 | 26 | def get_index_of_nearest_median(array_list, first, second, median): 27 | if first == second: 28 | return first 29 | else: 30 | return first + array_list[first:second].index(median) 31 | 32 | 33 | 34 | def partition(unsorted_array, first_index, last_index): 35 | 36 | if first_index == last_index: 37 | nearest_median = unsorted_array[first_index] 38 | else: 39 | nearest_median = median_of_medians(unsorted_array[first_index:last_index]) 40 | 41 | index_of_nearest_median = get_index_of_nearest_median(unsorted_array, first_index, last_index, nearest_median) 42 | 43 | swap(unsorted_array, first_index, index_of_nearest_median) 44 | 45 | pivot = unsorted_array[first_index] 46 | pivot_index = first_index 47 | index_of_last_element = last_index 48 | 49 | less_than_pivot_index = index_of_last_element 50 | greater_than_pivot_index = first_index + 1 51 | 52 | while True: 53 | 54 | while unsorted_array[greater_than_pivot_index] < pivot and greater_than_pivot_index < last_index: 55 | greater_than_pivot_index += 1 56 | while unsorted_array[less_than_pivot_index] > pivot and less_than_pivot_index >= first_index: 57 | less_than_pivot_index -= 1 58 | 59 | 60 | if greater_than_pivot_index < less_than_pivot_index: 61 | temp = unsorted_array[greater_than_pivot_index] 62 | unsorted_array[greater_than_pivot_index] = unsorted_array[less_than_pivot_index] 63 | unsorted_array[less_than_pivot_index] = temp 64 | else: 65 | break 66 | 67 | unsorted_array[pivot_index] = unsorted_array[less_than_pivot_index] 68 | unsorted_array[less_than_pivot_index] = pivot 69 | 70 | return less_than_pivot_index 71 | 72 | 73 | 74 | def deterministic_select(array_list, left, right, k): 75 | 76 | split = partition(array_list, left, right) 77 | 78 | 79 | if split == k: 80 | return array_list[split] 81 | elif split < k : 82 | return deterministic_select(array_list, split + 1, right, k) 83 | else: 84 | return deterministic_select(array_list, left, split-1, k) 85 | 86 | 87 | 88 | 89 | #arrays = [ int(i) for i in "29 44 39 09 23 10 52 50 05 06 38 11 15 03 26 37 53 25 54 40 02 12 16 30 19 55 13 41 48 01 18 43 17 47 46 24 20 33 32 31 34 04 07 51 49 35 27 21 08".split(" ")] 90 | arrays = [ int(i) for i in "2 3 5 4 1 12 11 13 16 7 8 6 10 9 17 15 19 20 18 23 21 22 25 24 14".split(" ")] 91 | 92 | #print(median_of_medians([25, 21, 98, 100, 76, 22, 43, 60, 89, 87])) 93 | 94 | 95 | 96 | 97 | stored = [3,1,10,4,6, 5] 98 | print(stored) 99 | print(deterministic_select(stored, 0, 5, 0)) 100 | 101 | stored = [3,1,10,4,6, 5] 102 | print(deterministic_select(stored, 0, 5, 1)) 103 | stored = [3,1,10,4,6, 5] 104 | print(deterministic_select(stored, 0, 5, 2)) 105 | stored = [3,1,10,4,6, 5] 106 | print(deterministic_select(stored, 0, 5, 3)) 107 | stored = [3,1,10,4,6, 5] 108 | print(deterministic_select(stored, 0, 5, 4)) 109 | stored = [3,1,10,4,6, 5] 110 | print(deterministic_select(stored, 0, 5, 5)) 111 | 112 | 113 | import random 114 | results = [] 115 | 116 | test_list = [ 265, 279, 687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 117 | 118 | for k in range(10): 119 | results = [] 120 | for i in range(1000): 121 | test_list = [ 265, 279, 687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 122 | results.append(deterministic_select(test_list, 0, 999, i)) 123 | 124 | 125 | if sorted(test_list) != results: 126 | raise Exception("Error in Algorithm") 127 | else: 128 | print("On Test: {}".format(k)) 129 | 130 | 131 | 132 | 133 | test_list = [ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 134 | 135 | for k in range(10): 136 | results = [] 137 | for i in range(1000): 138 | test_list = [ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 139 | results.append(deterministic_select(test_list, 0, 999, i)) 140 | 141 | 142 | if sorted(test_list) != results: 143 | raise Exception("Error in Algorithm") 144 | else: 145 | print("On Test: {}".format(k)) -------------------------------------------------------------------------------- /Chapter11/randomized_search.py: -------------------------------------------------------------------------------- 1 | # CHAPTER 11: QUICK SELECT 2 | 3 | 4 | def partition(unsorted_array, first_index, last_index): 5 | 6 | pivot = unsorted_array[first_index] 7 | pivot_index = first_index 8 | index_of_last_element = last_index 9 | 10 | less_than_pivot_index = index_of_last_element 11 | greater_than_pivot_index = first_index + 1 12 | 13 | while True: 14 | 15 | while unsorted_array[greater_than_pivot_index] < pivot and greater_than_pivot_index < last_index: 16 | greater_than_pivot_index += 1 17 | while unsorted_array[less_than_pivot_index] > pivot and less_than_pivot_index >= first_index: 18 | less_than_pivot_index -= 1 19 | 20 | if greater_than_pivot_index < less_than_pivot_index: 21 | temp = unsorted_array[greater_than_pivot_index] 22 | unsorted_array[greater_than_pivot_index] = unsorted_array[less_than_pivot_index] 23 | unsorted_array[less_than_pivot_index] = temp 24 | else: 25 | break 26 | 27 | unsorted_array[pivot_index] = unsorted_array[less_than_pivot_index] 28 | unsorted_array[less_than_pivot_index] = pivot 29 | 30 | return less_than_pivot_index 31 | 32 | 33 | def quick_select(array_list, left, right, k): 34 | 35 | split = partition(array_list, left, right) 36 | 37 | if split == k: 38 | return array_list[split] 39 | elif split < k: 40 | return quick_select(array_list, split + 1, right, k) 41 | else: 42 | return quick_select(array_list, left, split-1, k) 43 | 44 | 45 | 46 | stored = [5, 3] 47 | print(stored) 48 | print(quick_select(stored, 0, 1, 0)) 49 | 50 | stored = [3, 5] 51 | print(stored) 52 | print(quick_select(stored, 0, 1, 0)) 53 | 54 | 55 | 56 | 57 | 58 | 59 | stored = [3,1,10,4,6,5] 60 | print(stored) 61 | print(quick_select(stored, 0, 5, 0)) 62 | stored = [3,1,10,4,6, 5] 63 | print(quick_select(stored, 0, 5, 1)) 64 | stored = [3,1,10,4,6, 5] 65 | print(quick_select(stored, 0, 5, 2)) 66 | stored = [3,1,10,4,6, 5] 67 | print(quick_select(stored, 0, 5, 3)) 68 | stored = [3,1,10,4,6, 5] 69 | print(quick_select(stored, 0, 5, 4)) 70 | stored = [3,1,10,4,6, 5] 71 | print(quick_select(stored, 0, 5, 5)) 72 | 73 | 74 | import random 75 | results = [] 76 | 77 | test_list = [ 265, 279, 687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 78 | 79 | for k in range(10): 80 | results = [] 81 | for i in range(1000): 82 | test_list = [ 265, 279, 687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 83 | results.append(quick_select(test_list, 0, 999, i)) 84 | 85 | 86 | if sorted(test_list) != results: 87 | raise Exception("Error in Algorithm") 88 | else: 89 | print("On Test: {}".format(k)) 90 | 91 | 92 | 93 | 94 | test_list = [ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 95 | 96 | for k in range(10): 97 | results = [] 98 | for i in range(1000): 99 | test_list = [ 2622, 1179, 4687, 652, 72, 676, 988, 999, 264, 580, 380, 843, 30, 273, 949, 108, 147, 684, 12, 947, 719, 786, 770, 970, 479, 577, 260, 722, 101, 802, 117, 751, 159, 683, 478, 699, 23, 730, 1, 69, 16, 359, 820, 111, 195, 867, 293, 774, 646, 456, 537, 696, 8, 331, 2, 378, 783, 15, 310, 489, 725, 752, 872, 519, 709, 704, 502, 554, 525, 43, 49, 201, 437, 850, 369, 840, 157, 362, 738, 847, 959, 263, 254, 511, 445, 21, 992, 995, 221, 410, 953, 567, 47, 904, 654, 4, 145, 154, 584, 726, 809, 113, 510, 906, 458, 529, 923, 405, 241, 334, 771, 924, 176, 763, 104, 79, 897, 905, 931, 972, 210, 357, 805, 532, 522, 920, 152, 446, 619, 780, 205, 372, 748, 197, 84, 128, 919, 824, 505, 513, 628, 883, 749, 720, 610, 591, 740, 921, 816, 729, 194, 769, 429, 979, 9, 137, 549, 361, 721, 621, 945, 509, 358, 347, 233, 388, 552, 935, 37, 272, 124, 943, 56, 839, 822, 526, 606, 804, 421, 285, 455, 431, 553, 530, 160, 255, 346, 370, 671, 501, 568, 870, 670, 301, 26, 957, 838, 605, 337, 77, 173, 878, 395, 815, 996, 95, 184, 102, 28, 403, 785, 3, 852, 521, 106, 332, 149, 508, 663, 232, 733, 408, 627, 773, 439, 485, 607, 868, 189, 691, 167, 266, 793, 200, 284, 634, 344, 404, 374, 170, 419, 967, 582, 542, 776, 861, 432, 180, 678, 63, 611, 948, 146, 242, 73, 468, 276, 639, 910, 724, 702, 35, 14, 138, 300, 735, 119, 563, 856, 70, 292, 87, 966, 319, 764, 833, 902, 928, 345, 570, 708, 916, 227, 46, 877, 637, 385, 622, 968, 57, 96, 960, 812, 941, 603, 766, 33, 514, 488, 848, 389, 188, 391, 140, 718, 80, 781, 544, 656, 474, 473, 226, 693, 692, 10, 231, 792, 120, 107, 237, 235, 857, 100, 126, 638, 727, 686, 430, 750, 186, 596, 327, 428, 433, 575, 314, 60, 993, 31, 775, 394, 969, 716, 990, 491, 889, 682, 885, 330, 282, 800, 539, 915, 650, 977, 760, 336, 277, 616, 667, 743, 443, 228, 958, 899, 806, 562, 382, 875, 174, 499, 175, 620, 134, 506, 191, 756, 528, 299, 865, 462, 784, 703, 817, 434, 705, 262, 493, 971, 425, 18, 823, 504, 397, 578, 130, 645, 326, 366, 298, 655, 144, 68, 609, 288, 465, 187, 520, 534, 666, 886, 351, 565, 424, 169, 909, 92, 779, 495, 469, 798, 339, 761, 295, 246, 507, 305, 535, 414, 976, 459, 182, 317, 41, 38, 34, 55, 894, 141, 387, 778, 352, 887, 271, 444, 713, 965, 267, 460, 5, 11, 669, 772, 932, 323, 481, 825, 643, 675, 517, 895, 484, 311, 866, 685, 701, 422, 922, 129, 767, 291, 151, 234, 166, 40, 841, 348, 442, 436, 835, 561, 879, 617, 463, 315, 647, 85, 118, 980, 858, 384, 498, 631, 377, 373, 747, 753, 392, 845, 304, 665, 6, 613, 707, 278, 937, 229, 827, 739, 624, 181, 731, 401, 629, 492, 794, 360, 907, 383, 390, 127, 91, 754, 269, 854, 407, 579, 997, 179, 363, 222, 29, 818, 67, 381, 533, 864, 759, 165, 325, 541, 447, 985, 768, 664, 572, 569, 62, 376, 546, 649, 153, 343, 732, 253, 239, 788, 888, 808, 148, 417, 155, 681, 900, 810, 908, 486, 626, 615, 453, 349, 882, 303, 602, 22, 464, 121, 516, 597, 527, 821, 353, 251, 420, 844, 623, 338, 547, 413, 290, 243, 448, 855, 714, 208, 698, 17, 836, 472, 813, 862, 653, 131, 706, 586, 426, 545, 765, 368, 163, 636, 364, 112, 891, 329, 39, 98, 250, 178, 86, 99, 480, 612, 316, 559, 550, 677, 688, 830, 198, 225, 737, 642, 321, 257, 983, 956, 736, 335, 259, 294, 238, 74, 164, 608, 103, 599, 846, 449, 660, 961, 258, 811, 409, 876, 135, 90, 487, 697, 450, 633, 476, 694, 427, 415, 306, 543, 914, 375, 651, 162, 787, 672, 695, 302, 58, 863, 942, 566, 202, 557, 929, 849, 354, 998, 441, 828, 477, 25, 604, 418, 475, 829, 328, 640, 289, 819, 123, 114, 402, 588, 901, 954, 215, 82, 411, 65, 658, 496, 83, 283, 576, 930, 247, 680, 745, 583, 564, 185, 193, 981, 789, 470, 585, 757, 851, 890, 268, 859, 158, 320, 795, 340, 161, 762, 801, 592, 635, 142, 497, 917, 589, 940, 212, 280, 964, 618, 978, 171, 986, 755, 230, 371, 538, 196, 911, 240, 211, 287, 110, 416, 950, 379, 926, 741, 312, 396, 832, 710, 313, 435, 7, 367, 982, 842, 952, 531, 333, 452, 71, 662, 700, 494, 356, 962, 782, 884, 512, 536, 690, 318, 573, 927, 641, 975, 342, 936, 540, 711, 236, 657, 89, 308, 109, 61, 717, 81, 355, 893, 24, 912, 133, 177, 524, 1000, 797, 168, 963, 76, 423, 734, 50, 365, 632, 143, 551, 406, 466, 939, 27, 256, 803, 548, 54, 898, 601, 261, 341, 207, 350, 451, 723, 386, 853, 249, 881, 918, 42, 286, 871, 500, 791, 461, 190, 814, 471, 644, 206, 807, 48, 973, 274, 393, 974, 51, 873, 204, 796, 296, 248, 88, 214, 594, 659, 744, 324, 587, 399, 44, 398, 203, 860, 987, 777, 571, 595, 223, 218, 115, 903, 689, 555, 172, 503, 673, 991, 139, 758, 614, 955, 97, 78, 728, 219, 270, 593, 648, 245, 440, 944, 483, 192, 116, 674, 309, 156, 220, 281, 244, 122, 490, 799, 574, 590, 105, 581, 52, 400, 19, 457, 598, 45, 790, 946, 913, 679, 32, 984, 837, 869, 938, 252, 307, 600, 558, 625, 438, 925, 892, 136, 183, 59, 322, 661, 209, 826, 834, 934, 712, 217, 715, 454, 213, 951, 64, 746, 75, 482, 66, 994, 874, 150, 933, 297, 467, 216, 13, 36, 125, 523, 224, 275, 20, 93, 880, 989, 630, 742, 412, 515, 560, 831, 556, 53, 668, 518, 896, 132, 199, 94 ] 100 | results.append(quick_select(test_list, 0, 999, i)) 101 | 102 | 103 | if sorted(test_list) != results: 104 | raise Exception("Error in Algorithm") 105 | else: 106 | print("On Test: {}".format(k)) 107 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | *If you have read this book, please leave a review on [Amazon.com](https://www.amazon.com/gp/product/1786467356). Potential readers can then use your unbiased opinion to help them make purchase decisions.* 6 | 7 | # Python Data Structures and Algorithms 8 | This is the code repository for [Python Data Structures and Algorithms](https://www.packtpub.com/application-development/python-data-structures-and-algorithm?utm_source=github&utm_medium=repository&utm_campaign=9781786467355), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish. 9 | ## About the Book 10 | Data structures allow you to organize data in a particular way efficiently. They are critical to any problem, provide a complete solution, and act like reusable code. 11 | In this book, you will learn the essential Python data structures and the most common algorithms. 12 | ## Instructions and Navigation 13 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02. 14 | 15 | Chapter 12 and 13 does not have any code file 16 | 17 | The code will look like the following: 18 | ``` 19 | def dequeue(self): 20 | if not self.outbound_stack: 21 | while self.inbound_stack: 22 | self.outbound_stack.append(self.inbound_stack.pop()) 23 | return self.outbound_stack.pop() 24 | ``` 25 | 26 | The code in this book will require you to run Python 2.7.x or higher. Python's default interactive environment can also be used to run the snippets of code. In order to use other third-party libraries, pip should be installed on your system. 27 | 28 | ## Related Products 29 | * [Learning Functional Data Structures and Algorithms](https://www.packtpub.com/application-development/learning-functional-data-structures-and-algorithms?utm_source=github&utm_medium=repository&utm_campaign=9781785888731) 30 | 31 | * [Learning JavaScript Data Structures and Algorithms [Video]](https://www.packtpub.com/web-development/learning-javascript-data-structures-and-algorithms-video?utm_source=github&utm_medium=repository&utm_campaign=9781782175698) 32 | 33 | * [Java 9 Data Structures and Algorithms](https://www.packtpub.com/application-development/java-9-data-structures-and-algorithms?utm_source=github&utm_medium=repository&utm_campaign=9781785889349) 34 | ### Download a free PDF 35 | 36 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
37 |

https://packt.link/free-ebook/9781786467355

--------------------------------------------------------------------------------