mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Optimized recursive_bubble_sort (#2410)
* optimized recursive_bubble_sort
* Fixed doctest error due whitespace
* reduce loop times for optimization
* fixup! Format Python code with psf/black push
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -109,7 +109,7 @@ def right_rotation(node):
|
||||
|
||||
def left_rotation(node):
|
||||
"""
|
||||
a mirror symmetry rotation of the left_rotation
|
||||
a mirror symmetry rotation of the left_rotation
|
||||
"""
|
||||
print("right rotation node:", node.get_data())
|
||||
ret = node.get_right()
|
||||
|
||||
@@ -28,11 +28,11 @@ class RedBlackTree:
|
||||
right: Optional["RedBlackTree"] = None,
|
||||
) -> None:
|
||||
"""Initialize a new Red-Black Tree node with the given values:
|
||||
label: The value associated with this node
|
||||
color: 0 if black, 1 if red
|
||||
parent: The parent to this node
|
||||
left: This node's left child
|
||||
right: This node's right child
|
||||
label: The value associated with this node
|
||||
color: 0 if black, 1 if red
|
||||
parent: The parent to this node
|
||||
left: This node's left child
|
||||
right: This node's right child
|
||||
"""
|
||||
self.label = label
|
||||
self.parent = parent
|
||||
|
||||
@@ -118,7 +118,7 @@ def inorder(root: Node):
|
||||
return
|
||||
else:
|
||||
inorder(root.left)
|
||||
print(root.value, end=" ")
|
||||
print(root.value, end=",")
|
||||
inorder(root.right)
|
||||
|
||||
|
||||
@@ -130,19 +130,19 @@ def interactTreap(root, args):
|
||||
|
||||
>>> root = interactTreap(None, "+1")
|
||||
>>> inorder(root)
|
||||
1
|
||||
1,
|
||||
>>> root = interactTreap(root, "+3 +5 +17 +19 +2 +16 +4 +0")
|
||||
>>> inorder(root)
|
||||
0 1 2 3 4 5 16 17 19
|
||||
0,1,2,3,4,5,16,17,19,
|
||||
>>> root = interactTreap(root, "+4 +4 +4")
|
||||
>>> inorder(root)
|
||||
0 1 2 3 4 4 4 4 5 16 17 19
|
||||
0,1,2,3,4,4,4,4,5,16,17,19,
|
||||
>>> root = interactTreap(root, "-0")
|
||||
>>> inorder(root)
|
||||
1 2 3 4 4 4 4 5 16 17 19
|
||||
1,2,3,4,4,4,4,5,16,17,19,
|
||||
>>> root = interactTreap(root, "-4")
|
||||
>>> inorder(root)
|
||||
1 2 3 5 16 17 19
|
||||
1,2,3,5,16,17,19,
|
||||
>>> root = interactTreap(root, "=0")
|
||||
Unknown command
|
||||
"""
|
||||
|
||||
@@ -5,7 +5,7 @@ from number_theory.prime_numbers import check_prime, next_prime
|
||||
|
||||
class DoubleHash(HashTable):
|
||||
"""
|
||||
Hash Table example with open addressing and Double Hash
|
||||
Hash Table example with open addressing and Double Hash
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
@@ -4,7 +4,7 @@ from number_theory.prime_numbers import next_prime
|
||||
|
||||
class HashTable:
|
||||
"""
|
||||
Basic Hash Table example with open addressing and linear probing
|
||||
Basic Hash Table example with open addressing and linear probing
|
||||
"""
|
||||
|
||||
def __init__(self, size_table, charge_factor=None, lim_charge=None):
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
|
||||
def check_prime(number):
|
||||
"""
|
||||
it's not the best solution
|
||||
"""
|
||||
it's not the best solution
|
||||
"""
|
||||
special_non_primes = [0, 1, 2]
|
||||
if number in special_non_primes[:2]:
|
||||
return 2
|
||||
|
||||
@@ -5,7 +5,7 @@ from hash_table import HashTable
|
||||
|
||||
class QuadraticProbing(HashTable):
|
||||
"""
|
||||
Basic Hash Table example with open addressing using Quadratic Probing
|
||||
Basic Hash Table example with open addressing using Quadratic Probing
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
@@ -61,7 +61,7 @@ class _DoublyLinkedBase:
|
||||
|
||||
class LinkedDeque(_DoublyLinkedBase):
|
||||
def first(self):
|
||||
""" return first element
|
||||
"""return first element
|
||||
>>> d = LinkedDeque()
|
||||
>>> d.add_first('A').first()
|
||||
'A'
|
||||
@@ -73,7 +73,7 @@ class LinkedDeque(_DoublyLinkedBase):
|
||||
return self._header._next._data
|
||||
|
||||
def last(self):
|
||||
""" return last element
|
||||
"""return last element
|
||||
>>> d = LinkedDeque()
|
||||
>>> d.add_last('A').last()
|
||||
'A'
|
||||
@@ -87,14 +87,14 @@ class LinkedDeque(_DoublyLinkedBase):
|
||||
# DEque Insert Operations (At the front, At the end)
|
||||
|
||||
def add_first(self, element):
|
||||
""" insertion in the front
|
||||
"""insertion in the front
|
||||
>>> LinkedDeque().add_first('AV').first()
|
||||
'AV'
|
||||
"""
|
||||
return self._insert(self._header, element, self._header._next)
|
||||
|
||||
def add_last(self, element):
|
||||
""" insertion in the end
|
||||
"""insertion in the end
|
||||
>>> LinkedDeque().add_last('B').last()
|
||||
'B'
|
||||
"""
|
||||
@@ -103,7 +103,7 @@ class LinkedDeque(_DoublyLinkedBase):
|
||||
# DEqueu Remove Operations (At the front, At the end)
|
||||
|
||||
def remove_first(self):
|
||||
""" removal from the front
|
||||
"""removal from the front
|
||||
>>> d = LinkedDeque()
|
||||
>>> d.is_empty()
|
||||
True
|
||||
@@ -123,7 +123,7 @@ class LinkedDeque(_DoublyLinkedBase):
|
||||
return self._delete(self._header._next)
|
||||
|
||||
def remove_last(self):
|
||||
""" removal in the end
|
||||
"""removal in the end
|
||||
>>> d = LinkedDeque()
|
||||
>>> d.is_empty()
|
||||
True
|
||||
|
||||
@@ -10,7 +10,7 @@ def is_operand(char):
|
||||
|
||||
|
||||
def precedence(char):
|
||||
""" Return integer value representing an operator's precedence, or
|
||||
"""Return integer value representing an operator's precedence, or
|
||||
order of operation.
|
||||
|
||||
https://en.wikipedia.org/wiki/Order_of_operations
|
||||
@@ -20,7 +20,7 @@ def precedence(char):
|
||||
|
||||
|
||||
def infix_to_postfix(expression):
|
||||
""" Convert infix notation to postfix notation using the Shunting-yard
|
||||
"""Convert infix notation to postfix notation using the Shunting-yard
|
||||
algorithm.
|
||||
|
||||
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
|
||||
|
||||
@@ -2,7 +2,7 @@ __author__ = "Omkar Pathak"
|
||||
|
||||
|
||||
class Stack:
|
||||
""" A stack is an abstract data type that serves as a collection of
|
||||
"""A stack is an abstract data type that serves as a collection of
|
||||
elements with two principal operations: push() and pop(). push() adds an
|
||||
element to the top of the stack, and pop() removes an element from the top
|
||||
of a stack. The order in which elements come off of a stack are
|
||||
|
||||
Reference in New Issue
Block a user