Skip to content

Commit b17fd93

Browse files
ElissaElissa
authored andcommitted
adding solution branch
some initial solution code, tests
1 parent cd6b43c commit b17fd93

9 files changed

Lines changed: 132 additions & 10 deletions

File tree

‎.DS_Store‎

6 KB
Binary file not shown.

‎Sorting.md‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ The process of performing a binary search has a couple of extra steps. First, th
2121

2222
1. Compare the item in the middle of the data set to the item we are searching for.
2323
- If it is the same, stop. We found it and are done!
24+
- Else, if the middle was the last item to search, return that the item was NOT found
2425
- Else, if the item we are searching for is LESS than the item in the middle:
25-
- Eliminate the RHS of list. Repeat step 1 with only the LHS of list.
26-
- Else: ...
26+
- Eliminate the `RHS` of list. Repeat step 1 with only the `LHS` of list.
27+
- Else, the item we are searching for is GREATER than the item in the middle:
28+
- Eliminate the `LHS` of list. Repeat step 1 with only `RHS` of list.
2729

2830

2931
A visualization comparing these two algorithms is shown below.
@@ -99,36 +101,35 @@ While ***Selection Sort*** is one of the easier sorting algorithms to understand
99101
Recall that the runtime complexity of an algorithm, often expressed using *Big O notation*, tells us how the amount of operations our algorithm requires will grow as the size of our input grows. ***Selection Sort*** will have a runtime of O(n²) in *all* cases, making it impractical to use with many large, real-world data sets.
100102

101103
#### Check for understanding
102-
1. Is ***Selection Sort*** a comparison sorting algorithm?
103-
<details><summary>Answer</summary> Yes, since to <i>select</i> the desired element, we compare a current value to the rest of the "unsorted" segment of our list or array.
104-
</details>
105104

106-
2. Why do we end our loop before processing the item in the last index of an array when performing ***Selection Sort***?
105+
1. Why do we end our loop before processing the item in the last index of an array when performing ***Selection Sort***?
107106
<details><summary>Answer</summary> By the nature of how this algorithm works, if all other elements have been sorted, the last element will have been moved to the correct index.
108107

109-
3. When using ***Selection Sort***, what is the difference between the order or arrangement of elements in *best case* versus *worst case*?
108+
2. When using ***Selection Sort***, what is the difference between the order or arrangement of elements in *best case* versus *worst case*?
110109
<details><summary>Answer</summary> None, since the same number of operations will be performed <b>regardless</b> of how elements are ordered.
111110
</details>
112111

113-
4. When using ***Selection Sort***, what would be the runtime of sorting elements in the best, average, and worst cases?
112+
3. When using ***Selection Sort***, what would be the runtime of sorting elements in the best, average, and worst cases?
114113
<details><summary>Answer</summary> <ul>
115114
<li>Best case: O(n²)</li>
116115
<li>Average case: O(n²)</li>
117116
<li>Worst case: O(n²)</li>
118117
</ul>
119118
</details>
120-
5. Show how the array **[4, 8, 3, 1, 9, 6]** changes as it is being sorted using ***Selection Sort***. To do this, write out the contents of the array after each pass of the algorithm. (_hint...we should only need n-1 passes to sort the array, where n is the size_)
119+
4. Show how the array **[4, 8, 3, 1, 9, 6]** changes as it is being sorted using ***Selection Sort***. To do this, write out the contents of the array after each pass of the algorithm. (_hint...we should only need n-1 passes to sort the array, where n is the size_)
121120
<details><summary>Answer</summary> <pre><samp>[1, 8, 3, 4, 9, 6]
122121
[1, 3, 8, 4, 9, 6]
123122
[1, 3, 4, 8, 9, 6]
124123
[1, 3, 4, 6, 9, 8]
125124
[1, 3, 4, 6, 8, 9] </samp></pre></details>
126125

127126
### Insertion Sort
128-
[overview]
127+
***Insertion Sort***, like Selection Sort is an iterative sorting algorithm. However, it improves slightly on the performance of Selection Sort. Imagine you lay out several playing cards in a line on a table. Consider the first card to be a *sorted* subarray of lengh = 1. Pick up each card (in this case, from left to right) and insert it into the correct position in the *sorted* subarray. Shift other cards to the right to make room for the card you are inserting as you go.
129128

130129
[(VIDEO) Insert-sort with Romanian folk dance ![alt text](https://i.ytimg.com/vi/ROalU379l3U/hqdefault.jpg)](https://www.youtube.com/watch?v=ROalU379l3U)
131130

131+
The manner in which we process elements when performing ***Insertion Sort*** means that, depending on the original order of elements, on each pass of the array, we may only have to do 1 comparison to determine if an element is in the right position. However, in a worst-case scenario, we will perform just as many comparisons as with Selection Sort, meaning the runtime of this algorithm is also O(n²).
132+
132133
#### Algorithm
133134
1. Separate the first element from the rest of the array. Consider it a sorted list of one element.
134135

@@ -222,6 +223,8 @@ Recursive cases *must* be written in a way that will eventually allow us to reac
222223

223224
[(VIDEO) Merge-sort with Transylvanian-saxon (German) folk dance ![alt text](https://i.ytimg.com/vi/XaqR3G_NVoo/hqdefault.jpg)](https://www.youtube.com/watch?v=XaqR3G_NVoo)
224225

226+
[runtime]
227+
225228
#### Algorithm
226229
```
227230
TBC
@@ -278,6 +281,7 @@ Have you ever wondered how some of the languages you use actually implement thei
278281

279282
[(VIDEO) Quick-sort with Hungarian folk dance ![alt text](https://i.ytimg.com/vi/ywWBy6J5gz8/hqdefault.jpg)](https://www.youtube.com/watch?v=ywWBy6J5gz8)
280283

284+
[runtime]
281285

282286
#### Algorithm
283287
```

‎insertion.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# TO-DO: implement the Insertion Sort function below
2+
def insertion_sort( arr ):
3+
for i in range(1, len(arr)):
4+
temp = arr[i]
5+
j = i
6+
while j > 0 and temp < arr[j - 1]:
7+
# shift left
8+
arr[j] = arr[j - 1]
9+
j -= 1
10+
11+
arr[j] = temp
12+
13+
return arr

‎insertion.pyc‎

498 Bytes
Binary file not shown.

‎searching.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
def linear_search(arr, target):
3+
for i in range( 0, len(arr) ):
4+
if arr[i] == target:
5+
return i
6+
return -1 # not found
7+
8+
# ITERATIVE implementation of Binary Search
9+
def binary_search(arr, target):
10+
11+
if len(arr) == 0:
12+
return -1 # array empty
13+
14+
low = 0
15+
high = len(arr)-1
16+
17+
while low <= high:
18+
middle = (low+high)/2
19+
if target < arr[middle]:
20+
high = middle-1 # eliminate RHS
21+
elif target > arr[middle]:
22+
low = middle+1 # eliminate LHS
23+
else:
24+
return middle
25+
return -1 # not found
26+
27+
# ITERATIVE implementation of Binary Search
28+
def binary_search_recursive(arr, target, low, high):
29+
30+
middle = (low+high)/2
31+
32+
if len(arr) == 0:
33+
return -1 # array empty
34+
elif low > high:
35+
return -1 # not found
36+
elif arr[middle] == target:
37+
return middle
38+
else:
39+
if target < arr[middle]:
40+
high = middle-1 # eliminate RHS
41+
else:
42+
low = middle+1 # eliminate LHS
43+
return binary_search_recursive(arr, target, low, high)

‎searching.pyc‎

1.14 KB
Binary file not shown.

‎selection.py‎

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

‎selection.pyc‎

553 Bytes
Binary file not shown.

‎tests.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import unittest
2+
from searching import *
3+
from selection import selection_sort
4+
from insertion import insertion_sort
5+
6+
class SortingTest(unittest.TestCase):
7+
8+
def test_linear_search(self):
9+
arr1 = [-2, 7, 3, -9, 5, 1, 0, 4, -6]
10+
arr2 = []
11+
12+
self.assertEqual(linear_search(arr1, 6), -1)
13+
self.assertEqual(linear_search(arr1, -6), 8)
14+
self.assertEqual(linear_search(arr1, 0), 6)
15+
self.assertEqual(linear_search(arr2, 3), -1)
16+
17+
18+
def test_binary_search(self):
19+
arr1 = [-9, -8, -6, -4, -3, -2, 0, 1, 2, 3, 5, 7, 8, 9]
20+
arr2 = []
21+
22+
self.assertEqual(binary_search(arr1, -8), 1)
23+
self.assertEqual(binary_search(arr1, 0), 6)
24+
self.assertEqual(binary_search(arr2, 6), -1)
25+
self.assertEqual(binary_search(arr2, 0), -1)
26+
27+
28+
def test_binary_search_recursive(self):
29+
arr1 = [-9, -8, -6, -4, -3, -2, 0, 1, 2, 3, 5, 7, 8, 9]
30+
arr2 = []
31+
32+
self.assertEqual(binary_search_recursive(arr1, -8, 0, len(arr1)-1), 1)
33+
self.assertEqual(binary_search_recursive(arr1, 0, 0, len(arr1)-1), 6)
34+
self.assertEqual(binary_search_recursive(arr2, 6, 0, len(arr1)-1), -1)
35+
self.assertEqual(binary_search_recursive(arr2, 0, 0, len(arr1)-1), -1)
36+
37+
38+
def test_selection(self):
39+
arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
40+
arr2 = []
41+
arr3 = [0, 1, 2, 3, 4, 5]
42+
43+
self.assertEqual(selection_sort(arr1), [0,1,2,3,4,5,6,7,8,9])
44+
self.assertEqual(selection_sort(arr2), [])
45+
self.assertEqual(selection_sort(arr3), [0,1,2,3,4,5])
46+
47+
if __name__ == '__main__':
48+
unittest.main()

0 commit comments

Comments
 (0)