|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 10, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Sorting Via Bubble Sort:\n", |
| 13 | + "Random List= [14, 1, 16, 8, 19, 12, 6, 3, 10, 14, 18, 4, 1, 3, 3, 1, 2, 19, 7, 3]\n", |
| 14 | + "Sorted List= [1, 1, 1, 2, 3, 3, 3, 3, 4, 6, 7, 8, 10, 12, 14, 14, 16, 18, 19, 19]\n", |
| 15 | + "Sorting Via Insertion Sort:\n", |
| 16 | + "Random List= [7, 14, 5, 1, 14, 4, 20, 8, 18, 3, 4, 6, 15, 4, 19, 13, 15, 2, 11, 3]\n", |
| 17 | + "Sorted List= [1, 2, 3, 3, 4, 4, 4, 5, 6, 7, 8, 11, 13, 14, 14, 15, 15, 18, 19, 20]\n", |
| 18 | + "Sorting Via Selection Sort:\n", |
| 19 | + "Sorted List= [[1, 2, 3, 3, 4, 4, 4, 5, 6, 7, 8, 11, 13, 14, 14, 15, 15, 18, 19, 20]]\n" |
| 20 | + ] |
| 21 | + } |
| 22 | + ], |
| 23 | + "source": [ |
| 24 | + "import random\n", |
| 25 | + "class sorting:\n", |
| 26 | + " def __init__(self,lst):\n", |
| 27 | + " self.lst=lst\n", |
| 28 | + " \n", |
| 29 | + "# Generating random numbers to sort them\n", |
| 30 | + " \n", |
| 31 | + " def GenerateRandom(self,n):\n", |
| 32 | + " for i in range(n):\n", |
| 33 | + " x=random.randint(1,n)\n", |
| 34 | + " self.lst.append(x)\n", |
| 35 | + " return self.lst\n", |
| 36 | + " \n", |
| 37 | + "# Bubble Sort \n", |
| 38 | + "\n", |
| 39 | + " def BubbleSort(self):\n", |
| 40 | + " n=len(self.lst)\n", |
| 41 | + " for i in range(n):\n", |
| 42 | + " for j in range(n-1):\n", |
| 43 | + " if self.lst[j]>self.lst[j+1]:\n", |
| 44 | + " (self.lst[j],self.lst[j+1])=(self.lst[j+1],self.lst[j])\n", |
| 45 | + " return self.lst \n", |
| 46 | + "\n", |
| 47 | + "# Insertion Sort\n", |
| 48 | + " \n", |
| 49 | + " def InsertionSort(self):\n", |
| 50 | + " n=len(self.lst)\n", |
| 51 | + " for i in range(1,n):\n", |
| 52 | + " j=i-1\n", |
| 53 | + " x=self.lst[i]\n", |
| 54 | + " while j>=0 and self.lst[j]>x:\n", |
| 55 | + " self.lst[j+1]=self.lst[j]\n", |
| 56 | + " j=j-1\n", |
| 57 | + " self.lst[j+1]=x\n", |
| 58 | + " return self.lst\n", |
| 59 | + " \n", |
| 60 | + "# Selection Sort\n", |
| 61 | + " \n", |
| 62 | + " def SelectionSort(self):\n", |
| 63 | + " List=[]\n", |
| 64 | + " n=len(self.lst)\n", |
| 65 | + " List.append(self.lst)\n", |
| 66 | + " for i in range(n-1):\n", |
| 67 | + " min=i\n", |
| 68 | + " for j in range(i+1,i):\n", |
| 69 | + " if List[j]>List[min]:\n", |
| 70 | + " min=j\n", |
| 71 | + " if min!=i:\n", |
| 72 | + " (List[min],List[i])=(List[i],List[min]) \n", |
| 73 | + " return List\n", |
| 74 | + "\n", |
| 75 | + " \n", |
| 76 | + "# Driver Code\n", |
| 77 | + " \n", |
| 78 | + "print(\"Sorting Via Bubble Sort:\")\n", |
| 79 | + "lst=[]\n", |
| 80 | + "ob=sorting(lst)\n", |
| 81 | + "print(\"Random List=\",ob.GenerateRandom(20))\n", |
| 82 | + "print(\"Sorted List=\",ob.BubbleSort())\n", |
| 83 | + "\n", |
| 84 | + "print(\"Sorting Via Insertion Sort:\")\n", |
| 85 | + "List=[]\n", |
| 86 | + "ob1=sorting(List)\n", |
| 87 | + "print(\"Random List=\",ob1.GenerateRandom(20))\n", |
| 88 | + "print(\"Sorted List=\",ob1.InsertionSort())\n", |
| 89 | + "\n", |
| 90 | + "print(\"Sorting Via Selection Sort:\")\n", |
| 91 | + "print(\"Sorted List=\",ob1.SelectionSort())\n" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": 17, |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [ |
| 99 | + { |
| 100 | + "name": "stdout", |
| 101 | + "output_type": "stream", |
| 102 | + "text": [ |
| 103 | + "The Sorted list is:\n", |
| 104 | + "0\n", |
| 105 | + "1\n", |
| 106 | + "2\n", |
| 107 | + "3\n", |
| 108 | + "4\n", |
| 109 | + "5\n" |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "source": [ |
| 114 | + "# quickSort in ascending order\n", |
| 115 | + "\n", |
| 116 | + "def partition(array,low,high): \n", |
| 117 | + " i = ( low-1 ) \n", |
| 118 | + " y = array[high] \n", |
| 119 | + " for j in range(low , high): \n", |
| 120 | + " if array[j] <= y: \n", |
| 121 | + " i = i+1 \n", |
| 122 | + " array[i],array[j] = array[j],array[i] \n", |
| 123 | + " \n", |
| 124 | + " array[i+1],array[high] = array[high],array[i+1] \n", |
| 125 | + " return ( i+1 ) \n", |
| 126 | + "def quickSort(array,low,high): \n", |
| 127 | + " if low < high: \n", |
| 128 | + " x = partition(array,low,high) \n", |
| 129 | + " quickSort(array, low, x-1) \n", |
| 130 | + " quickSort(array, x+1, high) \n", |
| 131 | + "# Driver code \n", |
| 132 | + "array = [0,5,3,4,1,2] \n", |
| 133 | + "n = len(array) \n", |
| 134 | + "quickSort(array,0,n-1) \n", |
| 135 | + "print (\"The Sorted list is:\") \n", |
| 136 | + "for i in range(n): \n", |
| 137 | + " print (array[i])" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": 16, |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [ |
| 145 | + { |
| 146 | + "name": "stdout", |
| 147 | + "output_type": "stream", |
| 148 | + "text": [ |
| 149 | + "The Sorted list is:\n", |
| 150 | + "5\n", |
| 151 | + "4\n", |
| 152 | + "3\n", |
| 153 | + "2\n", |
| 154 | + "1\n", |
| 155 | + "0\n" |
| 156 | + ] |
| 157 | + } |
| 158 | + ], |
| 159 | + "source": [ |
| 160 | + "# quickSort in descending order\n", |
| 161 | + "\n", |
| 162 | + "def partition(array,low,high): \n", |
| 163 | + " i = ( low-1 ) \n", |
| 164 | + " y = array[high] \n", |
| 165 | + " for j in range(low , high): \n", |
| 166 | + " if array[j] >= y: \n", |
| 167 | + " i = i+1 \n", |
| 168 | + " array[i],array[j] = array[j],array[i] \n", |
| 169 | + " \n", |
| 170 | + " array[i+1],array[high] = array[high],array[i+1] \n", |
| 171 | + " return ( i+1 ) \n", |
| 172 | + "def quickSort(array,low,high): \n", |
| 173 | + " if low < high: \n", |
| 174 | + " x = partition(array,low,high) \n", |
| 175 | + " quickSort(array, low, x-1) \n", |
| 176 | + " quickSort(array, x+1, high) \n", |
| 177 | + "# Driver code \n", |
| 178 | + "array = [0,5,3,4,1,2] \n", |
| 179 | + "n = len(array) \n", |
| 180 | + "quickSort(array,0,n-1) \n", |
| 181 | + "print (\"The Sorted list is:\") \n", |
| 182 | + "for i in range(n): \n", |
| 183 | + " print (array[i])" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "code", |
| 188 | + "execution_count": 15, |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [ |
| 191 | + { |
| 192 | + "name": "stdout", |
| 193 | + "output_type": "stream", |
| 194 | + "text": [ |
| 195 | + "[1, 3, 5, 6, 8]\n" |
| 196 | + ] |
| 197 | + }, |
| 198 | + { |
| 199 | + "data": { |
| 200 | + "text/plain": [ |
| 201 | + "1" |
| 202 | + ] |
| 203 | + }, |
| 204 | + "execution_count": 15, |
| 205 | + "metadata": {}, |
| 206 | + "output_type": "execute_result" |
| 207 | + } |
| 208 | + ], |
| 209 | + "source": [ |
| 210 | + "# Sort & search the index of an element\n", |
| 211 | + "\n", |
| 212 | + "def search(v):\n", |
| 213 | + " List=[1,6,3,5,8]\n", |
| 214 | + " for i in List:\n", |
| 215 | + " if i<=List[0]:\n", |
| 216 | + " n=len(List)\n", |
| 217 | + " for i in range(n):\n", |
| 218 | + " for j in range(n-1):\n", |
| 219 | + " if List[j]>List[j+1]:\n", |
| 220 | + " (List[j],List[j+1])=(List[j+1],List[j])\n", |
| 221 | + " print( List) \n", |
| 222 | + "\n", |
| 223 | + " n=len(List)\n", |
| 224 | + " low=0\n", |
| 225 | + " high=n-1\n", |
| 226 | + " is_item=False\n", |
| 227 | + " while low<=high and not is_item:\n", |
| 228 | + " mid=(low+high)//2\n", |
| 229 | + " \n", |
| 230 | + " if List[mid]==v:\n", |
| 231 | + " is_item=True\n", |
| 232 | + " return mid \n", |
| 233 | + " else: \n", |
| 234 | + " if v>List[mid]:\n", |
| 235 | + " low=mid+1 \n", |
| 236 | + " else:\n", |
| 237 | + " high=mid-1 \n", |
| 238 | + "\n", |
| 239 | + "search(3)" |
| 240 | + ] |
| 241 | + } |
| 242 | + ], |
| 243 | + "metadata": { |
| 244 | + "kernelspec": { |
| 245 | + "display_name": "Python 3", |
| 246 | + "language": "python", |
| 247 | + "name": "python3" |
| 248 | + }, |
| 249 | + "language_info": { |
| 250 | + "codemirror_mode": { |
| 251 | + "name": "ipython", |
| 252 | + "version": 3 |
| 253 | + }, |
| 254 | + "file_extension": ".py", |
| 255 | + "mimetype": "text/x-python", |
| 256 | + "name": "python", |
| 257 | + "nbconvert_exporter": "python", |
| 258 | + "pygments_lexer": "ipython3", |
| 259 | + "version": "3.7.6" |
| 260 | + } |
| 261 | + }, |
| 262 | + "nbformat": 4, |
| 263 | + "nbformat_minor": 2 |
| 264 | +} |
0 commit comments