|
| 1 | +import math |
| 2 | + |
1 | 3 | from hypothesis import given |
2 | 4 | from hypothesis import strategies as st |
3 | 5 | from hypothesis.strategies import integers |
4 | 6 | from hypothesis.strategies import lists |
5 | 7 |
|
| 8 | +from book.chapter7.problem1 import hoare_partition |
6 | 9 | from book.chapter7.problem4 import stooge_sort |
7 | 10 | from book.chapter7.problem5 import tre_quicksort |
8 | 11 | from book.chapter7.section1 import quicksort |
@@ -64,6 +67,23 @@ def test_median_of_3_partition(self, data): |
64 | 67 | self.assertGreaterEqual(A[i], A[actual_pivot_index]) |
65 | 68 | self.assertArrayPermuted(A, elements, end=n) |
66 | 69 |
|
| 70 | + @given(st.data()) |
| 71 | + def test_hoare_partition(self, data): |
| 72 | + elements = data.draw(lists(integers(), min_size=1)) |
| 73 | + A = create_array(elements) |
| 74 | + n = len(elements) |
| 75 | + |
| 76 | + actual_split_index = hoare_partition(A, 1, n) |
| 77 | + |
| 78 | + left_max = -math.inf |
| 79 | + right_min = math.inf |
| 80 | + for i in range_of(1, to=actual_split_index): |
| 81 | + left_max = max(left_max, A[i]) |
| 82 | + for i in range_of(actual_split_index + 1, to=n): |
| 83 | + right_min = min(right_min, A[i]) |
| 84 | + self.assertLessEqual(left_max, right_min) |
| 85 | + self.assertArrayPermuted(A, elements, end=n) |
| 86 | + |
67 | 87 | @given(st.data()) |
68 | 88 | def test_stooge_sort(self, data): |
69 | 89 | elements = data.draw(lists(integers(), min_size=1)) |
|
0 commit comments