Skip to content
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
39 changes: 23 additions & 16 deletions project/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
# Complete the selection_sort() function below in class with your instructor
def selection_sort( arr ):


def selection_sort(arr):
# loop through n-1 elements
for i in range(0, len(arr) - 1):
cur_index = i
smallest_index = cur_index
# TO-DO: find next smallest element
# (hint, can do in 3 loc)




# TO-DO: swap




# # (hint, can do in 3 loc)
for j in range(cur_index, len(arr)):
if arr[j] < arr[smallest_index]:
smallest_index = j
# TO-DO: swap
arr[cur_index], arr[smallest_index] = arr[smallest_index], arr[cur_index]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job with the inline swapping syntax.

return arr

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works! Great job on this function.



# TO-DO: implement the Insertion Sort function below
def insertion_sort( arr ):


def insertion_sort(arr):
for i in range(1, len(arr)):
cur_index = i
while cur_index > 0 and arr[cur_index] < arr[cur_index - 1]:
temp = arr[cur_index - 1]
arr[cur_index - 1] = arr[cur_index]
arr[cur_index] = temp
cur_index -= 1
return arr

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to say you got this one working, too. Maybe today you can work on quicksort in the recursive file. 😄



# STRETCH: implement the Bubble Sort function below
def bubble_sort( arr ):


def bubble_sort(arr):

return arr


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

return arr
return arr
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
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,5 @@ While ***Quick Sort*** has "quick" in its name, it is typically not used as freq
- What programming languages use **Timsort** to implement their built-in `sort()` functions?
- If an interviewer asked you to describe the **Tim Sort** algorithm in 3-4 sentences, what would you say?
- Can you implement **Tim Sort** in Python?

PR: https://github.com/LambdaSchool/Sorting/pull/2