From 9801f4a16d8b7e5a14b347e1ffb4b2320a4b2e82 Mon Sep 17 00:00:00 2001 From: Swapnil Vishwakarma Date: Thu, 1 Oct 2020 08:57:25 +0530 Subject: [PATCH 1/4] Added Selection Sort in Python --- python/selection_sort.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 python/selection_sort.py diff --git a/python/selection_sort.py b/python/selection_sort.py new file mode 100644 index 000000000..bc5057e26 --- /dev/null +++ b/python/selection_sort.py @@ -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]) From b17adae80d6cc9a173421d0285ff4f015651b0a9 Mon Sep 17 00:00:00 2001 From: Swapnil Vishwakarma Date: Thu, 1 Oct 2020 09:12:32 +0530 Subject: [PATCH 2/4] Added Quick Sort in python --- python/quick_sort.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 python/quick_sort.py diff --git a/python/quick_sort.py b/python/quick_sort.py new file mode 100644 index 000000000..420d0f022 --- /dev/null +++ b/python/quick_sort.py @@ -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]) From d61a57f9d98c23c03d79ac19163e106a5c40fd3a Mon Sep 17 00:00:00 2001 From: Swapnil Vishwakarma Date: Thu, 1 Oct 2020 10:13:14 +0530 Subject: [PATCH 3/4] Added Breadth First Search in Python --- python/Breadth_First_Search.py | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 python/Breadth_First_Search.py diff --git a/python/Breadth_First_Search.py b/python/Breadth_First_Search.py new file mode 100644 index 000000000..1ef533d76 --- /dev/null +++ b/python/Breadth_First_Search.py @@ -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) From 914673defde8577a66b215effe63b4a0b41b9953 Mon Sep 17 00:00:00 2001 From: Swapnil Vishwakarma Date: Thu, 1 Oct 2020 10:13:56 +0530 Subject: [PATCH 4/4] Added Depth First Search in Python --- python/Depth_First_Search.py | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 python/Depth_First_Search.py diff --git a/python/Depth_First_Search.py b/python/Depth_First_Search.py new file mode 100644 index 000000000..6bb826b3d --- /dev/null +++ b/python/Depth_First_Search.py @@ -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)