From d618c81c4af44b52561ea8793d3b50dc7328f729 Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Mon, 15 Jun 2020 18:02:39 +0300 Subject: [PATCH 01/11] selection sort method complete --- src/iterative_sorting/iterative_sorting.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..de8ced26 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,16 +1,22 @@ # TO-DO: Complete the selection_sort() function below def selection_sort( arr ): # 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 for i in range(0, len(arr) - 1): + # taking in account the smallest argument comparing with the largest on cur_index = i smallest_index = cur_index # TO-DO: find next smallest element # (hint, can do in 3 loc) + for j in range(cur_index+1, len(arr)): + if arr[j]< arr[smallest_index]: + smallest_index = j - - - # TO-DO: swap + # at the end before our loop we swap the argument located in the current index + # with the smallest item we locate during our loop + arr[cur_index], arr[cur_index] = arr[cur_index], arr[smallest_index] From 01835f6eb7cb4357e01940f598ea7d7957200b3a Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Mon, 15 Jun 2020 18:26:59 +0300 Subject: [PATCH 02/11] adding more comments in sorting --- src/iterative_sorting/iterative_sorting.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index de8ced26..e64e52d4 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -3,24 +3,25 @@ def selection_sort( arr ): # 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 argument comparing with the largest on cur_index = i smallest_index = cur_index # TO-DO: find next smallest element # (hint, can do in 3 loc) - for j in range(cur_index+1, len(arr)): - if arr[j]< arr[smallest_index]: - smallest_index = j + # 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[smallest_index] > arr[j]: + # then we need to replace our smallest_index with j + smallest_index = j # TO-DO: swap # at the end before our loop we swap the argument located in the current index # with the smallest item we locate during our loop - arr[cur_index], arr[cur_index] = arr[cur_index], arr[smallest_index] - - - - + arr[cur_index], arr[smallest_index] = arr[smallest_index], arr[cur_index] return arr From 801759362081a1f6d4e772e24cbabaeb14345a31 Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Mon, 15 Jun 2020 19:02:16 +0300 Subject: [PATCH 03/11] sorted method done --- src/iterative_sorting/iterative_sorting.py | 40 ++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e64e52d4..e5986e00 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -6,7 +6,7 @@ def selection_sort( arr ): # 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 argument comparing with the largest on + # taking in account the smallest element comparing with the largest on cur_index = i smallest_index = cur_index # TO-DO: find next smallest element @@ -14,24 +14,52 @@ def selection_sort( arr ): # 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[smallest_index] > arr[j]: + 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 smallest_index = j # TO-DO: swap - # at the end before our loop we swap the argument located in the current index + # at the end before our loop we swap the element located in the current index # with the smallest item we locate during our loop - arr[cur_index], arr[smallest_index] = arr[smallest_index], arr[cur_index] + # 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: implement the Count Sort function below +# STRETCH: smallest_index def count_sort( arr, maximum=-1 ): return arr \ No newline at end of file From d9d01c3e44867baf9130210635aa25ad9ad814fd Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Mon, 15 Jun 2020 21:34:09 +0300 Subject: [PATCH 04/11] linear stretch complete --- src/iterative_sorting/iterative_sorting.py | 3 +++ src/searching/searching.py | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e5986e00..8faa228f 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -60,6 +60,9 @@ def bubble_sort( arr ): # STRETCH: smallest_index +# a technique based on keys in a specific range +# it works by counting the number of objects having distinct keys + def count_sort( arr, maximum=-1 ): return arr \ No newline at end of file diff --git a/src/searching/searching.py b/src/searching/searching.py index d4969a0c..d520283b 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -1,9 +1,12 @@ # 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 From 8740efc0090c08b5aa8d9e9fbb070fb025ebbec3 Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Mon, 15 Jun 2020 22:13:55 +0300 Subject: [PATCH 05/11] binary complete --- src/searching/searching.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/searching/searching.py b/src/searching/searching.py index d520283b..360fe599 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -10,16 +10,26 @@ def linear_search(arr, target): # 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 From 31bd106aa8b86f4835cc6b2ce10e76ca551e659e Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Tue, 16 Jun 2020 10:12:07 +0300 Subject: [PATCH 06/11] adding selection sort comments --- src/iterative_sorting/iterative_sorting.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 8faa228f..8d40c31b 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,5 +1,10 @@ # 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 @@ -16,7 +21,8 @@ def selection_sort( 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 + # 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 From 4ef09e24ffbd6f56b137a4026ad76b5aeb0dfe30 Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Tue, 16 Jun 2020 10:43:01 +0300 Subject: [PATCH 07/11] attempt recursive binary search implementation --- src/searching/searching.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/searching/searching.py b/src/searching/searching.py index 360fe599..5186a2b8 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -41,3 +41,11 @@ def binary_search_recursive(arr, target, low, high): 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] Date: Tue, 16 Jun 2020 14:49:44 +0300 Subject: [PATCH 08/11] adding comment to recursive binary search stretch --- src/searching/searching.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/searching/searching.py b/src/searching/searching.py index 5186a2b8..04ecdc0f 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -34,6 +34,14 @@ def binary_search(arr, target): # 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 From 32232bd61a7f89f474e9dffe33fb107ab25fd268 Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Tue, 16 Jun 2020 19:19:51 +0300 Subject: [PATCH 09/11] attempting merge sort with recursion --- src/recursive_sorting/recursive_sorting.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..ca430297 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -3,6 +3,7 @@ def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements # TO-DO + return merged_arr @@ -10,7 +11,12 @@ def merge( arrA, arrB ): # TO-DO: implement the Merge Sort function below USING RECURSION def merge_sort( arr ): # TO-DO - + # base case + if len(arr) >1: + mid = len(arr)//2 # Finding the mid of the array + Left = arr[0:mid] # Dividing the array elements + Right = arr[mid:] # into 2 halves + arr = merge(merge_sort(Left), merge_sort(Right)) return arr From 5837fcc83011b6782eb3f5a0c2d5b91c97182706 Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Tue, 16 Jun 2020 20:01:15 +0300 Subject: [PATCH 10/11] helper function --- src/recursive_sorting/recursive_sorting.py | 26 +++++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index ca430297..3c89a9ad 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -3,8 +3,23 @@ def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements # TO-DO - - + a = 0 + b = 0 + # 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 @@ -13,10 +28,9 @@ def merge_sort( arr ): # TO-DO # base case if len(arr) >1: - mid = len(arr)//2 # Finding the mid of the array - Left = arr[0:mid] # Dividing the array elements - Right = arr[mid:] # into 2 halves - arr = merge(merge_sort(Left), merge_sort(Right)) + 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 From 5b8ae497bba5669c86ad51df3d29f2edbe00f129 Mon Sep 17 00:00:00 2001 From: Dimos-Christidis Date: Tue, 16 Jun 2020 20:19:25 +0300 Subject: [PATCH 11/11] adding comments --- src/recursive_sorting/recursive_sorting.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index 3c89a9ad..cf46e102 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -3,8 +3,9 @@ def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements # TO-DO - a = 0 - b = 0 + 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)