Skip to content

Commit 1cee5b9

Browse files
kehsihba19OmkarPathak
authored andcommitted
Added some commits in Sort section (#14)
* 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. * Update 1. Bubble_Sort.ipynb * Update Quick sort comments Added extra comments in functions, for better understanding. * Update 5. Quick_Sort.ipynb * Update P01_Fibonnaci.py Added the recursion+memoization method for Fibonacci sequence.
1 parent 48882bc commit 1cee5b9

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Diff for: Dynamic Programming/P01_Fibonnaci.py

+15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ def fibonacciRec(number):
1919
else:
2020
return (fibonacciRec(number - 1) + fibonacciRec(number - 2))
2121

22+
# improved recursive fibonacci function
23+
def fib_memoization(n, lookup):
24+
if n == 0 or n == 1 :
25+
lookup[n] = n
26+
if lookup[n] is None:
27+
lookup[n] = fib(n-1 , lookup) + fib(n-2 , lookup)
28+
return lookup[n]
29+
30+
2231
if __name__ == '__main__':
2332
userInput = int(input('Enter the number: '))
2433

@@ -37,3 +46,9 @@ def fibonacciRec(number):
3746
result = fibonacciRec(userInput)
3847
stopTime = time.time()
3948
print('Time:', (stopTime - startTime), 'Result:', result)
49+
50+
startTime = time.time()
51+
lookup=[None]*(101)
52+
result = fib_memoization(userInput,lookup)
53+
stopTime = time.time()
54+
print('Time:', (stopTime - startTime), 'Result:', result)

Diff for: Sorting/1. Bubble_Sort.ipynb

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
"outputs": [],
1717
"source": [
1818
"def bubble_sort(array):\n",
19-
" for i in range(len(array) - 1):\n",
20-
" for j in range(len(array) - 1):\n",
19+
" n=len(array)\n",
20+
" for i in range(0,n):\n",
21+
" for j in range(0,n-i-1):\n",
2122
" if array[j] > array[j + 1]:\n",
2223
" array[j], array[j + 1] = array[j + 1], array[j] # swap"
2324
]

Diff for: Sorting/5. Quick_Sort.ipynb

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
"outputs": [],
1515
"source": [
1616
"def partition(array, low, high):\n",
17-
" i = low - 1\n",
18-
" pivot = array[high]\n",
17+
" i = low - 1 # index of smaller element\n",
18+
" pivot = array[high] # pivot \n",
1919
" \n",
2020
" for j in range(low, high):\n",
21+
" # If current element is smaller than the pivot\n",
22+
" \n",
2123
" if array[j] < pivot:\n",
24+
" # increment index of smaller element\n",
25+
" \n",
2226
" i += 1\n",
2327
" array[i], array[j] = array[j], array[i]\n",
2428
" \n",
@@ -27,8 +31,12 @@
2731
"\n",
2832
"def quick_sort(array, low, high):\n",
2933
" if low < high:\n",
34+
" # pi is partitioning index, arr[p] is now\n",
35+
" # at right place \n",
3036
" temp = partition(array, low, high)\n",
3137
" \n",
38+
" # Separately sort elements before\n",
39+
" # partition and after partition \n",
3240
" quick_sort(array, low, temp - 1)\n",
3341
" quick_sort(array, temp + 1, high)"
3442
]

0 commit comments

Comments
 (0)