diff --git a/python/Linked_List.py b/python/Linked_List.py new file mode 100644 index 00000000..0b380133 --- /dev/null +++ b/python/Linked_List.py @@ -0,0 +1,88 @@ +class Node: + def __init__(self, value): + self.value = value + self.next = None + + +class LinkedList: + def __init__(self): + self.head = None + self.size = 0 + + def __str__(self): + nodes = [] + current = self.head + while current: + nodes.append(str(current.value)) + current = current.next + return ' -> '.join(nodes) + + def __len__(self): + return self.size + + def insert_at_head(self, value): + new_node = Node(value) + new_node.next = self.head + self.head = new_node + self.size += 1 + + def insert_at_tail(self, value): + + if not self.head: + self.head = Node(value) + else: + current = self.head + while current.next: + current = current.next + current.next = Node(value) + self.size += 1 + + def delete_at_head(self): + if not self.head: + return None + value = self.head.value + self.head = self.head.next + self.size -= 1 + return value + + def delete(self, value): + if not self.head: + return None + if self.head.value == value: + return self.delete_at_head() + current = self.head + while current.next: + if current.next.value == value: + value = current.next.value + current.next = current.next.next + self.size -= 1 + return value + current = current.next + return None + + def find(self, value): + current = self.head + while current: + if current.value == value: + return current + current = current.next + return None + +linked_list = LinkedList() +linked_list.insert_at_head(1) +linked_list.insert_at_head(2) +linked_list.insert_at_head(3) +print(linked_list) +print(len(linked_list)) + +linked_list.insert_at_tail(4) +print(linked_list) +print(len(linked_list)) + +deleted_value = linked_list.delete_at_head() +print(deleted_value) +print(linked_list) +print(len(linked_list)) + +found_node = linked_list.find(2) +print(found_node.value) \ No newline at end of file diff --git a/python/QuickSort.py b/python/QuickSort.py new file mode 100644 index 00000000..986d5439 --- /dev/null +++ b/python/QuickSort.py @@ -0,0 +1,34 @@ +def partition(array, low, high): + pivot = array[high] + i = low - 1 + + for j in range(low, high): + if array[j] <= pivot: + i = i + 1 + + (array[i], array[j]) = (array[j], array[i]) + + (array[i + 1], array[high]) = (array[high], array[i + 1]) + + return i + 1 + +def quickSort(array, low, high): + if low < high: + + pi = partition(array, low, high) + + quickSort(array, low, pi - 1) + + quickSort(array, pi + 1, high) + + +data = [8, 7, 2, 1, 0, 9, 6] +print("Unsorted Array") +print(data) + +size = len(data) + +quickSort(data, 0, size - 1) + +print('Sorted Array in Ascending Order:') +print(data) \ No newline at end of file