Skip to content

Commit 60d09ed

Browse files
ElissaElissa
authored andcommitted
updating project files
1 parent b17fd93 commit 60d09ed

14 files changed

Lines changed: 83 additions & 18 deletions

‎.DS_Store‎

0 Bytes
Binary file not shown.

‎project/.DS_Store‎

6 KB
Binary file not shown.

‎project/bubble.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# STRETCH: implement the Bubble Sort function below
2+
def bubble_sort( arr ):
3+
4+
return arr
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# TO-DO: implement the Insertion Sort function below
22
def insertion_sort( arr ):
3+
# loop through n-1 elements
34
for i in range(1, len(arr)):
45
temp = arr[i]
56
j = i
67
while j > 0 and temp < arr[j - 1]:
7-
# shift left
8+
# shift left until correct position found
89
arr[j] = arr[j - 1]
910
j -= 1
10-
11+
# insert at correct position
1112
arr[j] = temp
1213

1314
return arr
File renamed without changes.

‎project/merge.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
### helper function
2+
def merge( arrA, arrB ):
3+
elements = len( arrA ) + len( arrB )
4+
merged_arr = [ ]
5+
a = 0
6+
b = 0
7+
# since arrA and arrB already sorted, we only need to compare the first element of each when merging!
8+
for i in range( 0, elements ):
9+
if a >= len(arrA): # all elements in arrA have been merged
10+
merged_arr.append(arrB[b])
11+
b += 1
12+
elif b >= len(arrB): # all elements in arrB have been merged
13+
merged_arr.append(arrA[a])
14+
a += 1
15+
elif arrA[a] < arrB[b]: # next element in arrA smaller, so add to final array
16+
merged_arr.append(arrA[a])
17+
a += 1
18+
else: # else, next element in arrB must be smaller, so add it to final array
19+
merged_arr.append(arrB[b])
20+
b += 1
21+
return merged_arr
22+
23+
24+
### recursive sorting function
25+
def merge_sort( arr ):
26+
if len( arr ) > 1:
27+
left = merge_sort( arr[ 0 : len( arr ) / 2 ] )
28+
right = merge_sort( arr[ len( arr ) / 2 : ] )
29+
arr = merge( left, right ) # merge() defined later
30+
return arr
31+
32+
33+
# STRETCH: implement an in-place merge sort algorithm
34+
def merge_sort_in_place(arr):
35+
# TO-DO
36+
37+
return arr

‎project/quick.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# TO-DO: implement the Quick Sort function below
2+
def quick_sort( arr ):
3+
4+
return arr

searching.py renamed to project/searching.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
1+
# implementation of Linear Search
22
def linear_search(arr, target):
33
for i in range( 0, len(arr) ):
44
if arr[i] == target:
55
return i
66
return -1 # not found
77

8+
89
# ITERATIVE implementation of Binary Search
910
def binary_search(arr, target):
1011

@@ -22,9 +23,11 @@ def binary_search(arr, target):
2223
low = middle+1 # eliminate LHS
2324
else:
2425
return middle
26+
2527
return -1 # not found
2628

27-
# ITERATIVE implementation of Binary Search
29+
30+
# RECURSIVE implementation of Binary Search
2831
def binary_search_recursive(arr, target, low, high):
2932

3033
middle = (low+high)/2
File renamed without changes.

‎project/selection.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def selection_sort( arr ):
2+
# loop through n-1 elements
3+
for i in range(0, len(arr) - 1):
4+
cur_index = i
5+
smallest_index = cur_index
6+
# find next smallest element
7+
for j in range(cur_index, len(arr)):
8+
if arr[j] < arr[smallest_index]:
9+
smallest_index = j
10+
11+
# swap
12+
temp = arr[smallest_index]
13+
arr[smallest_index] = arr[cur_index]
14+
arr[cur_index] = temp
15+
16+
return arr

0 commit comments

Comments
 (0)