From 9c2ebd063902379066fec50cccf312d4d4c0bacc Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 20:04:41 +0530 Subject: [PATCH 1/5] Update1: Bubble Sort(Removing Extra Iterations) Removed the extra iterations in simple bubble_sort() function, so as the real meaning of bubble sort come out. --- Sorting/1. Bubble_Sort.ipynb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sorting/1. Bubble_Sort.ipynb b/Sorting/1. Bubble_Sort.ipynb index 1728a9f..459c2d0 100644 --- a/Sorting/1. Bubble_Sort.ipynb +++ b/Sorting/1. Bubble_Sort.ipynb @@ -16,8 +16,9 @@ "outputs": [], "source": [ "def bubble_sort(array):\n", - " for i in range(len(array) - 1):\n", - " for j in range(len(array) - 1):\n", + " n=len(array)", + " for i in range(0,n):\n", + " for j in range(0,n-i-1):\n", " if array[j] > array[j + 1]:\n", " array[j], array[j + 1] = array[j + 1], array[j] # swap" ] From 3b5a7a3e4447a1779feb18dedd0c622aa9a3fd6f Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 20:05:14 +0530 Subject: [PATCH 2/5] Update 1. Bubble_Sort.ipynb --- Sorting/1. Bubble_Sort.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorting/1. Bubble_Sort.ipynb b/Sorting/1. Bubble_Sort.ipynb index 459c2d0..f8e6807 100644 --- a/Sorting/1. Bubble_Sort.ipynb +++ b/Sorting/1. Bubble_Sort.ipynb @@ -16,7 +16,7 @@ "outputs": [], "source": [ "def bubble_sort(array):\n", - " n=len(array)", + " n=len(array)\n", " for i in range(0,n):\n", " for j in range(0,n-i-1):\n", " if array[j] > array[j + 1]:\n", From 52c4e2d5c41ad8bfc957c27988be04077c2d1bda Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 20:17:48 +0530 Subject: [PATCH 3/5] Update Quick sort comments Added extra comments in functions, for better understanding. --- Sorting/5. Quick_Sort.ipynb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sorting/5. Quick_Sort.ipynb b/Sorting/5. Quick_Sort.ipynb index 9e2186d..7c2d7a3 100644 --- a/Sorting/5. Quick_Sort.ipynb +++ b/Sorting/5. Quick_Sort.ipynb @@ -14,11 +14,15 @@ "outputs": [], "source": [ "def partition(array, low, high):\n", - " i = low - 1\n", - " pivot = array[high]\n", + " i = low - 1 # index of smaller element\n", + " pivot = array[high] # pivot \n", " \n", " for j in range(low, high):\n", + " # If current element is smaller than the pivot\n", + " \n", " if array[j] < pivot:\n", + " # increment index of smaller element\n" + " \n", " i += 1\n", " array[i], array[j] = array[j], array[i]\n", " \n", @@ -27,8 +31,12 @@ "\n", "def quick_sort(array, low, high):\n", " if low < high:\n", + " # pi is partitioning index, arr[p] is now\n", + " # at right place \n", " temp = partition(array, low, high)\n", " \n", + " # Separately sort elements before\n", + " # partition and after partition \n", " quick_sort(array, low, temp - 1)\n", " quick_sort(array, temp + 1, high)" ] From cacb487a2963594bda91650548edfd69719ce23a Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 20:19:44 +0530 Subject: [PATCH 4/5] Update 5. Quick_Sort.ipynb --- Sorting/5. Quick_Sort.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorting/5. Quick_Sort.ipynb b/Sorting/5. Quick_Sort.ipynb index 7c2d7a3..cc2d444 100644 --- a/Sorting/5. Quick_Sort.ipynb +++ b/Sorting/5. Quick_Sort.ipynb @@ -21,7 +21,7 @@ " # If current element is smaller than the pivot\n", " \n", " if array[j] < pivot:\n", - " # increment index of smaller element\n" + " # increment index of smaller element\n", " \n", " i += 1\n", " array[i], array[j] = array[j], array[i]\n", From 2d7bd639282ef647497de3510c2db2b74be52e21 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <44729252+abhishek-kehsihba@users.noreply.github.com> Date: Tue, 1 Oct 2019 21:48:58 +0530 Subject: [PATCH 5/5] Update P01_Fibonnaci.py Added the recursion+memoization method for Fibonacci sequence. --- Dynamic Programming/P01_Fibonnaci.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Dynamic Programming/P01_Fibonnaci.py b/Dynamic Programming/P01_Fibonnaci.py index 05ea778..53d1745 100644 --- a/Dynamic Programming/P01_Fibonnaci.py +++ b/Dynamic Programming/P01_Fibonnaci.py @@ -19,6 +19,15 @@ def fibonacciRec(number): else: return (fibonacciRec(number - 1) + fibonacciRec(number - 2)) +# improved recursive fibonacci function +def fib_memoization(n, lookup): + if n == 0 or n == 1 : + lookup[n] = n + if lookup[n] is None: + lookup[n] = fib(n-1 , lookup) + fib(n-2 , lookup) + return lookup[n] + + if __name__ == '__main__': userInput = int(input('Enter the number: ')) @@ -37,3 +46,9 @@ def fibonacciRec(number): result = fibonacciRec(userInput) stopTime = time.time() print('Time:', (stopTime - startTime), 'Result:', result) + + startTime = time.time() + lookup=[None]*(101) + result = fib_memoization(userInput,lookup) + stopTime = time.time() + print('Time:', (stopTime - startTime), 'Result:', result)