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

Skip to content

added two new algo quick & merge #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

Merged
merged 2 commits into from
Oct 1, 2021
Merged
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
30 changes: 30 additions & 0 deletions Other Algorithms/Bubble_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def bubble_sort(array):
n = len(array)

for i in range(n):
# Create a flag that will allow the function to
# terminate early if there's nothing left to sort
already_sorted = True

# Start looking at each item of the list one by one,
# comparing it with its adjacent value. With each
# iteration, the portion of the array that you look at
# shrinks because the remaining items have already been
# sorted.
for j in range(n - i - 1):
if array[j] > array[j + 1]:
# If the item you're looking at is greater than its
# adjacent value, then swap them
array[j], array[j + 1] = array[j + 1], array[j]

# Since you had to swap two elements,
# set the `already_sorted` flag to `False` so the
# algorithm doesn't finish prematurely
already_sorted = False

# If there were no swaps during the last iteration,
# the array is already sorted, and you can terminate
if already_sorted:
break

return array
39 changes: 39 additions & 0 deletions Other Algorithms/Merge_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def merge(left, right):
# If the first array is empty, then nothing needs
# to be merged, and you can return the second array as the result
if len(left) == 0:
return right

# If the second array is empty, then nothing needs
# to be merged, and you can return the first array as the result
if len(right) == 0:
return left

result = []
index_left = index_right = 0

# Now go through both arrays until all the elements
# make it into the resultant array
while len(result) < len(left) + len(right):
# The elements need to be sorted to add them to the
# resultant array, so you need to decide whether to get
# the next element from the first or the second array
if left[index_left] <= right[index_right]:
result.append(left[index_left])
index_left += 1
else:
result.append(right[index_right])
index_right += 1

# If you reach the end of either array, then you can
# add the remaining elements from the other array to
# the result and break the loop
if index_right == len(right):
result += left[index_left:]
break

if index_left == len(left):
result += right[index_right:]
break

return result
28 changes: 28 additions & 0 deletions Other Algorithms/Quick_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from random import randint

def quicksort(array):
# If the input array contains fewer than two elements,
# then return it as the result of the function
if len(array) < 2:
return array

low, same, high = [], [], []

# Select your `pivot` element randomly
pivot = array[randint(0, len(array) - 1)]

for item in array:
# Elements that are smaller than the `pivot` go to
# the `low` list. Elements that are larger than
# `pivot` go to the `high` list. Elements that are
# equal to `pivot` go to the `same` list.
if item < pivot:
low.append(item)
elif item == pivot:
same.append(item)
elif item > pivot:
high.append(item)

# The final result combines the sorted `low` list
# with the `same` list and the sorted `high` list
return quicksort(low) + same + quicksort(high)
32 changes: 32 additions & 0 deletions Other Algorithms/Tim_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def insertion_sort(array, left=0, right=None):
if right is None:
right = len(array) - 1

# Loop from the element indicated by
# `left` until the element indicated by `right`
for i in range(left + 1, right + 1):
# This is the element we want to position in its
# correct place
key_item = array[i]

# Initialize the variable that will be used to
# find the correct position of the element referenced
# by `key_item`
j = i - 1

# Run through the list of items (the left
# portion of the array) and find the correct position
# of the element referenced by `key_item`. Do this only
# if the `key_item` is smaller than its adjacent values.
while j >= left and array[j] > key_item:
# Shift the value one position to the left
# and reposition `j` to point to the next element
# (from right to left)
array[j + 1] = array[j]
j -= 1

# When you finish shifting the elements, position
# the `key_item` in its correct location
array[j + 1] = key_item

return array