Skip to content
62 changes: 53 additions & 9 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
# TO-DO: Complete the selection_sort() function below
def selection_sort( arr ):
# in Selection sort we trying to find the minimum value in a list
# so we set the first number as a minimum and we comparing it, to every number to the right
# as soon as we come accross with a number that is the minimum, we assign that as the new minimum and we continue.
# Example: [4,6,2,8,7]. Our first number was 4, however we found out that 2 is the minimum and we set it up to the left.
# the number 2 at the left is a sorted list and to the right is unsorted list. We continue our iteration with the minimum number in our right side and we put it in the left side last position.
# loop through n-1 elements
# we are looping through each item in our collection one at a time
# 0 is the starting number of the index
# we say -1 here as we have only one item in the list we dont want to do a comparison
# instead we can assume that is the highest value as it it the last one
for i in range(0, len(arr) - 1):
# taking in account the smallest element comparing with the largest on
cur_index = i
smallest_index = cur_index
# TO-DO: find next smallest element
# (hint, can do in 3 loc)
# so all elements to the right of the position (cur_index = i)
for j in range(i+1, len(arr)):
# if arr in the position j is less than our smallest_index
if arr[j] < arr[smallest_index]:
# then we need to replace our smallest_index with j
# so we going through all the elements to the right we are currently are on the index
# and if we find something smaller we change that to the smallest index
smallest_index = j




# TO-DO: swap




# at the end before our loop we swap the element located in the current index
# with the smallest item we locate during our loop
# if we found an item which is smaller than the smallest index than our default the we need to swap the items
if smallest_index != i:
arr[cur_index], arr[smallest_index] = arr[smallest_index], arr[cur_index]
return arr


# TO-DO: implement the Bubble Sort function below
def bubble_sort( arr ):
# takes an unsorted list and orders it to sign in values
# so we have the lowest in the begining and the highest number will be at the end of our list
# we comparing the highest values together and moving the highest to the right
# we repeat the comparison in every iteration so every highest value will be at the right side
# we continue looping at this list until all numbers will be sorted
# when we sort all our items we need to create a way to break the iterations

def bubble_sort( arr ):
# we need the indexing_length were we are going to make the comparison
# we say -1 because we cant compare with a number of the list if there is no number there
indexing_length = len(arr)-1
# we use a local variable for this function
# we using the sorted variable to a control to break us out whenever the list is been sorted
sorted = False
while not sorted:
sorted = True
for i in range(0, indexing_length):
# so if arr position i to the left is greater than the arr to the position to the right
if arr[i] > arr[i+1]:
# this sorted variable is false
sorted = False
# we need to flip these two values in our list
arr[i], arr[i+1] = arr[i+1], arr[i]
# so in the end if statement will activate and the sorted variable will remain true
# and that will break us out the while loop
return arr

print(bubble_sort([3,5,7,9]))


# STRETCH: smallest_index
# a technique based on keys in a specific range
# it works by counting the number of objects having distinct keys

# STRETCH: implement the Count Sort function below
def count_sort( arr, maximum=-1 ):

return arr
25 changes: 23 additions & 2 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@ def merge( arrA, arrB ):
elements = len( arrA ) + len( arrB )
merged_arr = [0] * elements
# TO-DO

a = 0 # initial array [1 4 9 10]
b = 0 # initial array [ 2 3 6 7 8]
# put both element list in one array [1 2 3 4 6 7 8 9 10]
# arrA & arrB are sorted , we might need to compare the first element of each list before merging!
# Therefore:
for el in range(0, elements):# for every element in range from (0 to elements)
if a >= len(arrA):#
merged_arr[el] = arrB[b]
b += 1
elif b >= len(arrB):
merged_arr[el] = arrA[a]
a += 1
elif arrA[a] < arrB[b]:
merged_arr[el] = arrA[a]
a += 1
else:
merged_arr[el] = arrB[b]
b += 1
return merged_arr


# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort( arr ):
# TO-DO

# base case
if len(arr) >1:
left = merge_sort(arr[0:len(arr) // 2]) # Dividing the array elements
right = merge_sort(arr[len(arr) // 2:]) # into 2 lists
arr = merge(merge_sort(left), merge_sort(right))
return arr


Expand Down
49 changes: 39 additions & 10 deletions src/searching/searching.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
# STRETCH: implement Linear Search
def linear_search(arr, target):
for i in range(len(arr)):
if(arr[i]==target):
return i

# TO-DO: add missing code

return -1 # not found
return -1 # not found


# STRETCH: write an iterative implementation of Binary Search
def binary_search(arr, target):

if len(arr) == 0:
return -1 # array empty

low = 0
high = len(arr)-1

# TO-DO: add missing code
# The goal of the Binary is to look to an order sequence and determine a value that we looking for
# is in that sequence or not. Example : [1, 3, 4, 6, 7, 8, 9] the number we are looking for is 7.
# The way we are doing is to find the mindpoint and comparing with the number we are looking for.
# If is it higher we choose the items to right or if it is lower we choose the items to the left
# if we find the number we are searching , we stop the algorithm and return the position to the item

def binary_search(arr, target):
left = 0 # we start with the first element
right = len(arr)-1 # and we finish with the last element

while left <= right:
midpoint = left + (right - left) // 2 # we want the midpoint here so we put 2
midpoint_value = arr[midpoint]
if midpoint_value == target: # if we found the target equal to the midpoint return as back the position
return midpoint
elif target < midpoint_value: # the target we are looking for will be at the left
right = midpoint -1 # the position will be directly to the left of the midpoint
else:
left = midpoint + 1 # if the target is greater then we need to check if it is at the right and repositioning

return -1 # not found


# STRETCH: write a recursive implementation of Binary Search
# we having an index of 5 numbers: [4,7,8,12,45,99] and we set 4 the low number and 99 the high number
# we need to determine a middle index in these array of numbers.
# to do so we say middle = (Lower + Upper) devided by 2: 0 + 5 /2 = 2 (0=4, 5=99)
# searching for 45: we count the index array until the number 45 and devided by 2. The middle is 8
# checking to see if our middle number is the number we are searching it
# if the search value is bigger than the one we searching then middle number becomes new low abd the list came out smaller
# so the new list will be [8, 12, 45, 99] same calculations again and the new list will be [12, 45, 99]

def binary_search_recursive(arr, target, low, high):

middle = (low+high)//2

if len(arr) == 0:
return -1 # array empty
# TO-DO: add missing if/else statements, recursive calls

# if value is smaller than the middle , the it can only be in the left index
if arr[middle] > target:
return binary_search_recursive(arr, target, low, middle-1)
elif arr[middle]<target:
return binary_search_recursive(arr, target, middle+1,high)
else:
return middle