From 7579dffc8962a3d173a7e6b11f6b87c457cd1c65 Mon Sep 17 00:00:00 2001 From: manish soni Date: Wed, 3 Oct 2018 00:48:17 +0530 Subject: [PATCH 1/2] added heap sort algorithm program in python --- sorting/heap_sort.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 sorting/heap_sort.py diff --git a/sorting/heap_sort.py b/sorting/heap_sort.py new file mode 100644 index 0000000..4c03ac4 --- /dev/null +++ b/sorting/heap_sort.py @@ -0,0 +1,65 @@ +#!usr/bin/python3 +''' +This is a pure python implementation of the heap sort algorithm. + +For doctests run following command: +python -m doctest -v heap_sort.py +or +python3 -m doctest -v heap_sort.py + +For manual testing run: +python heap_sort.py +''' + +from __future__ import print_function + + +def heapify(unsorted, index, heap_size): + largest = index + left_index = 2 * index + 1 + right_index = 2 * index + 2 + if left_index < heap_size and unsorted[left_index] > unsorted[largest]: + largest = left_index + + if right_index < heap_size and unsorted[right_index] > unsorted[largest]: + largest = right_index + + if largest != index: + unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest] + heapify(unsorted, largest, heap_size) + + +def heap_sort(unsorted): + ''' + Pure implementation of the heap sort algorithm in Python + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + + Examples: + >>> heap_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + + >>> heap_sort([]) + [] + + >>> heap_sort([-2, -5, -45]) + [-45, -5, -2] + ''' + n = len(unsorted) + for i in range(n // 2 - 1, -1, -1): + heapify(unsorted, i, n) + for i in range(n - 1, 0, -1): + unsorted[0], unsorted[i] = unsorted[i], unsorted[0] + heapify(unsorted, 0, i) + return unsorted + +if __name__ == '__main__': + try: + raw_input # Python 2 + except NameError: + raw_input = input # Python 3 + + user_input = raw_input('Enter numbers separated by a comma:\n').strip() + unsorted = [int(item) for item in user_input.split(',')] + print(heap_sort(unsorted)) From 2ab1e532b3feaff120d267e56f5c6f865805cec9 Mon Sep 17 00:00:00 2001 From: manish soni Date: Wed, 3 Oct 2018 02:22:31 +0530 Subject: [PATCH 2/2] add ternary_search algorithm --- searches/ternary_search.py | 107 +++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 searches/ternary_search.py diff --git a/searches/ternary_search.py b/searches/ternary_search.py new file mode 100644 index 0000000..c610f9b --- /dev/null +++ b/searches/ternary_search.py @@ -0,0 +1,107 @@ +''' +This is a type of divide and conquer algorithm which divides the search space into +3 parts and finds the target value based on the property of the array or list +(usually monotonic property). + +Time Complexity : O(log3 N) +Space Complexity : O(1) +''' +from __future__ import print_function + +import sys + +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + +# This is the precision for this function which can be altered. +# It is recommended for users to keep this number greater than or equal to 10. +precision = 10 + +# This is the linear search that will occur after the search space has become smaller. +def lin_search(left, right, A, target): + for i in range(left, right+1): + if(A[i] == target): + return i + +# This is the iterative method of the ternary search algorithm. +def ite_ternary_search(A, target): + left = 0 + right = len(A) - 1; + while(True): + if(left pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy