From 8a7579f2f57a16a592828974c02b590f5346512c Mon Sep 17 00:00:00 2001 From: jrtechs Date: Fri, 12 Oct 2018 17:12:44 -0400 Subject: [PATCH] Added file which has two ways for doing quick sort. --- other/hasSum.py | 49 ++++++++++++++++++++++++++++++++++ recurrences.py | 0 sorting/quickSort.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 other/hasSum.py create mode 100644 recurrences.py create mode 100644 sorting/quickSort.py diff --git a/other/hasSum.py b/other/hasSum.py new file mode 100644 index 0000000..eed4dcf --- /dev/null +++ b/other/hasSum.py @@ -0,0 +1,49 @@ +def sortedHasSum(s, x): + tempSum = 0 + for i in range(len(s) -1, -1, -1): + if tempSum + s[i] <= x: + tempSum += s[i] + if(tempSum == x): + return True + return False + + +def hasSumHelper(s, x): + if len(s) == 1: + if x - s[0] >= 0: + return x - s[0] + else: + return x + elif len(s) == 0: + return x; + lower,equal,upper = partition(s) + + leftOver = hasSumHelper(upper, x) + + if len(equal) > 1: + leftOver = hasSumHelper([equal[0]], leftOver) + leftOver = hasSumHelper(equal[1::],leftOver) + else: + leftOver = hasSumHelper(upper, leftOver) + + return hasSumHelper(lower, leftOver) + + +def hasSum(s, x): + return hasSumHelper(s, x) == 0 + + +def partition(data): + pivot = len(data) -1 + lower, equal, upper = [], [], [] + + for i in range(0, len(data), 1): + if data[i] > data[pivot]: + upper.append(data[i]) + elif data[i] == data[pivot]: + equal.append(data[i]) + else: + lower.append(data[i]) + + return lower, equal, upper + diff --git a/recurrences.py b/recurrences.py new file mode 100644 index 0000000..e69de29 diff --git a/sorting/quickSort.py b/sorting/quickSort.py new file mode 100644 index 0000000..9ed48cc --- /dev/null +++ b/sorting/quickSort.py @@ -0,0 +1,63 @@ +""" +Jeffery Russell +10-6-18 + +File Containing Variations of Quick Sort +""" + + +def partition(data): + """ + Partitions a list of data into three sections + which are lower, equal, and equal to the pivot + which is selected to be the last element in the + list. + """ + pivot = len(data) -1 + lower, equal, upper = [], [], [] + + for i in range(0, len(data), 1): + if data[i] > data[pivot]: + upper.append(data[i]) + elif data[i] == data[pivot]: + equal.append(data[i]) + else: + lower.append(data[i]) + + return lower, equal, upper + + +def quickSortNormal(data): + """ + This is the traditional implementation of quick sort + where there are two recursive calls. + """ + if len(data) == 0: + return [] + else: + less, equal, greater = partition(data) + return quickSortNormal(less) + equal + quickSortNormal(greater) + + +def quick_sort_accumulation(data, a): + """ + Implementation of quickSort which forces tail recursion + by wrapping the second recursive in the tail positioned + recursive call and added an accumulation variable. + """ + if len(data) == 0: + return a + less, equal, greater = partition(data) + return quick_sort_accumulation(less, + equal + quick_sort_accumulation(greater, a)) + + +def quicksort(data): + """ + Wrapper function for quick sort accumulation. + """ + return quick_sort_accumulation(data, []) + + + +print quicksort([1,3,1,5,7,9,2,3,5,5,6])