Binary Representation, Matrices, Algorithms, and Sorting

Binary Repartition

Binary Representation:

  • Signed Magnitude: The first bit determines if the number is negative (the sign bit). The rest of the bits represent the magnitude (mantissa).
  • One’s Complement: Similar to signed magnitude, but if the number is negative, all bits in the magnitude are inverted.
  • Two’s Complement: Similar to one’s complement, but 1 is added to the inverted magnitude if the number is negative.
  • Biased Representation: Similar to signed magnitude, but a bias is added to the target number.

IE754 Binary Representation

Bz2qmnqVtPZ54_rJCrK7qtgdgcPa0v9fyD_EeSHt428cuAvSa9wkpT1_9E2LqLr4MzE2Q_LFB6AttGxQl6yN3hJcL3SdSxnRw3jwTxIFzstFcxxzkx2NDmCH0Y_AyweKp6r3BatwBXmoXXsoy79F0Q

fGqsf3sdcM5yvIzDR3-qg-9tsz3n7b8XMyzSezWejuLjf9zwGbZ2p8KhuaNzn0EdhA_kX7bwexkgw6onOufkBcZu2OTw5qi1nq9j3W1ydziJ1JZVkezDHe-ziOYjB7qet-Dc4mXCmAWPxpu8S9dIfw

To calculate the bias, determine the number of exponent bits, subtract 1, and raise 2 to that power. Then, subtract 1 from the result. This is your bias.

Matrices

AD_4nXddO6h-XTpxhn8b2hs0UU0FGAGeA0bhrMosm8eKJM1DzPIDWNJD9vuhSaqplGBygwVTmO2_i_3ZqA8DzPN-XvL1tPdHrHOILXV62ot59IfIBYSLeXfbvGy91BPuSB4f-_OeotfiA2UAtyZpDW9oJ8QVMO0?key=wEBFuEIwUEivB28ZJRgeXg

When multiplying matrices, for the first resulting element, you only multiply from the first row of the first matrix and the first column of the second matrix. This is why the entire top row in the example uses 2 and 4 for multiplication.

To check if two matrices can be multiplied, the number of columns in the first matrix must equal the number of rows in the second matrix. In the example, the first matrix is 3×2, and the second is 2×4. Since both have a ‘2’, they can be multiplied.

For matrix addition, the matrices must have the exact same dimensions. You can verify the result by observing how the corresponding elements are added, going down the rows and then along the columns.

Reductions

Reduction involves breaking down an algorithm into two connected parts, a smaller one (B) and a larger one (A).

D8ZAAAAABJRU5ErkJggg==

Algorithm A is broken down into A and B, with A being the larger one. The correctness of B is checked by seeing if A works. If A doesn’t work, then B cannot work.

Halting Problem

The halting problem occurs when two algorithms rely on each other to terminate. Algorithm A needs algorithm B to stop, but algorithm B needs algorithm A to stop, resulting in neither algorithm working.

Recursion is when an algorithm calls upon itself.

Asymptotic Notation

4YWVYPCcP+jITB1JEBCSDX4mnl69E4mSy4V8zeXTUbruJCIzz7x25sp1fDfU2EOWneEFDlERX6j4ygicqCuFhHZwAJTUN2BlRRrzgAAAABJRU5ErkJggg==

Multiple notations can be true. For example, if A grows faster than B, it means A can grow at least as fast and at the same rate as B. This is used for predicting speed.

Worst-Case and Best-Case Analysis

FF180yZzIQtzYs2ZvQ0JCdArDi01NOPZwLpyHG2UO0xcIIXDrlipcOapv2hG3g8igr9tBrMD9RvhhUcgwDMMwDMOw+5hhGIZhGIZhUcgwDMMwDMMQLAoZhmEYhmEYFoUMwzAMwzAMi0KGYRiGYRgGwP8fBdfRQiccvzIAAAAASUVORK5CYII=

Ac9j1X+7gSCWAAAAAElFTkSuQmCC

Insertion sort with binary search has a worst-case time complexity of O(n log n) and a best-case time complexity of O(n). N is O(1) is incorrect. Space complexity refers to the amount of memory used.

Pivot Search / Quick Sort

  1. Pick a number to be the pivot. This is moved to the end.
  2. Select an item from the left that is greater than the pivot, and then an item from the right that is smaller than the pivot.
  3. Swap the selected items and continue until the right index is smaller than the left index.

Often, the middle element is chosen as the pivot.

Z44c3ksrfRO1QREREREREREc0ie7OZIyIiIiIiIiJmGKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRJKJDFRERERERERHRFET+P1a0OqKsDVErAAAAAElFTkSuQmCC

Quick Select

Quick select is based on quick sort, where you find an element of a specific type, such as the second smallest element.

The difference is that once you’ve partitioned the array into two sub-arrays around the pivot, you only look at the partition that contains the desired element, such as the smaller partition if you’re looking for a smaller element.

Turing Machine

kwvfPyTjq8u1Z0GKCyAQAAADcXXXQRdeqVRzfd2Yd6DOyktgYDwhoAAADIINkS1gAAAABIH3AFBwAAAAAAAAAAUgDCGgAAAAAAAAAASAEIawAAAAAAAAAAIAUgrAEAAAAAAAAAgKQh+v8Bwz+zBgVpZ6IAAAAASUVORK5CYII=

j9Xvfy0s+Y3XAAAAABJRU5ErkJggg==

Insertion Sort

Insertion sort goes from left to right, comparing each element to its neighbors.

va39uXQI4880gxKa2v0Dkc3FbvPNCCMyC233GKzkG233VY223Qze5u7NtuooYbvMkT+HzB7W2T1+Y5tAAAAAElFTkSuQmCC

Insertion Binary

Start sorting the array. When you move to a new number, compare it to the already sorted portion. Check if it’s greater than the middle element of the sorted portion and move accordingly. Then, find a new middle. If there are only 1-2 elements left, this process helps determine the correct placement.

Heap Sort

m6sGkghauJz6LEPHHxmy5+1Pgpi0HIuYHuHSZznkCQYQjUaSolFAreGSt6PWIECFChIgbQyQHdwC4gHn6Dpc0cwImBmKTqAgRIkSIuBlEcnCHgOfSJkZOia0GIkSIECHid0IkByJEiBAhQoSIORDnsYkQIUKECBEi5kAkByJEiBAhQoSIORDJgQgRIkSIECFiDkRyIEKECBEiRIiYA5EciBAhQoQIESLmQCQHIkSIECFChIg5EMmBCBEiRIgQIWIWgP8HWrrprkojLj0AAAAASUVORK5CYII=

Create a binary tree from the array, going from left to right. Reorder the tree from highest to lowest. Remove the highest element, add it to a new array, and repeat until the tree is empty.

Merge Sort

Break down the array into smaller sub-arrays. Once fully broken down, begin to reassemble them in sorted order. The intermediate steps (often represented by grey blocks) are usually ignored as a single step.

300px-Merge_sort_algorithm_diagram.svg.png

Selection Sort

  1. Go through the array to find the lowest value.
  2. Move the lowest value to the front of the unsorted part of the array.
  3. Repeat steps 1 and 2 for the remaining unsorted portion of the array until the entire array is sorted.