|
1067 | 1067 | "arr = [24, 27, 22, 32, 29, 1, 2, 3, 4, 5, 6, 85, 90, 12, 6, 23, 18, 95, 80, 88, 0, 10, 100, 12, 25, 300, 41]\n",
|
1068 | 1068 | "n = len(arr)\n",
|
1069 | 1069 | "\n",
|
| 1070 | + "# Bubble sort algorithm\n", |
1070 | 1071 | "for i in range(n):\n",
|
1071 |
| - " for j in range(n-1):\n", |
1072 |
| - " if(arr[j] > arr[j+1]):\n", |
| 1072 | + " # Last i elements are already in place\n", |
| 1073 | + " for j in range(n - 1):\n", |
| 1074 | + " # Swap if the element found is greater than the next element\n", |
| 1075 | + " if arr[j] > arr[j + 1]:\n", |
1073 | 1076 | " temp = arr[j]\n",
|
1074 |
| - " arr[j] = arr[j+1]\n", |
1075 |
| - " arr[j+1] = temp\n", |
1076 |
| - " \n", |
| 1077 | + " arr[j] = arr[j + 1]\n", |
| 1078 | + " arr[j + 1] = temp\n", |
1077 | 1079 | "\n",
|
| 1080 | + "# Print the sorted array\n", |
1078 | 1081 | "print(arr, end=\" \")"
|
1079 | 1082 | ]
|
1080 | 1083 | },
|
|
1096 | 1099 | "arr = [24, 27, 22, 32, 29, 1, 2, 3, 4, 5, 6, 85, 90, 12, 6, 23, 18, 95, 80, 88, 0, 10, 100, 12, 25, 300, 41]\n",
|
1097 | 1100 | "n = len(arr)\n",
|
1098 | 1101 | "\n",
|
| 1102 | + "# Insertion sort algorithm\n", |
1099 | 1103 | "for i in range(n):\n",
|
1100 |
| - " key = arr[i]\n", |
| 1104 | + " key = arr[i] # Select the element to be inserted\n", |
1101 | 1105 | " j = i - 1\n",
|
| 1106 | + " # Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position\n", |
1102 | 1107 | " while j >= 0 and key < arr[j]:\n",
|
1103 |
| - " arr[j+1] = arr[j]\n", |
1104 |
| - " j = j-1\n", |
1105 |
| - " arr[j+1] = key\n", |
| 1108 | + " arr[j + 1] = arr[j]\n", |
| 1109 | + " j = j - 1\n", |
| 1110 | + " arr[j + 1] = key # Insert the key at the correct position\n", |
1106 | 1111 | "\n",
|
| 1112 | + "# Print the sorted array\n", |
1107 | 1113 | "for i in range(n):\n",
|
1108 | 1114 | " print(arr[i], end=\" \")"
|
1109 | 1115 | ]
|
1110 | 1116 | },
|
| 1117 | + { |
| 1118 | + "cell_type": "markdown", |
| 1119 | + "metadata": {}, |
| 1120 | + "source": [ |
| 1121 | + "#### Sorting Method 3 - Merge Sort technique\n", |
| 1122 | + "In this, we use divide-and-conquer algorithm that sorts an array by recursively dividing it into two halves (recusion), sorting each half, and then merging the sorted halves back together." |
| 1123 | + ] |
| 1124 | + }, |
| 1125 | + { |
| 1126 | + "cell_type": "code", |
| 1127 | + "execution_count": null, |
| 1128 | + "metadata": {}, |
| 1129 | + "outputs": [], |
| 1130 | + "source": [ |
| 1131 | + "def merge_sort(arr):\n", |
| 1132 | + "\n", |
| 1133 | + " # Step 1: Split the array into two halves\n", |
| 1134 | + " mid = len(arr) // 2\n", |
| 1135 | + " left_half = merge_sort(arr[:mid]) # Recursively sort the left half\n", |
| 1136 | + " right_half = merge_sort(arr[mid:]) # Recursively sort the right half\n", |
| 1137 | + "\n", |
| 1138 | + " # Step 2: Merge the sorted halves\n", |
| 1139 | + " merged = []\n", |
| 1140 | + " i = j = 0 # Pointers for left and right halves\n", |
| 1141 | + "\n", |
| 1142 | + " # Compare elements from both halves and add the smallest to the merged array\n", |
| 1143 | + " while i < len(left_half) and j < len(right_half):\n", |
| 1144 | + " if left_half[i] <= right_half[j]:\n", |
| 1145 | + " merged.append(left_half[i])\n", |
| 1146 | + " i += 1\n", |
| 1147 | + " else:\n", |
| 1148 | + " merged.append(right_half[j])\n", |
| 1149 | + " j += 1\n", |
| 1150 | + "\n", |
| 1151 | + " # Append any remaining elements from the left half\n", |
| 1152 | + " while i < len(left_half):\n", |
| 1153 | + " merged.append(left_half[i])\n", |
| 1154 | + " i += 1\n", |
| 1155 | + "\n", |
| 1156 | + " # Append any remaining elements from the right half\n", |
| 1157 | + " while j < len(right_half):\n", |
| 1158 | + " merged.append(right_half[j])\n", |
| 1159 | + " j += 1\n", |
| 1160 | + "\n", |
| 1161 | + " return merged\n", |
| 1162 | + "\n", |
| 1163 | + "# Example array\n", |
| 1164 | + "arr = [24, 27, 22, 32, 29, 1, 2, 3, 4, 5, 6, 85, 90, 12, 6, 23, 18, 95, 80, 88, 0, 10, 100, 12, 25, 300, 41]\n", |
| 1165 | + "\n", |
| 1166 | + "# Sort the array using the merge_sort function\n", |
| 1167 | + "sorted_arr = merge_sort(arr)\n", |
| 1168 | + "\n", |
| 1169 | + "# Print the sorted array\n", |
| 1170 | + "for num in sorted_arr:\n", |
| 1171 | + " print(num, end=\" \")\n" |
| 1172 | + ] |
| 1173 | + }, |
| 1174 | + { |
| 1175 | + "cell_type": "markdown", |
| 1176 | + "metadata": {}, |
| 1177 | + "source": [ |
| 1178 | + "#### Sorting Method 4 - Quick Sort technique\n", |
| 1179 | + "In this, we use divide-and-conquer algorithm that sorts an array by recursively dividing it into partitions, sorting each partitions." |
| 1180 | + ] |
| 1181 | + }, |
| 1182 | + { |
| 1183 | + "cell_type": "code", |
| 1184 | + "execution_count": null, |
| 1185 | + "metadata": {}, |
| 1186 | + "outputs": [], |
| 1187 | + "source": [ |
| 1188 | + "def quicksort_inplace(arr, low, high):\n", |
| 1189 | + " # Base case: if the array has one or zero elements, it is already sorted\n", |
| 1190 | + " if low < high:\n", |
| 1191 | + " # Partition the array and get the pivot index\n", |
| 1192 | + " pi = partition(arr, low, high)\n", |
| 1193 | + " # Recursively apply quicksort to the left sub-array\n", |
| 1194 | + " quicksort_inplace(arr, low, pi - 1)\n", |
| 1195 | + " # Recursively apply quicksort to the right sub-array\n", |
| 1196 | + " quicksort_inplace(arr, pi + 1, high)\n", |
| 1197 | + "\n", |
| 1198 | + "def partition(arr, low, high):\n", |
| 1199 | + " pivot = arr[high] # Choose the last element as the pivot\n", |
| 1200 | + " i = low - 1 # Index of the smaller element\n", |
| 1201 | + " for j in range(low, high):\n", |
| 1202 | + " # If the current element is smaller than the pivot\n", |
| 1203 | + " if arr[j] < pivot:\n", |
| 1204 | + " i += 1 # Increment the index of the smaller element\n", |
| 1205 | + " arr[i], arr[j] = arr[j], arr[i] # Swap the elements\n", |
| 1206 | + " # Place the pivot in its correct position\n", |
| 1207 | + " arr[i + 1], arr[high] = arr[high], arr[i + 1]\n", |
| 1208 | + " return i + 1 # Return the pivot index\n", |
| 1209 | + "\n", |
| 1210 | + "\n", |
| 1211 | + "arr = [24, 27, 22, 32, 29, 1, 2, 3, 4, 5, 6, 85, 90, 12, 6, 23, 18, 95, 80, 88, 0, 10, 100, 12, 25, 300, 41]\n", |
| 1212 | + "quicksort_inplace(arr, 0, len(arr) - 1)\n", |
| 1213 | + "\n", |
| 1214 | + "print(arr) # Print the sorted array" |
| 1215 | + ] |
| 1216 | + }, |
| 1217 | + { |
| 1218 | + "cell_type": "markdown", |
| 1219 | + "metadata": {}, |
| 1220 | + "source": [ |
| 1221 | + "#### Sorting Method 5 - Shell Sort technique\n", |
| 1222 | + "It is an extension of insertion sort that compares elements that are far apart and reduce the gap gradually to 1." |
| 1223 | + ] |
| 1224 | + }, |
| 1225 | + { |
| 1226 | + "cell_type": "code", |
| 1227 | + "execution_count": null, |
| 1228 | + "metadata": {}, |
| 1229 | + "outputs": [], |
| 1230 | + "source": [ |
| 1231 | + "arr = [24, 27, 22, 32, 29, 1, 2, 3, 4, 5, 6, 85, 90, 12, 6, 23, 18, 95, 80, 88, 0, 10, 100, 12, 25, 300, 41]\n", |
| 1232 | + "\n", |
| 1233 | + "n = len(arr)\n", |
| 1234 | + "gap = n // 2 # Initialize the gap\n", |
| 1235 | + "\n", |
| 1236 | + "# Start with a large gap, then reduce the gap\n", |
| 1237 | + "while gap > 0:\n", |
| 1238 | + " # Perform a gapped insertion sort for this gap size\n", |
| 1239 | + " for i in range(gap, n):\n", |
| 1240 | + " temp = arr[i]\n", |
| 1241 | + " j = i\n", |
| 1242 | + " \n", |
| 1243 | + " # Shift earlier gap-sorted elements up until the correct location for arr[i] is found\n", |
| 1244 | + " while j >= gap and arr[j - gap] > temp:\n", |
| 1245 | + " arr[j] = arr[j - gap]\n", |
| 1246 | + " j -= gap\n", |
| 1247 | + " arr[j] = temp\n", |
| 1248 | + " gap //= 2 # Reduce the gap for the next iteration\n", |
| 1249 | + "\n", |
| 1250 | + "\n", |
| 1251 | + "print(arr) # Print the sorted array" |
| 1252 | + ] |
| 1253 | + }, |
| 1254 | + { |
| 1255 | + "cell_type": "markdown", |
| 1256 | + "metadata": {}, |
| 1257 | + "source": [ |
| 1258 | + "#### Sorting Method 6 - Selection Sort technique\n", |
| 1259 | + "In this, we use repeatedly select the smallest element and move it to the front." |
| 1260 | + ] |
| 1261 | + }, |
| 1262 | + { |
| 1263 | + "cell_type": "code", |
| 1264 | + "execution_count": null, |
| 1265 | + "metadata": {}, |
| 1266 | + "outputs": [], |
| 1267 | + "source": [ |
| 1268 | + "arr = [24, 27, 22, 32, 29, 1, 2, 3, 4, 5, 6, 85, 90, 12, 6, 23, 18, 95, 80, 88, 0, 10, 100, 12, 25, 300, 41]\n", |
| 1269 | + "\n", |
| 1270 | + "n = len(arr)\n", |
| 1271 | + "\n", |
| 1272 | + "# Traverse through all array elements\n", |
| 1273 | + "for i in range(n):\n", |
| 1274 | + " # Find the minimum element in the remaining unsorted array\n", |
| 1275 | + " min_idx = i\n", |
| 1276 | + " for j in range(i + 1, n):\n", |
| 1277 | + " if arr[j] < arr[min_idx]:\n", |
| 1278 | + " min_idx = j\n", |
| 1279 | + " \n", |
| 1280 | + " # Swap the found minimum element with the first unsorted element\n", |
| 1281 | + " arr[i], arr[min_idx] = arr[min_idx], arr[i]\n", |
| 1282 | + "\n", |
| 1283 | + "print(arr) # Print the sorted array" |
| 1284 | + ] |
| 1285 | + }, |
| 1286 | + { |
| 1287 | + "cell_type": "markdown", |
| 1288 | + "metadata": {}, |
| 1289 | + "source": [ |
| 1290 | + "### Sorting Method 7 - Heap Sort technique\n", |
| 1291 | + "Heapsort is a comparison-based sorting algorithm that uses a binary heap data structure." |
| 1292 | + ] |
| 1293 | + }, |
| 1294 | + { |
| 1295 | + "cell_type": "code", |
| 1296 | + "execution_count": null, |
| 1297 | + "metadata": {}, |
| 1298 | + "outputs": [], |
| 1299 | + "source": [ |
| 1300 | + "def heapify(arr, n, i):\n", |
| 1301 | + " largest = i # Initialize largest as root\n", |
| 1302 | + " left = 2 * i + 1 # Left child\n", |
| 1303 | + " right = 2 * i + 2 # Right child\n", |
| 1304 | + "\n", |
| 1305 | + " # If left child is larger than root\n", |
| 1306 | + " if left < n and arr[left] > arr[largest]:\n", |
| 1307 | + " largest = left\n", |
| 1308 | + "\n", |
| 1309 | + " # If right child is larger than largest so far\n", |
| 1310 | + " if right < n and arr[right] > arr[largest]:\n", |
| 1311 | + " largest = right\n", |
| 1312 | + "\n", |
| 1313 | + " # If largest is not root\n", |
| 1314 | + " if largest != i:\n", |
| 1315 | + " arr[i], arr[largest] = arr[largest], arr[i] # Swap\n", |
| 1316 | + " # Recursively heapify the affected sub-tree\n", |
| 1317 | + " heapify(arr, n, largest)\n", |
| 1318 | + "\n", |
| 1319 | + "def heapsort(arr):\n", |
| 1320 | + " n = len(arr)\n", |
| 1321 | + "\n", |
| 1322 | + " # Build a max heap\n", |
| 1323 | + " for i in range(n // 2 - 1, -1, -1):\n", |
| 1324 | + " heapify(arr, n, i)\n", |
| 1325 | + "\n", |
| 1326 | + " # One by one extract elements\n", |
| 1327 | + " for i in range(n - 1, 0, -1):\n", |
| 1328 | + " arr[i], arr[0] = arr[0], arr[i] # Swap\n", |
| 1329 | + " heapify(arr, i, 0)\n", |
| 1330 | + "\n", |
| 1331 | + "# Example usage\n", |
| 1332 | + "arr = [24, 27, 22, 32, 29, 1, 2, 3, 4, 5, 6, 85, 90, 12, 6, 23, 18, 95, 80, 88, 0, 10, 100, 12, 25, 300, 41]\n", |
| 1333 | + "heapsort(arr)\n", |
| 1334 | + "print(arr) # Print the sorted array" |
| 1335 | + ] |
| 1336 | + }, |
1111 | 1337 | {
|
1112 | 1338 | "cell_type": "markdown",
|
1113 | 1339 | "metadata": {},
|
|
0 commit comments