Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quicksort python #352

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions python/Linked_List.py
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions python/QuickSort.py
Original file line number Diff line number Diff line change
@@ -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)