Skip to content

Commit 49103df

Browse files
authored
Create solution2.ipynb
1 parent 340b2c2 commit 49103df

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

notebook/solution2.ipynb

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "27b09e13",
6+
"metadata": {},
7+
"source": [
8+
"# Algorithm Optimization problems"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "425bc2b8",
14+
"metadata": {},
15+
"source": [
16+
"## Exercise 2 : QuickSort algorithm\n",
17+
"\n",
18+
"Quicksort is a naturally recursive algorithm - divide the input array into smaller arrays, move the elements to the proper side of the pivot, and repeat. \n",
19+
"\n",
20+
"When we first call the algorithm, we consider all of the elements - from indexes **0** to **n-1** where **n** is the number of elements in our array. If our pivot ended up in position **k**, we'd then repeat the process for elements from **0 to k-1** and from **k+1** to **n-1**.\n",
21+
"\n",
22+
"This time we are going to order an array by using two functions - partition() and quick_sort(). The quick_sort() function will first partition() the collection and then recursively call itself on the divided parts.\n",
23+
"\n",
24+
"The code for the partition() function has been provided to you in the following cell. Use this partition() function to implement the quick_sort() function in order to sort the following array:\n",
25+
"\n",
26+
"array = [29,99,23,41,66,28,44,78,87,19,31,76,58,88,82,97,12,21,44]"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": null,
32+
"id": "e32eceef",
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"def partition(array, start, end):\n",
37+
" pivot = array[start]\n",
38+
" low = start + 1\n",
39+
" high = end\n",
40+
"\n",
41+
" while True:\n",
42+
" # If the current value we're looking at is larger than the pivot\n",
43+
" # it's in the right place (right side of pivot) and we can move left,\n",
44+
" # to the next element.\n",
45+
" # We also need to make sure we haven't surpassed the low pointer, since that\n",
46+
" # indicates we have already moved all the elements to their correct side of the pivot\n",
47+
" while low <= high and array[high] >= pivot:\n",
48+
" high = high - 1\n",
49+
"\n",
50+
" # Opposite process of the one above\n",
51+
" while low <= high and array[low] <= pivot:\n",
52+
" low = low + 1\n",
53+
"\n",
54+
" # We either found a value for both high and low that is out of order\n",
55+
" # or low is higher than high, in which case we exit the loop\n",
56+
" if low <= high:\n",
57+
" array[low], array[high] = array[high], array[low]\n",
58+
" # The loop continues\n",
59+
" else:\n",
60+
" # We exit out of the loop\n",
61+
" break\n",
62+
"\n",
63+
" array[start], array[high] = array[high], array[start]\n",
64+
"\n",
65+
" return high"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"id": "c0c22449",
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"def quick_sort(array, start, end):\n",
76+
" #COMPLETE HERE THE CODE OF THE quick_sort() ARRAY\n",
77+
" if start >= end:\n",
78+
" return\n",
79+
"\n",
80+
" p = partition(array, start, end)\n",
81+
" quick_sort(array, start, p-1)\n",
82+
" quick_sort(array, p+1, end)"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"id": "e6cd4c0e",
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"#RUN YOUR quick_sort() FUNCTION ON THE GIVEN ARRAY AND PRINT THE RESULT (SORTED ARRAY).\n",
93+
"\n",
94+
"array = [29,99,23,41,66,28,44,78,87,19,31,76,58,88,82,97,12,21,44]\n",
95+
"\n",
96+
"quick_sort(array, 0, len(array) - 1)\n",
97+
"print(array)"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"id": "b281af6e",
103+
"metadata": {},
104+
"source": [
105+
"Source:\n",
106+
"\n",
107+
"https://www.geeksforgeeks.org/\n",
108+
"\n",
109+
"https://stackabuse.com/big-o-notation-and-algorithm-analysis-with-python-examples/\n",
110+
"\n",
111+
"https://stackabuse.com/quicksort-in-python/\n",
112+
"\n",
113+
"https://stackabuse.com/k-nearest-neighbors-algorithm-in-python-and-scikit-learn/"
114+
]
115+
}
116+
],
117+
"metadata": {
118+
"interpreter": {
119+
"hash": "9248718ffe6ce6938b217e69dbcc175ea21f4c6b28a317e96c05334edae734bb"
120+
},
121+
"kernelspec": {
122+
"display_name": "Python 3.9.12 ('ML-BOOTCAMP')",
123+
"language": "python",
124+
"name": "python3"
125+
},
126+
"language_info": {
127+
"codemirror_mode": {
128+
"name": "ipython",
129+
"version": 3
130+
},
131+
"file_extension": ".py",
132+
"mimetype": "text/x-python",
133+
"name": "python",
134+
"nbconvert_exporter": "python",
135+
"pygments_lexer": "ipython3",
136+
"version": "3.9.12"
137+
}
138+
},
139+
"nbformat": 4,
140+
"nbformat_minor": 5
141+
}

0 commit comments

Comments
 (0)