Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
finished Linear Search
  • Loading branch information
fellanonymous committed Jan 17, 2019
commit a81341b8e986825a9e065b2566723b2975d7e2bb
31 changes: 16 additions & 15 deletions project/recursive_sorting.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
### helper function
def merge( arrA, arrB ):
elements = len( arrA ) + len( arrB )
# helper function
def merge(arrA, arrB):
elements = len(arrA) + len(arrB)
merged_arr = [0] * elements
a = 0
b = 0
# since arrA and arrB already sorted, we only need to compare the first element of each when merging!
for i in range( 0, elements ):
for i in range(0, elements):
if a >= len(arrA): # all elements in arrA have been merged
merged_arr[i] = arrB[b]
b += 1
Expand All @@ -21,12 +21,12 @@ def merge( arrA, arrB ):
return merged_arr


### recursive sorting function
def merge_sort( arr ):
if len( arr ) > 1:
left = merge_sort( arr[ 0 : len( arr ) / 2 ] )
right = merge_sort( arr[ len( arr ) / 2 : ] )
arr = merge( left, right ) # merge() defined later
# recursive sorting function
def merge_sort(arr):
if len(arr) > 1:
left = merge_sort(arr[0: len(arr) / 2])
right = merge_sort(arr[len(arr) / 2:])
arr = merge(left, right) # merge() defined later
return arr


Expand All @@ -36,20 +36,21 @@ def merge_in_place(arr, start, mid, end):

return arr

def merge_sort_in_place(arr, l, r):

def merge_sort_in_place(arr, l, r):
# TO-DO

return arr
return arrw


# TO-DO: implement the Quick Sort function below
def quick_sort( arr, low, high ):
def quick_sort(arr, low, high):

return arr


# STRETCH: implement the Timsort function below
# hint: check out https://github.com/python/cpython/blob/master/Objects/listsort.txt
def timsort( arr ):
def timsort(arr):

return arr
return arr
38 changes: 20 additions & 18 deletions project/searching.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
# STRETCH: implement Linear Search
# STRETCH: implement Linear Search
def linear_search(arr, target):

# TO-DO: add missing code

return -1 # not found
for value in arr:
if value == target:
return 1
return -1 # not found

# STRETCH: write an iterative implementation of Binary Search


# 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
if len(arr) == 0:
return -1 # array empty

low = 0
high = len(arr)-1

# TO-DO: add missing code
# TO-DO: add missing code

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


# STRETCH: write a recursive implementation of Binary Search
# STRETCH: write a recursive implementation of Binary Search
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
middle = (low+high)/2

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