aboutsummaryrefslogtreecommitdiff
path: root/notebooks/sorting_algorithms.ipynb
blob: f1d81149655b61ff77790dcf23f878bfa59d01bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world\n",
      "[31, 61, 29, 87, 82, 97, 9, 34, 67, 76]\n",
      "['A', 'B', 'C', 'D', 'E']\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'>' not supported between instances of 'str' and 'int'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 73\u001b[0m\n\u001b[1;32m     71\u001b[0m     test_existsness(\u001b[38;5;241m100\u001b[39m)\n\u001b[1;32m     72\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m---> 73\u001b[0m     \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[2], line 61\u001b[0m, in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m     59\u001b[0m query \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mYour query for item: \u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[1;32m     60\u001b[0m qstring \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mString item: \u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 61\u001b[0m \u001b[43mbinsearch\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnarr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mquery\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     62\u001b[0m found1 \u001b[38;5;241m=\u001b[39m linear_search(array, query)\n\u001b[1;32m     63\u001b[0m found2 \u001b[38;5;241m=\u001b[39m linear_search_while(array, query)\n",
      "Cell \u001b[0;32mIn[2], line 18\u001b[0m, in \u001b[0;36mbinsearch\u001b[0;34m(arr, item)\u001b[0m\n\u001b[1;32m     16\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFound here\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m     17\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m found\n\u001b[0;32m---> 18\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43marr\u001b[49m\u001b[43m[\u001b[49m\u001b[43mindex\u001b[49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m>\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mitem\u001b[49m:\n\u001b[1;32m     19\u001b[0m     ubound \u001b[38;5;241m=\u001b[39m index \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[1;32m     20\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m arr[index] \u001b[38;5;241m<\u001b[39m item:\n",
      "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'str' and 'int'"
     ]
    }
   ],
   "source": [
    "import random\n",
    "def generate_test_data(length: int, _range: tuple):\n",
    "    arr = []\n",
    "    for i in range(length):\n",
    "        arr.append(random.randint(int(_range[0]), int(_range[1])))\n",
    "    return arr\n",
    "def binsearch(arr, item):\n",
    "    arr = sorted(arr)\n",
    "    print(arr)\n",
    "    ubound, lbound = len(arr) - 1, 0\n",
    "    found = False\n",
    "    while not found and lbound <= ubound:\n",
    "        index = (lbound + ubound) // 2\n",
    "        if arr[index] == item: \n",
    "            found = True\n",
    "            print(\"Found here\")\n",
    "            return found\n",
    "        if arr[index] > item:\n",
    "            ubound = index - 1\n",
    "        if arr[index] < item:\n",
    "            lbound = index + 1\n",
    "    print(\"not found\")\n",
    "    return found\n",
    "\n",
    "\n",
    "def linear_search_while(arr: list, item):\n",
    "    found: bool = False\n",
    "    index = 0\n",
    "    while not found and index < len(arr):\n",
    "        if (arr[index] == item): \n",
    "            found = True\n",
    "            break\n",
    "        index += 1\n",
    "    return (found, index)\n",
    "def linear_search(arr: list, item):\n",
    "    index = 0\n",
    "    for _item in arr:\n",
    "        if _item == item:\n",
    "            return (True, index) \n",
    "        index += 1\n",
    "    return False\n",
    "def test_existsness(samples: int):\n",
    "    yes = 0\n",
    "    no = 0\n",
    "    for i in range(samples):\n",
    "        query = random.randint(0, 100)\n",
    "        array = generate_test_data(100, (0, 100))\n",
    "        found = linear_search(array, query)\n",
    "        if found:\n",
    "            yes += 1    \n",
    "        else:\n",
    "            no += 1\n",
    "    print(f\"In {samples} samples, {yes} match, {no} don't\")\n",
    "def main():\n",
    "    narr = sorted([\"A\", \"B\", \"C\", \"D\", \"E\"])\n",
    "    print(\"Hello world\")\n",
    "    array = generate_test_data(10, (0, 100))\n",
    "    print(array)\n",
    "    query = int(input(\"Your query for item: \"))\n",
    "    qstring = input(\"String item: \")\n",
    "    binsearch(narr, query)\n",
    "    found1 = linear_search(array, query)\n",
    "    found2 = linear_search_while(array, query)\n",
    "    if found1[0]:\n",
    "        print(\"Found by for loop method \", found1[1]) \n",
    "    else: print(\"Not found.\")\n",
    "    if found2[0]:\n",
    "        print(\"Found by while loop method \", found2[1])\n",
    "    else: \n",
    "        print(\"Not found.\")\n",
    "    test_existsness(100)\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\n",
      "100 checks passed!\n",
      "['mahoylwzsf', 'oqsmmuavgz', 'glrgtvhwur', 'tmlvraonck', 'jutgjynhmg', 'qrbozldpmj', 'wepatxjctj', 'axlymmpbns', 'dieazmvcin', 'ecfxsjbjha']\n",
      "['axlymmpbns', 'dieazmvcin', 'ecfxsjbjha', 'glrgtvhwur', 'jutgjynhmg', 'mahoylwzsf', 'oqsmmuavgz', 'qrbozldpmj', 'tmlvraonck', 'wepatxjctj']\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import string\n",
    "CHARS = list(string.ascii_lowercase)\n",
    "print(CHARS)\n",
    "def generate_test_data(length: int, _range: tuple):\n",
    "    arr = []\n",
    "    for i in range(length):\n",
    "        arr.append(random.randint(_range[0], _range[1]))\n",
    "    return arr\n",
    "def bubsort(arr: list):\n",
    "    for i in range(len(arr)):\n",
    "        # print(arr)\n",
    "        changed = False\n",
    "        for j in range(len(arr) - i - 1):\n",
    "            if arr[j] > arr[j+1]:\n",
    "                swp = arr[j+1]\n",
    "                arr[j+1] = arr[j]\n",
    "                arr[j] = swp\n",
    "                changed = True\n",
    "        if not changed: return arr \n",
    "def testarr_string(length: int):\n",
    "    arr = []\n",
    "    for i in range(length):\n",
    "        string = \"\"\n",
    "        for i in range(10):\n",
    "            string += CHARS[random.randint(0,len(CHARS) - 1)]\n",
    "        arr.append(string)\n",
    "    print(arr)\n",
    "    return arr\n",
    "def bubsort_reliability(samples: int):\n",
    "    y = 0\n",
    "    n = 0\n",
    "    for _ in range(samples):\n",
    "        test  = generate_test_data(100, (0, 100))\n",
    "        if bubsort(test) == sorted(test): y += 1\n",
    "        else: n += 1\n",
    "    if y == samples: print(f\"{samples} checks passed!\")\n",
    "bubsort_reliability(100)\n",
    "print(bubsort(testarr_string(10)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OH NO ITEM NOT FOUND IN LIST!!\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "def generate_test_data(length: int, _range: tuple):\n",
    "    arr = []\n",
    "    for i in range(length):\n",
    "        arr.append(random.randint(_range[0], _range[1]))\n",
    "    return arr\n",
    "def linsearch(myList: list, valueToFind: int):\n",
    "    mindex: int = len(myList) - 1\n",
    "    index: int = 0\n",
    "    found = False\n",
    "    while not found and index <= mindex:\n",
    "        if myList[index] == valueToFind:\n",
    "            found = True\n",
    "        index += 1\n",
    "    if found: print(\"VALUE FOUND!!!\")\n",
    "    else: print(\"OH NO ITEM NOT FOUND IN LIST!!\")\n",
    "def main():\n",
    "    inputFind = int(input(\"Value to find: \"))\n",
    "    linsearch(generate_test_data(10, (0, 1000)), inputFind)\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}