Skip to content

Commit ebebde9

Browse files
authored
algorithm
0 parents  commit ebebde9

14 files changed

+345
-0
lines changed

001-linear-search.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Linear Search Algorithm
2+
3+
pos = 0
4+
def linearsearch(list, n):
5+
i = 0
6+
for i in range(len(list)):
7+
if list[i] == n:
8+
globals()['pos'] = i
9+
return True
10+
i = i + 1
11+
return False
12+
13+
list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
14+
n = 6
15+
if linearsearch(list, n):
16+
print("Found at position: ", pos)
17+
else:
18+
print("Not Found in list")

002-binary-search.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Binary Search Algorithm
2+
3+
pos = 0
4+
def binarysearch(list, n):
5+
# l -> lower bound
6+
l = 0
7+
8+
# u -> upper bound
9+
u = len(list) - 1
10+
11+
while l <= u:
12+
# mid -> mid value
13+
mid = (l + u) // 2
14+
if list[mid] == n:
15+
globals()['pos'] = mid
16+
return True
17+
else:
18+
if list[mid] < n:
19+
l = mid + 1
20+
else:
21+
u = mid - 1
22+
return False
23+
24+
list = [4,7,8,12,45,99,102,702,10987,56666]
25+
n = 99
26+
if binarysearch(list, n):
27+
print("Found at position: ", pos+1)
28+
else:
29+
print("Not Found in list")

003-depth-first-search.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Depth First Search algorithm
2+
3+
graph = {
4+
'A' : ['B', 'S'],
5+
'B' : ['A'],
6+
'C' : ['D', 'E', 'F', 'S'],
7+
'D' : ['C'],
8+
'E' : ['C', 'H'],
9+
'F' : ['C', 'G'],
10+
'G' : ['F', 'S'],
11+
'H' : ['E', 'G'],
12+
'S' : ['A', 'C', 'G'],
13+
}
14+
15+
def dfs(graph, node, visited):
16+
if node not in visited:
17+
visited.append(node)
18+
for i in graph[node]:
19+
dfs(graph, i, visited)
20+
return visited
21+
visited = dfs(graph, 'A', [])
22+
print(visited)

004-breadth-first-search.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Breadth First Search Algorithm
2+
3+
graph = {
4+
'A': ['B', 'C', 'E'],
5+
'B': ['A','D', 'E'],
6+
'C': ['A', 'F', 'G'],
7+
'D': ['B'],
8+
'E': ['A', 'B','D'],
9+
'F': ['C'],
10+
'G': ['C']
11+
}
12+
visited = []
13+
queue = []
14+
15+
def bfs(visited, graph, node):
16+
visited.append(node)
17+
queue.append(node)
18+
19+
while queue:
20+
s = queue.pop(0)
21+
print(s, end = " ")
22+
for neighbour in graph[s]:
23+
if neighbour not in visited:
24+
visited.append(neighbour)
25+
queue.append(neighbour)
26+
27+
bfs(visited, graph, 'A')

005-bubble-sort.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Bubble Sort Algorithm
2+
3+
def bubblesort(nums):
4+
for i in range(len(nums) - 1, 0, -1):
5+
for j in range(i):
6+
if nums[j] > nums[j+1]:
7+
nums[j], nums[j+1] = nums[j+1], nums[j]
8+
9+
nums = [5, 45, 8, 6, 7, 3, 2, 1, 0]
10+
bubblesort(nums)
11+
print("Sorted Array in asc order: ", nums)

006-selection-sort.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Selection Sort Algorithm
2+
3+
def selectionsort(nums):
4+
for i in range(len(nums)):
5+
minpos = i
6+
for j in range(i + 1, len(nums)):
7+
if nums[j] < nums[minpos]:
8+
minpos = j
9+
nums[i], nums[minpos] = nums[minpos], nums[i]
10+
# print(nums)
11+
12+
nums = [5, 3, 8, 9, 7, 2, 0, 4, 1, 6]
13+
print("Unsorted list: ", nums)
14+
selectionsort(nums)
15+
print("Sorted list: ", nums)

007-insertion-sort.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Insertion Sort Algorithm
2+
3+
def insertionsort(list):
4+
for index in range(0, len(list)):
5+
current_element = list[index]
6+
pos = index
7+
while current_element < list[pos - 1] and pos > 0:
8+
list[pos] = list[pos - 1]
9+
pos -= 1
10+
list[pos] = current_element
11+
12+
list = [26, 5, 3, 78, 35, 42, 29, 66, 49]
13+
insertionsort(list)
14+
print(list)

008-merge-sort.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Merge Sort Algorithm
2+
3+
def mergesort(list):
4+
if len(list) > 1:
5+
mid = len(list) // 2
6+
left = list[:mid]
7+
right = list[mid:]
8+
mergesort(left)
9+
mergesort(right)
10+
i = j = k = 0
11+
while i < len(left) and j < len(right):
12+
if left[i] < right[j]:
13+
list[k] = left[i]
14+
i += 1
15+
else:
16+
list[k] = right[j]
17+
j += 1
18+
k += 1
19+
20+
while i < len(left):
21+
list[k] = left[i]
22+
i += 1
23+
k += 1
24+
25+
while j < len(right):
26+
list[k] = right[j]
27+
j += 1
28+
k += 1
29+
30+
list = [54, 26, 93, 17, 77, 31, 44, 55, 20]
31+
mergesort(list)
32+
print(list)

009-counting-sort.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Counting Sort Algorithm
2+
3+
def countingsort(array, high):
4+
m = high + 1
5+
count = [0] * m
6+
for a in array:
7+
count[a] += 1
8+
i = 0
9+
for a in range(m):
10+
for c in range(count[a]):
11+
array[i] = a
12+
i += 1
13+
return array
14+
15+
array = [1, 2, 7, 3, 2, 1, 4, 2, 3, 2, 1, 8, 7, 9, 0]
16+
high = max(array)
17+
countingsort(array, high)
18+
print(array)

010-quick-sort.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Quick Sort Algorithm
2+
3+
"""
4+
# In this program pivot = first
5+
def pivot_place(list, first, last):
6+
pivot = list[first]
7+
print(pivot)
8+
left = first + 1
9+
right = last
10+
while True:
11+
while left <= right and list[left] <= pivot:
12+
left += 1
13+
while left <= right and list[right] >= pivot:
14+
right -= 1
15+
if right < left:
16+
break
17+
else:
18+
list[left], list[right] = list[right], list[left]
19+
list[first], list[right] = list[right], list[first]
20+
return right
21+
22+
def quicksort(list, first, last):
23+
if first < last:
24+
p = pivot_place(list, first, last)
25+
quicksort(list, first, p - 1)
26+
quicksort(list, p + 1, last)
27+
28+
list = [56, 26, 93, 17, 31, 44]
29+
n = len(list)
30+
quicksort(list, 0, n - 1)
31+
print(list)
32+
"""
33+
34+
35+
# In this program pivot = last
36+
import random
37+
def pivot_place(list, first, last):
38+
rindex = random.randint(first, last)
39+
list[rindex], list[last] = list[last], list[rindex]
40+
pivot = list[last]
41+
print(pivot)
42+
left = first
43+
right = last - 1
44+
while True:
45+
while left <= right and list[left] <= pivot:
46+
left += 1
47+
while left <= right and list[right] >= pivot:
48+
right -= 1
49+
if right < left:
50+
break
51+
else:
52+
list[left], list[right] = list[right], list[left]
53+
list[last], list[left] = list[left], list[last]
54+
return left
55+
56+
def quicksort(list, first, last):
57+
if first < last:
58+
p = pivot_place(list, first, last)
59+
quicksort(list, first, p - 1)
60+
quicksort(list, p + 1, last)
61+
62+
list = [56, 26, 93, 17, 31, 44]
63+
n = len(list)
64+
quicksort(list, 0, n - 1)
65+
print(list)

011-bucket-sort.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Bucket Sort Algorithm
2+
3+
def bucketsort(array):
4+
bucket = []
5+
6+
# Create empty buckets
7+
for i in range(len(array)):
8+
bucket.append([])
9+
10+
# Insert elements into their respective buckets
11+
for j in array:
12+
index = int(10 * j)
13+
bucket[index].append(j)
14+
15+
# Sort the elements of each bucket
16+
for i in range(len(array)):
17+
bucket[i] = sorted(bucket[i])
18+
19+
# Get the sorted elements
20+
k = 0
21+
for i in range(len(array)):
22+
for j in range(len(bucket[i])):
23+
array[k] = bucket[i][j]
24+
k += 1
25+
return array
26+
27+
array = [.42, .32, .33, .52, .37, .47, .51, .01, .00]
28+
print(bucketsort(array))

012-heap-sort.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Heap Sort Algorithm
2+
3+
from heapq import heappop, heappush
4+
def heapsort(array):
5+
heap = []
6+
for element in array:
7+
heappush(heap, element)
8+
9+
ordered = []
10+
11+
# while we have elements left in the heap
12+
while heap:
13+
ordered.append(heappop(heap))
14+
return ordered
15+
16+
array = [13, 21, 15, 5, 26, 4, 17, 18, 24, 2, 1, 0, 7, 49]
17+
print(heapsort(array))

013-radix-sort.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Radix Sort Algorithm
2+
3+
4+
# get number of digits in largest item
5+
def __get_num_digits(array):
6+
m = 0
7+
for item in array:
8+
m = max(item, m)
9+
return len(str(m))
10+
11+
from functools import reduce
12+
def __flatten(array):
13+
return reduce(lambda x, y : x + y, array)
14+
15+
def radix(array, num_digits):
16+
for digit in range(0, num_digits):
17+
bucket = [[] for i in range(10)]
18+
for item in array:
19+
# num is the bucket number that the item will be put into
20+
num = item // 10 ** (digit) % 10
21+
bucket[num].append(item)
22+
array = __flatten(bucket)
23+
return array
24+
25+
def main():
26+
array = [55, 45, 3, 289, 213, 1, 288, 53, 2, 0, 7865, 2]
27+
28+
# find longest integer in our list
29+
num_digits = __get_num_digits(array)
30+
array = radix(array, num_digits)
31+
print(array)
32+
main()

014-shell-sort.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Shell Sort Algorithm
2+
3+
def shellsort(list):
4+
interval = len(list) // 2
5+
while interval > 0:
6+
for index in range(interval, len(list)):
7+
current_element = list[index]
8+
pos = index
9+
while pos >= interval and current_element < list[pos - interval]:
10+
list[pos] = list[pos - interval]
11+
pos -= interval
12+
list[pos] = current_element
13+
interval = interval // 2
14+
15+
list = [9, 8, 3, 7, 5, 6, 4, 1]
16+
shellsort(list)
17+
print(list)

0 commit comments

Comments
 (0)