Skip to content

Added Merge Sort , Bubble sort and Heap Operations with Python #207

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

Open
wants to merge 11 commits into
base: master
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
62 changes: 62 additions & 0 deletions Data Structures/Heap/Python/heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Import the heap functions from python library
from heapq import heappush, heappop, heapify

# heappop - pop and return the smallest element from heap
# heappush - push the value item onto the heap, maintaining
# heap invarient
# heapify - transform list into heap, in place, in linear time

# A class for Min Heap
class MinHeap:

# Constructor to initialize a heap
def __init__(self):
self.heap = []

def parent(self, i):
return int((i-1)/2)

# Inserts a new key 'k'
def insertKey(self, k):
heappush(self.heap, k)

# Decrease value of key at index 'i' to new_val
# It is assumed that new_val is smaller than heap[i]
def decreaseKey(self, i, new_val):
self.heap[i] = new_val
while(i != 0 and self.heap[self.parent(i)] > self.heap[i]):
# Swap heap[i] with heap[parent(i)]
self.heap[i] , self.heap[self.parent(i)] = (
self.heap[self.parent(i)], self.heap[i])

# Method to remove minium element from min heap
def extractMin(self):
return heappop(self.heap)

# This functon deletes key at index i. It first reduces
# value to minus infinite and then calls extractMin()
def deleteKey(self, i):
self.decreaseKey(i, float("-inf"))
self.extractMin()

# Get the minimum element from the heap
def getMin(self):
return self.heap[0]

# Driver pgoratm to test above function
heapObj = MinHeap()
heapObj.insertKey(3)
heapObj.insertKey(2)
heapObj.deleteKey(1)
heapObj.insertKey(15)
heapObj.insertKey(5)
heapObj.insertKey(4)
heapObj.insertKey(45)

print(heapObj.extractMin()),
print(heapObj.getMin()),
heapObj.decreaseKey(2, 1)
print(heapObj.getMin())


#heap implementation by Prakhar Varshney
16 changes: 16 additions & 0 deletions Sorting/Bubble Sort/Python/bubble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def bubble_sort(arr):
size = len(arr)
for i in range(size):
for j in range(0,size-i-1):
if arr[j]>arr[j+1]:
arr[j],arr[j+1] = arr[j+1],arr[j]


test_arr = [ 12, 11, 13, 5, 6, 7]
print("List before sorting")
print(test_arr)
bubble_sort(test_arr)
print("List After Sorting")
print(test_arr)

#Bubble Sorting by Prakhar Varshney
59 changes: 59 additions & 0 deletions Sorting/Merge Sort/Python/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
def merge(arr,left,mid,right):
n1 = (mid-left) + 1
n2 = right-mid

Left_arr = [0]*(n1)
Right_arr = [0]*(n2)

for i in range(0,n1):
Left_arr[i] = arr[left + i]

for j in range(0, n2):
Right_arr[j] = arr[mid + 1 +j]

i = 0
j = 0
k = left

while(i < n1 and j < n2):
if(Left_arr[i]<=Right_arr[j]):
arr[k] = Left_arr[i]
i = i+1
else:
arr[k] = Right_arr[j]
j = j+1
k = k+1

while(i<n1):
arr[k] = Left_arr[i]
i = i+1
k = k+1

while(j<n2):
arr[k] = Right_arr[j]
j = j+1
k = k+1

def mergeSort(arr,left,right):
if(left<right):
mid = int((left+(right-1))/2)

mergeSort(arr,left,mid)
mergeSort(arr,mid+1,right)
merge(arr,left,mid,right)


arr = []
size = int(input("Enter the size"))

for i in range(0,size):
a = int(input())
arr.append(a)

n = len(arr)
print("List before sorting")
print(arr)

mergeSort(arr,0,n-1)
print("sorted list ")
print(arr)