Skip to content

Added Selection Sort and Quick Sort in Python #3

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 4 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
50 changes: 50 additions & 0 deletions python/Breadth_First_Search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from collections import defaultdict

# This class represents a directed graph using adjacency list representation
class Graph:

def __init__(self):

# Default dictionary to store graph
self.graph = defaultdict(list)

# Function to add an edge to graph
def addEdge(self, u, v):
self.graph[u].append(v)

# Function to print a BFS of graph
def BFS(self, s):

# Mark all the vertices as not visited
visited = [False] * (len(self.graph))

# Create a queue for BFS
queue = []

# Mark the source node as visited and enqueue it
queue.append(s)
visited[s] = True

while queue:

# Dequeue a vertex from queue and print it
s = queue.pop(0)
print (s, end = " ")

for i in self.graph[s]:
if visited[i] == False:
queue.append(i)
visited[i] = True


# Create a graph given in the above diagram
g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 0)

print ("Following is Breadth First Traversal (starting from vertex 3):")
g.BFS(3)
48 changes: 48 additions & 0 deletions python/Depth_First_Search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from collections import defaultdict

# This class represents a directed graph using adjacency list representation
class Graph:

# Constructor
def __init__(self):

# Default dictionary to store graph
self.graph = defaultdict(list)

# Function to add an edge to graph
def addEdge(self, u, v):
self.graph[u].append(v)

# A function used by DFS
def DFSUtil(self, v, visited):

# Mark the current node as visited and print it
visited[v] = True
print(v, end = ' ')

# Recur for all the vertices adjacent to this vertex
for i in self.graph[v]:
if visited[i] == False:
self.DFSUtil(i, visited)

# The function to do DFS traversal. It uses recursive DFSUtil()
def DFS(self, v):

# Mark all the vertices as not visited
visited = [False] * (max(self.graph) + 1)

# Call the recursive helper function to print DFS traversal
self.DFSUtil(v, visited)


# Create a graph given in the above diagram
g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)

print("Following is DFS from (starting from vertex 0)")
g.DFS(0)
44 changes: 44 additions & 0 deletions python/quick_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Quick Sort
- It picks an element as pivot and partitions the given array around the picked pivot.
It's a Divide and Conquer algorithm
"""

# This function takes last element as pivot
def partition(arr, low, high):

# index of smaller element
i = (low - 1)

pivot = arr[high]

for j in range(low, high):

# If current element is smaller than the pivot
if arr[j] < pivot:

# increment index of smaller element
i = i + 1
arr[i], arr[j] = arr[j], arr[i]

arr[i + 1], arr[high] = arr[high], arr[i + 1]
return (i + 1)

# Function to do Quick sort
def quickSort(arr, low, high):
if low < high:

# pi is partitioning index, arr[p] is now at right place
pi = partition(arr, low, high)

# Separately sorting the elements before and after partition
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)


arr = [10, 7, 8, 9, 1, 5, 55, 6, 46, 52, 21, 22, 11, 70]
n = len(arr)
quickSort(arr, 0, n-1)
print ("Sorted array is:")
for i in range(n):
print (arr[i])
23 changes: 23 additions & 0 deletions python/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Selection Sort
- It sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted
part and putting it at the beginning.
"""

A = [82, 55, 6, 46, 52, 21, 22, 11, 70]
length_of_A = len(A)

# Traversing through all array elements
for i in range(length_of_A):
# Finding the minimum element in remaining unsorted array
min_idx = i
for j in range(i+1, length_of_A):
if A[min_idx] > A[j]:
min_idx = j

# Swapping the found minimum element with the first element
A[i], A[min_idx] = A[min_idx], A[i]

print ("Sorted Array: ")
for i in range(len(A)):
print(A[i])