From 6d81693f149673af993039412ea2e1fe056b1648 Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Wed, 18 Mar 2015 14:56:52 -0700 Subject: [PATCH 1/9] Added quick sort method first try, still not functional. --- q_sort.py | 30 ++++++++++++++++++++++++++++++ test_q_sort.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 q_sort.py create mode 100644 test_q_sort.py diff --git a/q_sort.py b/q_sort.py new file mode 100644 index 0000000..e192f63 --- /dev/null +++ b/q_sort.py @@ -0,0 +1,30 @@ +def q_sort(sequence): + if len(sequence) == 1: + return + pivot = get_pivot(sequence) + left = [] + right = [] + for item in sequence: + if item > pivot and item is not pivot: + right.append(item) + else: + left.append(item) + right = q_sort(right) + left = q_sort(left) + + if len(sequence) > 2: + return left + [pivot] + right + else: + return left + right + +def get_pivot(sequence): + first = sequence[0] + last = sequence[-1] + mid_index = len(sequence) // 2 + middle = sequence[mid_index] + + if last > first > middle or middle > first > last: + return first + if last > middle > first or first > middle > last: + return middle + return last diff --git a/test_q_sort.py b/test_q_sort.py new file mode 100644 index 0000000..b55f951 --- /dev/null +++ b/test_q_sort.py @@ -0,0 +1,28 @@ +from q_sort import get_pivot, q_sort + + +def test_get_pivot_f(): + a = [2, 1, 3] + assert get_pivot(a) == 2 + b = [2, 3, 1] + assert get_pivot(b) == 2 + + +def test_get_pivot_l(): + a = [1, 3, 2] + assert get_pivot(a) == 2 + b = [3, 1, 2] + assert get_pivot(b) == 2 + + +def test_get_pivot_m(): + a = [1, 5, 5, 2, 1, 1, 3] + assert get_pivot(a) == 2 + b = [3, 2, 1] + assert get_pivot(b) == 2 + + +def test_q_one(): + a = [4, 3, 2, 5, 7, 1, 6] + a = q_sort(a) + assert a == [1, 2, 3, 4, 5, 6, 7] From 0d08d304f84b9c95e4a6df3b10744daa3b00ccd1 Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Wed, 18 Mar 2015 15:35:32 -0700 Subject: [PATCH 2/9] Fixed general bugs. TODO: handle multiple occurences of values. --- q_sort.py | 29 ++++++++++++++++------------- test_q_sort.py | 12 ++++++------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/q_sort.py b/q_sort.py index e192f63..99ee3a5 100644 --- a/q_sort.py +++ b/q_sort.py @@ -1,21 +1,24 @@ def q_sort(sequence): - if len(sequence) == 1: - return + if len(sequence) <= 1: + # Return a sequence of one sorted item, or stop if no input + return sequence + if len(sequence) == 2: + # Return a sequence of two sorted items + if sequence[1] > sequence[0]: + return sequence + else: + return sequence[::-1] + pivot = get_pivot(sequence) left = [] right = [] for item in sequence: - if item > pivot and item is not pivot: + if item > pivot[0] and item is not pivot[0]: right.append(item) - else: + elif item is not pivot[0]: left.append(item) - right = q_sort(right) - left = q_sort(left) - if len(sequence) > 2: - return left + [pivot] + right - else: - return left + right + return q_sort(left)+ pivot + q_sort(right) def get_pivot(sequence): first = sequence[0] @@ -24,7 +27,7 @@ def get_pivot(sequence): middle = sequence[mid_index] if last > first > middle or middle > first > last: - return first + return [first] if last > middle > first or first > middle > last: - return middle - return last + return [middle] + return [last] diff --git a/test_q_sort.py b/test_q_sort.py index b55f951..f961de1 100644 --- a/test_q_sort.py +++ b/test_q_sort.py @@ -3,23 +3,23 @@ def test_get_pivot_f(): a = [2, 1, 3] - assert get_pivot(a) == 2 + assert get_pivot(a) == [2] b = [2, 3, 1] - assert get_pivot(b) == 2 + assert get_pivot(b) == [2] def test_get_pivot_l(): a = [1, 3, 2] - assert get_pivot(a) == 2 + assert get_pivot(a) == [2] b = [3, 1, 2] - assert get_pivot(b) == 2 + assert get_pivot(b) == [2] def test_get_pivot_m(): a = [1, 5, 5, 2, 1, 1, 3] - assert get_pivot(a) == 2 + assert get_pivot(a) == [2] b = [3, 2, 1] - assert get_pivot(b) == 2 + assert get_pivot(b) == [2] def test_q_one(): From ffcb140af915a11ed7acaa01d8a235c888ca694b Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Wed, 18 Mar 2015 15:35:49 -0700 Subject: [PATCH 3/9] fixed q_sort to work and wrote a new test --- q_sort.py | 31 +++++++++++++++++++++---------- test_q_sort.py | 2 ++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/q_sort.py b/q_sort.py index e192f63..e26b2aa 100644 --- a/q_sort.py +++ b/q_sort.py @@ -1,21 +1,32 @@ -def q_sort(sequence): +def q_sort(sequence): + print sequence if len(sequence) == 1: - return + return sequence + if len(sequence) == 2: + if sequence[0] < sequence[1]: + return sequence + else: + return sequence[::-1] pivot = get_pivot(sequence) left = [] right = [] for item in sequence: - if item > pivot and item is not pivot: - right.append(item) - else: - left.append(item) + if item != pivot: + if item > pivot: + right.append(item) + else: + left.append(item) right = q_sort(right) left = q_sort(left) - if len(sequence) > 2: - return left + [pivot] + right - else: - return left + right + new_list = [] + if left: + new_list += left + new_list.append(pivot) + if right: + new_list += right + return new_list + def get_pivot(sequence): first = sequence[0] diff --git a/test_q_sort.py b/test_q_sort.py index b55f951..5b21628 100644 --- a/test_q_sort.py +++ b/test_q_sort.py @@ -6,6 +6,8 @@ def test_get_pivot_f(): assert get_pivot(a) == 2 b = [2, 3, 1] assert get_pivot(b) == 2 + c = [1, 2, 3, 4] + assert get_pivot(c) == 3 def test_get_pivot_l(): From 984091ab449c2f871f83bb9f8d42717fa4f885ab Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Wed, 18 Mar 2015 16:09:42 -0700 Subject: [PATCH 4/9] Added a simple tes for repeated values. --- q_sort.py | 27 +++++++++++++++++++++++++++ test_q_sort.py | 6 ++++++ 2 files changed, 33 insertions(+) diff --git a/q_sort.py b/q_sort.py index 99ee3a5..03e39bf 100644 --- a/q_sort.py +++ b/q_sort.py @@ -1,3 +1,5 @@ +from merge_sort import timed_func + def q_sort(sequence): if len(sequence) <= 1: # Return a sequence of one sorted item, or stop if no input @@ -31,3 +33,28 @@ def get_pivot(sequence): if last > middle > first or first > middle > last: return [middle] return [last] + + +@timed_func +def timed_q_sort(sequence): + return q_sort(sequence) + +def worst_maker(da_size): + a = True + worst = [] + for num in range(da_size): + if a: + worst.insert(0, num) + a = False + else: + worst.append(num) + return worst + + +if __name__ == '__main__': + inputs = [range(1000), range(10000)] + inputs.append(worst_maker(1000)) + inputs.append(worst_maker(10000)) + + for inp in inputs: + timed_q_sort(inp) diff --git a/test_q_sort.py b/test_q_sort.py index f961de1..4408f22 100644 --- a/test_q_sort.py +++ b/test_q_sort.py @@ -26,3 +26,9 @@ def test_q_one(): a = [4, 3, 2, 5, 7, 1, 6] a = q_sort(a) assert a == [1, 2, 3, 4, 5, 6, 7] + + +def test_q_mixed(): + a = [1, 2, 5, 4, 4, 6, 7, 9, 8, 0, 0] + a = q_sort(a) + assert a == [0, 0, 1, 2, 4, 4, 5, 6, 7, 8, 9] \ No newline at end of file From 4585a3914de1e28c4fb9613b25335040ba5188b5 Mon Sep 17 00:00:00 2001 From: Matthew Lee Date: Wed, 18 Mar 2015 16:22:50 -0700 Subject: [PATCH 5/9] Added demonstration of the worst. --- q_sort.py | 22 +++++++++------------- test_q_sort.py | 4 ++-- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/q_sort.py b/q_sort.py index 4ec8dc1..b69ce31 100644 --- a/q_sort.py +++ b/q_sort.py @@ -43,22 +43,18 @@ def get_pivot(sequence): def timed_q_sort(sequence): return q_sort(sequence) -def worst_maker(da_size): - a = True - worst = [] - for num in range(da_size): - if a: - worst.insert(0, num) - a = False - else: - worst.append(num) - return worst + +def same_maker(how_many): + moAr = [] + for each in range(how_many): + moAr.append(1) + return moAr if __name__ == '__main__': - inputs = [range(1000), range(10000)] - inputs.append(worst_maker(1000)) - inputs.append(worst_maker(10000)) + inputs = [range(500), range(1000)] + inputs.append(same_maker(500)) + inputs.append(same_maker(997)) for inp in inputs: timed_q_sort(inp) diff --git a/test_q_sort.py b/test_q_sort.py index d330748..f49aac8 100644 --- a/test_q_sort.py +++ b/test_q_sort.py @@ -29,9 +29,9 @@ def test_q_one(): def test_q_two(): - a = [5, 5, 5, 5] + a = [5, 5, 5, 5, 5, 5, 5, 5, 5] a = q_sort(a) - assert a == [5, 5, 5, 5] + assert a == [5, 5, 5, 5, 5, 5, 5, 5, 5] def test_q_mixed(): From b4162372e5378a6b942f83d03a0ac2729a79a554 Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Thu, 2 Apr 2015 01:10:22 -0700 Subject: [PATCH 6/9] redid the sort in a, i hope, clearer way --- q_sort.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/q_sort.py b/q_sort.py index 0b34e91..3fe4b48 100644 --- a/q_sort.py +++ b/q_sort.py @@ -11,17 +11,15 @@ def q_sort(sequence): pivot = get_pivot(sequence) left = [] right = [] - not_pivot = False - for item in sequence: - - if item > pivot[0] and item is not pivot[0]: + for index, item in enumerate(sequence): + # to prevent recursion error + first_two_same = (index == 0 and (sequence[0] == sequence[1])) + if item > pivot[0] or first_two_same: right.append(item) - elif item is not pivot[0] or not_pivot: + elif item <= pivot[0]: left.append(item) - elif item == pivot[0]: - # after not putting the first pivot in, put the rest in - not_pivot = True - return q_sort(left) + pivot + q_sort(right) + + return q_sort(left) + q_sort(right) def get_pivot(sequence): @@ -34,4 +32,4 @@ def get_pivot(sequence): return [first] if last > middle > first or first > middle > last: return [middle] - return [last] \ No newline at end of file + return [last] From 8ac521318bc468ee42a5d5f65c96f3c8eb317658 Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Thu, 2 Apr 2015 11:50:37 -0700 Subject: [PATCH 7/9] quick sort works and added a test for when the last two numbers are the same --- q_sort.py | 12 +++++++----- test_q_sort.py | 19 ++++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/q_sort.py b/q_sort.py index 3fe4b48..a19edaf 100644 --- a/q_sort.py +++ b/q_sort.py @@ -9,16 +9,18 @@ def q_sort(sequence): else: return sequence[::-1] pivot = get_pivot(sequence) + print "pivot: " + str(pivot) + print sequence left = [] right = [] for index, item in enumerate(sequence): - # to prevent recursion error + # to prevent recursion error when first two are the same + # put the first into the right sequence first_two_same = (index == 0 and (sequence[0] == sequence[1])) - if item > pivot[0] or first_two_same: - right.append(item) - elif item <= pivot[0]: + if item < pivot[0] or first_two_same: left.append(item) - + elif item >= pivot[0]: + right.append(item) return q_sort(left) + q_sort(right) diff --git a/test_q_sort.py b/test_q_sort.py index 1371c05..342c1cb 100644 --- a/test_q_sort.py +++ b/test_q_sort.py @@ -25,9 +25,22 @@ def test_get_pivot_m(): def test_q_one(): a = [4, 3, 2, 5, 7, 1, 6] a = q_sort(a) - assert a == [1, 2, 3, 4, 5, 6, 7] + assert a == sorted(a) + def test_q_two(): - a = [5, 5, 5, 5] + a = [5, 5, 5, 5, 5, 5, 5, 5, 5] + a = q_sort(a) + assert a == sorted(a) + + +def test_q_mixed(): + a = [1, 2, 5, 4, 4, 6, 7, 9, 8, 0, 0] + a = q_sort(a) + assert a == sorted(a) + + +def test_q_mixed(): + a = [1, 2, 5, 10, 10] a = q_sort(a) - assert a == [5, 5, 5, 5] \ No newline at end of file + assert a == sorted(a) From 90a6faadc01320726821f18dadd657af9ff2936a Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Thu, 2 Apr 2015 12:04:00 -0700 Subject: [PATCH 8/9] added some tests and rewrote if statement to keep track of pivots --- q_sort.py | 18 +++++++++--------- test_q_sort.py | 28 ++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/q_sort.py b/q_sort.py index a19edaf..a72a551 100644 --- a/q_sort.py +++ b/q_sort.py @@ -13,15 +13,15 @@ def q_sort(sequence): print sequence left = [] right = [] + center = [] for index, item in enumerate(sequence): - # to prevent recursion error when first two are the same - # put the first into the right sequence - first_two_same = (index == 0 and (sequence[0] == sequence[1])) - if item < pivot[0] or first_two_same: + if item < pivot: left.append(item) - elif item >= pivot[0]: + elif item > pivot: right.append(item) - return q_sort(left) + q_sort(right) + else: + center.append(item) + return q_sort(left) + center + q_sort(right) def get_pivot(sequence): @@ -31,7 +31,7 @@ def get_pivot(sequence): middle = sequence[mid_index] if last > first > middle or middle > first > last: - return [first] + return first if last > middle > first or first > middle > last: - return [middle] - return [last] + return middle + return last diff --git a/test_q_sort.py b/test_q_sort.py index 342c1cb..ab34ba2 100644 --- a/test_q_sort.py +++ b/test_q_sort.py @@ -3,23 +3,23 @@ def test_get_pivot_f(): a = [2, 1, 3] - assert get_pivot(a) == [2] + assert get_pivot(a) == 2 b = [2, 3, 1] - assert get_pivot(b) == [2] + assert get_pivot(b) == 2 def test_get_pivot_l(): a = [1, 3, 2] - assert get_pivot(a) == [2] + assert get_pivot(a) == 2 b = [3, 1, 2] - assert get_pivot(b) == [2] + assert get_pivot(b) == 2 def test_get_pivot_m(): a = [1, 5, 5, 2, 1, 1, 3] - assert get_pivot(a) == [2] + assert get_pivot(a) == 2 b = [3, 2, 1] - assert get_pivot(b) == [2] + assert get_pivot(b) == 2 def test_q_one(): @@ -35,12 +35,24 @@ def test_q_two(): def test_q_mixed(): - a = [1, 2, 5, 4, 4, 6, 7, 9, 8, 0, 0] + a = [1, 2, 5, 4, 4, 6, 7, 9, 8, 10, 10] a = q_sort(a) assert a == sorted(a) def test_q_mixed(): - a = [1, 2, 5, 10, 10] + a = [5, 4, 4] + a = q_sort(a) + assert a == sorted(a) + + +def test_q_mixed_duplicate_last_values_greater(): + a = [1, 2, 10, 10, 10] + a = q_sort(a) + assert a == sorted(a) + + +def test_q_mixed_duplicate_last_values_lesser(): + a = [1, 2, 5, 0, 0] a = q_sort(a) assert a == sorted(a) From 097ebd3b623e243e27ee1892493425fa6d189e5e Mon Sep 17 00:00:00 2001 From: Henry Grantham Date: Thu, 2 Apr 2015 12:13:02 -0700 Subject: [PATCH 9/9] removed print statements --- q_sort.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/q_sort.py b/q_sort.py index 2dc0a3a..339d8df 100644 --- a/q_sort.py +++ b/q_sort.py @@ -12,8 +12,6 @@ def q_sort(sequence): else: return sequence[::-1] pivot = get_pivot(sequence) - print "pivot: " + str(pivot) - print sequence left = [] right = [] center = []