Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b73298

Browse files
authored
Merge pull request #3 from ranamabdullah/main
added two new algo quick & merge
2 parents 7a0b866 + 4af331f commit 3b73298

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

Other Algorithms/Bubble_Sort.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def bubble_sort(array):
2+
n = len(array)
3+
4+
for i in range(n):
5+
# Create a flag that will allow the function to
6+
# terminate early if there's nothing left to sort
7+
already_sorted = True
8+
9+
# Start looking at each item of the list one by one,
10+
# comparing it with its adjacent value. With each
11+
# iteration, the portion of the array that you look at
12+
# shrinks because the remaining items have already been
13+
# sorted.
14+
for j in range(n - i - 1):
15+
if array[j] > array[j + 1]:
16+
# If the item you're looking at is greater than its
17+
# adjacent value, then swap them
18+
array[j], array[j + 1] = array[j + 1], array[j]
19+
20+
# Since you had to swap two elements,
21+
# set the `already_sorted` flag to `False` so the
22+
# algorithm doesn't finish prematurely
23+
already_sorted = False
24+
25+
# If there were no swaps during the last iteration,
26+
# the array is already sorted, and you can terminate
27+
if already_sorted:
28+
break
29+
30+
return array

Other Algorithms/Merge_Sort.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def merge(left, right):
2+
# If the first array is empty, then nothing needs
3+
# to be merged, and you can return the second array as the result
4+
if len(left) == 0:
5+
return right
6+
7+
# If the second array is empty, then nothing needs
8+
# to be merged, and you can return the first array as the result
9+
if len(right) == 0:
10+
return left
11+
12+
result = []
13+
index_left = index_right = 0
14+
15+
# Now go through both arrays until all the elements
16+
# make it into the resultant array
17+
while len(result) < len(left) + len(right):
18+
# The elements need to be sorted to add them to the
19+
# resultant array, so you need to decide whether to get
20+
# the next element from the first or the second array
21+
if left[index_left] <= right[index_right]:
22+
result.append(left[index_left])
23+
index_left += 1
24+
else:
25+
result.append(right[index_right])
26+
index_right += 1
27+
28+
# If you reach the end of either array, then you can
29+
# add the remaining elements from the other array to
30+
# the result and break the loop
31+
if index_right == len(right):
32+
result += left[index_left:]
33+
break
34+
35+
if index_left == len(left):
36+
result += right[index_right:]
37+
break
38+
39+
return result

Other Algorithms/Quick_Sort.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from random import randint
2+
3+
def quicksort(array):
4+
# If the input array contains fewer than two elements,
5+
# then return it as the result of the function
6+
if len(array) < 2:
7+
return array
8+
9+
low, same, high = [], [], []
10+
11+
# Select your `pivot` element randomly
12+
pivot = array[randint(0, len(array) - 1)]
13+
14+
for item in array:
15+
# Elements that are smaller than the `pivot` go to
16+
# the `low` list. Elements that are larger than
17+
# `pivot` go to the `high` list. Elements that are
18+
# equal to `pivot` go to the `same` list.
19+
if item < pivot:
20+
low.append(item)
21+
elif item == pivot:
22+
same.append(item)
23+
elif item > pivot:
24+
high.append(item)
25+
26+
# The final result combines the sorted `low` list
27+
# with the `same` list and the sorted `high` list
28+
return quicksort(low) + same + quicksort(high)

Other Algorithms/Tim_Sort.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def insertion_sort(array, left=0, right=None):
2+
if right is None:
3+
right = len(array) - 1
4+
5+
# Loop from the element indicated by
6+
# `left` until the element indicated by `right`
7+
for i in range(left + 1, right + 1):
8+
# This is the element we want to position in its
9+
# correct place
10+
key_item = array[i]
11+
12+
# Initialize the variable that will be used to
13+
# find the correct position of the element referenced
14+
# by `key_item`
15+
j = i - 1
16+
17+
# Run through the list of items (the left
18+
# portion of the array) and find the correct position
19+
# of the element referenced by `key_item`. Do this only
20+
# if the `key_item` is smaller than its adjacent values.
21+
while j >= left and array[j] > key_item:
22+
# Shift the value one position to the left
23+
# and reposition `j` to point to the next element
24+
# (from right to left)
25+
array[j + 1] = array[j]
26+
j -= 1
27+
28+
# When you finish shifting the elements, position
29+
# the `key_item` in its correct location
30+
array[j + 1] = key_item
31+
32+
return array

0 commit comments

Comments
 (0)