From bc155dc53c242a8c62efb00a38439e0ed08147de Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 7 Oct 2019 19:06:14 -0700 Subject: [PATCH] Add initial implementation of hw2 --- inversions.py | 39 +++++++++++++++++++++++++++++++++++++++ longest.py | 13 +++++++++++++ msort.py | 26 ++++++++++++++++++++++++++ report.txt | 26 ++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 inversions.py create mode 100644 longest.py create mode 100644 msort.py create mode 100644 report.txt diff --git a/inversions.py b/inversions.py new file mode 100644 index 0000000..c07e8b3 --- /dev/null +++ b/inversions.py @@ -0,0 +1,39 @@ +def _inversions(xs): + # Can't be unsorted + if len(xs) < 2: return 0, xs + + # Divide into two subarrays + leng = len(xs)//2 + left = xs[:leng] + right = xs[leng:] + + # Perform recursive call + lcount, left = _inversions(left) + rcount, right = _inversions(right) + + # Reverse the lists to allow O(1) pop. + # Since we already have an O(n) copy, + # this doesn't increase complexity. + left.reverse() + right.reverse() + + # Merge by popping + count = 0 + total = [] + while left != [] and right != []: + if left[-1] <= right[-1]: + total.append(left.pop()) + else: + total.append(right.pop()) + count += len(left) + + # Add elements from non-empty array, + # if applicable. + left.reverse() + right.reverse() + total += left + right + return (count + lcount + rcount), total + +def inversions(xs): + count, _ = _inversions(xs) + return count diff --git a/longest.py b/longest.py new file mode 100644 index 0000000..3707264 --- /dev/null +++ b/longest.py @@ -0,0 +1,13 @@ +def _longest(ts): + if ts == []: return 0, 0 + + ldepth, lpath = _longest(ts[0]) + rdepth, rpath = _longest(ts[2]) + + depth = max(ldepth, rdepth) + 1 + cpath = max(lpath, rpath) + + return depth, max(ldepth+rdepth, cpath) + +def longest(ts): + return _longest(ts)[1] diff --git a/msort.py b/msort.py new file mode 100644 index 0000000..48da2e4 --- /dev/null +++ b/msort.py @@ -0,0 +1,26 @@ +def msort(xs): + if len(xs) < 2: return xs + + leng = len(xs) // 2 + left = msort(xs[:leng]) + right = msort(xs[leng:]) + + # Reversing is O(n), but so is copying, so + # this does not worsen complexity. + # It does slow us down, but... oh well :) + left.reverse() + right.reverse() + + # Since we're reversed, the smallest numbers + # are on the end. We can thus use pop (O(1)) + total = [] + while left != [] and right != []: + if left[-1] <= right[-1]: + total.append(left.pop()) + else: + total.append(right.pop()) + + left.reverse() + right.reverse() + total += left + right + return total diff --git a/report.txt b/report.txt new file mode 100644 index 0000000..d5edd5e --- /dev/null +++ b/report.txt @@ -0,0 +1,26 @@ +Q0: Which of the following sorting algorithms are (or can be made) stable? +A: mergesort, quicksort with first element as pivot, selection sort with insertion, +and insertion sort are stable. + +Debrief: +1. Approximately how many hours did you spend on this assignment? + + Less than 1 hour. + +2. Would you rate it as easy, moderate, or difficult? + + Seemed easy, I didn't have any difficulties. + +3. Did you work on it mostly alone, or mostly with other people? + Note you are encouraged to discuss with your classmates, + but each students should submit his/her own code. + + Alone. + +4. How deeply do you feel you understand the material it covers (0%–100%)? + + Seems like 100%. Didn't have any difficulties. + +5. Any other comments? + + In my opinion, it kinda sucks that only 1 code assignment is auto-graded.