|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# 第三章代码" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## 算法3.1: MergeSort" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 11, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "# author: Zeqi Zhu ([email protected])\n", |
| 24 | + "\n", |
| 25 | + "def merge_sort(A, l, r):\n", |
| 26 | + " if l < r:\n", |
| 27 | + " # Same as (l+r)//2, but avoids overflow for large l and r\n", |
| 28 | + " mid = l + (r - l) // 2\n", |
| 29 | + "\n", |
| 30 | + " # Sort first and second halves\n", |
| 31 | + " merge_sort(A, l, mid)\n", |
| 32 | + " merge_sort(A, mid + 1, r)\n", |
| 33 | + " merge(A, l, mid, r)\n", |
| 34 | + "\n", |
| 35 | + " return A" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "## 算法3.2: Merge " |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 10, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [ |
| 50 | + { |
| 51 | + "name": "stdout", |
| 52 | + "output_type": "stream", |
| 53 | + "text": [ |
| 54 | + "[5, 6, 7, 11, 12, 12, 13]\n" |
| 55 | + ] |
| 56 | + } |
| 57 | + ], |
| 58 | + "source": [ |
| 59 | + "def merge(arr, l, m, r):\n", |
| 60 | + " n1 = m - l + 1\n", |
| 61 | + " n2 = r - m\n", |
| 62 | + " B = [0] * (n1 + n2) # Temporary array B\n", |
| 63 | + "\n", |
| 64 | + " i, j, k = l, m + 1, 0 # Initial indexes\n", |
| 65 | + "\n", |
| 66 | + " # Merge the subarrays into B\n", |
| 67 | + " while i <= m and j <= r:\n", |
| 68 | + " if arr[i] <= arr[j]:\n", |
| 69 | + " B[k] = arr[i]\n", |
| 70 | + " i += 1\n", |
| 71 | + " else:\n", |
| 72 | + " B[k] = arr[j]\n", |
| 73 | + " j += 1\n", |
| 74 | + " k += 1\n", |
| 75 | + "\n", |
| 76 | + " # Copy the remaining elements of left subarray, if there are any\n", |
| 77 | + " while i <= m:\n", |
| 78 | + " B[k] = arr[i]\n", |
| 79 | + " i += 1\n", |
| 80 | + " k += 1\n", |
| 81 | + "\n", |
| 82 | + " # Copy the remaining elements of right subarray, if there are any\n", |
| 83 | + " while j <= r:\n", |
| 84 | + " B[k] = arr[j]\n", |
| 85 | + " j += 1\n", |
| 86 | + " k += 1\n", |
| 87 | + "\n", |
| 88 | + " # Copy the merged elements back into original array arr\n", |
| 89 | + " for i in range(l, r + 1):\n", |
| 90 | + " arr[i] = B[i - l]\n", |
| 91 | + "\n", |
| 92 | + " return arr\n", |
| 93 | + "\n", |
| 94 | + "# Example usage:\n", |
| 95 | + "example_array = [12, 12, 11, 13, 5, 6, 7]\n", |
| 96 | + "sorted_array = merge_sort(example_array, 0, len(example_array) - 1)\n", |
| 97 | + "print(sorted_array) # Output: [5, 6, 7, 11, 12, 12, 13]" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "## 算法3.3: Inversion-BruteForce" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 3, |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [ |
| 112 | + { |
| 113 | + "name": "stdout", |
| 114 | + "output_type": "stream", |
| 115 | + "text": [ |
| 116 | + "3\n" |
| 117 | + ] |
| 118 | + } |
| 119 | + ], |
| 120 | + "source": [ |
| 121 | + "def count_inversions_bruteforce(arr):\n", |
| 122 | + " n = len(arr)\n", |
| 123 | + " sum = 0\n", |
| 124 | + " for i in range(n):\n", |
| 125 | + " for j in range(i + 1, n):\n", |
| 126 | + " if arr[i] > arr[j]:\n", |
| 127 | + " sum += 1\n", |
| 128 | + " return sum\n", |
| 129 | + "\n", |
| 130 | + "# Example usage:\n", |
| 131 | + "example_array = [2, 4, 1, 3, 5]\n", |
| 132 | + "print(count_inversions_bruteforce(example_array)) # Output: 3" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "## 算法3.4: MergeCount " |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": 4, |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "def merge_count(arr, l, mid, r):\n", |
| 149 | + " i = l\n", |
| 150 | + " j = mid + 1\n", |
| 151 | + " k = 0\n", |
| 152 | + " inv_count = 0 # This will hold the count of inversions\n", |
| 153 | + " B = [0] * (r - l + 1) # Temporary array B\n", |
| 154 | + " \n", |
| 155 | + " # Merge the subarrays and count inversions\n", |
| 156 | + " while i <= mid and j <= r:\n", |
| 157 | + " if arr[i] > arr[j]:\n", |
| 158 | + " B[k] = arr[j]\n", |
| 159 | + " # The number of inversions is the number of elements remaining in the left subarray\n", |
| 160 | + " inv_count += (mid - i + 1)\n", |
| 161 | + " k += 1\n", |
| 162 | + " j += 1\n", |
| 163 | + " else:\n", |
| 164 | + " B[k] = arr[i]\n", |
| 165 | + " k += 1\n", |
| 166 | + " i += 1\n", |
| 167 | + " \n", |
| 168 | + " # Copy the remaining elements of left subarray, if there are any\n", |
| 169 | + " while i <= mid:\n", |
| 170 | + " B[k] = arr[i]\n", |
| 171 | + " i += 1\n", |
| 172 | + " k += 1\n", |
| 173 | + " \n", |
| 174 | + " # Copy the remaining elements of right subarray, if there are any\n", |
| 175 | + " while j <= r:\n", |
| 176 | + " B[k] = arr[j]\n", |
| 177 | + " j += 1\n", |
| 178 | + " k += 1\n", |
| 179 | + " \n", |
| 180 | + " # Copy the merged elements back into original array arr\n", |
| 181 | + " for i in range(0, k):\n", |
| 182 | + " arr[l + i] = B[i]\n", |
| 183 | + " \n", |
| 184 | + " return inv_count\n" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "markdown", |
| 189 | + "metadata": {}, |
| 190 | + "source": [ |
| 191 | + "## 算法3.5: CountInver" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": 6, |
| 197 | + "metadata": {}, |
| 198 | + "outputs": [ |
| 199 | + { |
| 200 | + "name": "stdout", |
| 201 | + "output_type": "stream", |
| 202 | + "text": [ |
| 203 | + "(45, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])\n" |
| 204 | + ] |
| 205 | + } |
| 206 | + ], |
| 207 | + "source": [ |
| 208 | + "def count_inver(arr, l, r):\n", |
| 209 | + " if l >= r:\n", |
| 210 | + " return 0, arr[l:r+1]\n", |
| 211 | + " \n", |
| 212 | + " mid = (l + r) // 2\n", |
| 213 | + " s1, left_half = count_inver(arr, l, mid)\n", |
| 214 | + " s2, right_half = count_inver(arr, mid + 1, r)\n", |
| 215 | + " s3 = merge_count(arr, l, mid, r) # merge_count function from previous implementation\n", |
| 216 | + " total = s1 + s2 + s3\n", |
| 217 | + " \n", |
| 218 | + " return total, arr[l:r+1]\n", |
| 219 | + "\n", |
| 220 | + "\n", |
| 221 | + "# Example usage:\n", |
| 222 | + "example_array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n", |
| 223 | + "print(count_inver(example_array, 0, len(example_array) - 1)) # Output: 45\n", |
| 224 | + "\n" |
| 225 | + ] |
| 226 | + } |
| 227 | + ], |
| 228 | + "metadata": { |
| 229 | + "kernelspec": { |
| 230 | + "display_name": "py38_torch", |
| 231 | + "language": "python", |
| 232 | + "name": "python3" |
| 233 | + }, |
| 234 | + "language_info": { |
| 235 | + "codemirror_mode": { |
| 236 | + "name": "ipython", |
| 237 | + "version": 3 |
| 238 | + }, |
| 239 | + "file_extension": ".py", |
| 240 | + "mimetype": "text/x-python", |
| 241 | + "name": "python", |
| 242 | + "nbconvert_exporter": "python", |
| 243 | + "pygments_lexer": "ipython3", |
| 244 | + "version": "3.8.18" |
| 245 | + } |
| 246 | + }, |
| 247 | + "nbformat": 4, |
| 248 | + "nbformat_minor": 2 |
| 249 | +} |
0 commit comments