From ce7faa5a3ae4824ebc89bf7b40b715cb4c898999 Mon Sep 17 00:00:00 2001 From: anubhav-sharma13 <45630457+anubhav-sharma13@users.noreply.github.com> Date: Tue, 22 Oct 2019 13:00:11 +0530 Subject: [PATCH] Largest subarray sum (#1404) * Insertion_sort * largest subarray sum * updated print command * removed extraspaces * removed sys.maxint * added explaination * Updated function style * Update largest_subarray_sum.py * Update i_sort.py * Delete bogo_bogo_sort.py --- other/largest_subarray_sum.py | 23 +++++++++++++++ sorts/bogo_bogo_sort.py | 54 ----------------------------------- sorts/i_sort.py | 21 ++++++++++++++ 3 files changed, 44 insertions(+), 54 deletions(-) create mode 100644 other/largest_subarray_sum.py delete mode 100644 sorts/bogo_bogo_sort.py create mode 100644 sorts/i_sort.py diff --git a/other/largest_subarray_sum.py b/other/largest_subarray_sum.py new file mode 100644 index 000000000..0449e72e6 --- /dev/null +++ b/other/largest_subarray_sum.py @@ -0,0 +1,23 @@ +from sys import maxsize + + +def max_sub_array_sum(a: list, size: int = 0): + """ + >>> max_sub_array_sum([-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7]) + -3 + """ + size = size or len(a) + max_so_far = -maxsize - 1 + max_ending_here = 0 + for i in range(0, size): + max_ending_here = max_ending_here + a[i] + if max_so_far < max_ending_here: + max_so_far = max_ending_here + if max_ending_here < 0: + max_ending_here = 0 + return max_so_far + + +if __name__ == "__main__": + a = [-13, -3, -25, -20, 1, -16, -23, -12, -5, -22, -15, -4, -7] + print(("Maximum contiguous sum is", max_sub_array_sum(a, len(a)))) diff --git a/sorts/bogo_bogo_sort.py b/sorts/bogo_bogo_sort.py deleted file mode 100644 index f26a46e78..000000000 --- a/sorts/bogo_bogo_sort.py +++ /dev/null @@ -1,54 +0,0 @@ -""" -Python implementation of bogobogosort, a "sorting algorithm -designed not to succeed before the heat death of the universe -on any sizable list" - https://en.wikipedia.org/wiki/Bogosort. - -Author: WilliamHYZhang -""" - -import random - - -def bogo_bogo_sort(collection): - """ - returns the collection sorted in ascending order - :param collection: list of comparable items - :return: the list sorted in ascending order - - Examples: - >>> bogo_bogo_sort([0, 5, 3, 2, 2]) - [0, 2, 2, 3, 5] - >>> bogo_bogo_sort([-2, -5, -45]) - [-45, -5, -2] - >>> bogo_bogo_sort([420, 69]) - [69, 420] - """ - - def is_sorted(collection): - if len(collection) == 1: - return True - - clone = collection.copy() - while True: - random.shuffle(clone) - ordered = bogo_bogo_sort(clone[:-1]) - if clone[len(clone) - 1] >= max(ordered): - break - - for i in range(len(ordered)): - clone[i] = ordered[i] - - for i in range(len(collection)): - if clone[i] != collection[i]: - return False - return True - - while not is_sorted(collection): - random.shuffle(collection) - return collection - - -if __name__ == "__main__": - user_input = input("Enter numbers separated by a comma:\n").strip() - unsorted = [int(item) for item in user_input.split(",")] - print(bogo_bogo_sort(unsorted)) diff --git a/sorts/i_sort.py b/sorts/i_sort.py new file mode 100644 index 000000000..f6100a8d0 --- /dev/null +++ b/sorts/i_sort.py @@ -0,0 +1,21 @@ +def insertionSort(arr): + """ + >>> a = arr[:] + >>> insertionSort(a) + >>> a == sorted(a) + True + """ + for i in range(1, len(arr)): + key = arr[i] + j = i - 1 + while j >= 0 and key < arr[j]: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + + +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +print("Sorted array is:") +for i in range(len(arr)): + print("%d" % arr[i])