diff --git a/_notebooks/2023-10-31-P1_Recursion.ipynb b/_notebooks/2023-10-31-P1_Recursion.ipynb new file mode 100644 index 0000000..b58c45e --- /dev/null +++ b/_notebooks/2023-10-31-P1_Recursion.ipynb @@ -0,0 +1,1029 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "toc: true\n", + "comments: true\n", + "layout: post\n", + "title: U10 Recursion P1\n", + "description: Lesson on Java recursion.\n", + "type: ccc\n", + "courses: { csa: {week: 11} }\n", + "authors: David, Derrick\n", + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 10.1 Recursion\n", + "> A method calling itself to repeat a certain number of times. \n", + "\n", + "**Popcorn Hack:** What is recursion?\n", + "\n", + "### Vocabulary\n", + "- Base Case - this sets the final requirement for when the recursion will stop\n", + "- Formal Parameters - attributes within the method (local variables)\n", + "\n", + "### Method Calling Itself\n", + "\n", + "We will first be looking at how a method can call itself." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [], + "source": [ + "/* \n", + "DO NOT RUN THIS\n", + "if you do run this accidentally press `ctrl + c` twice\n", + "*/\n", + "\n", + "public class Recursion1 {\n", + " public static void recursionCount(int n) { // original method definition\n", + " System.out.println(n);\n", + " recursionCount(n - 1); // calling the method again\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " recursionCount(3); // original call of the method\n", + " }\n", + "}\n", + "\n", + "Recursion1.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this example, you will receive an overflow error because the method is calling itself infinitely. To avoid this you would create a base case.\n", + "\n", + "A base case usually uses an `if-then` statement to turn the loop off after a certain condition is met." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "2\n", + "1\n", + "done\n" + ] + } + ], + "source": [ + "public class Recursion2 {\n", + " public static void recursionCount(int n) {\n", + " if (n <= 0) { // added base case, telling the program to stop when n is less than or equal to 0\n", + " System.out.println(\"done\");\n", + " } else {\n", + " System.out.println(n);\n", + " recursionCount(n - 1); // method recursion\n", + " }\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " recursionCount(3); // calling original function\n", + " }\n", + "}\n", + "\n", + "Recursion2.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Popcorn Hack:** Create your own quick recursive program." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [], + "source": [ + "// Add code here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Calling With Different Parameters\n", + "\n", + "You can also call the method with multiple different formal parameters, also known as local variables.\n", + "\n", + "In this example we call an integer and string." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "n = 5 a = test\n", + "n = 4 a = test!\n", + "n = 3 a = test!!\n", + "n = 2 a = test!!!\n", + "n = 1 a = test!!!!\n", + "done\n" + ] + } + ], + "source": [ + "public class Recursion3 {\n", + " public static void recursionPrint(int n, String a) { // formal parameters are within this method\n", + " if (n <= 0) { // base case\n", + " System.out.println(\"done\");\n", + " return;\n", + " }\n", + "\n", + " System.out.println(\"n = \" + n + \" a = \" + a); // printing each var with each call of the method\n", + "\n", + " String addChar = a + \"!\"; // mutating string with each recursion\n", + " int addN = n - 1; // mutating integer with each recursion\n", + "\n", + " recursionPrint(addN, addChar); // recursive function\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " recursionPrint(5, \"test\"); // initial call\n", + " }\n", + "}\n", + "Recursion3.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here we just compound each string and subtract one from every previous number.\n", + "\n", + "### Capturing Progression Through Recursion\n", + "\n", + "You can also capture the progression through recursion. This is useful for doing specific tasks in specific steps through recursion." + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "5\n", + "4\n", + "Reached halfway through the array.\n", + "3\n", + "2\n", + "1\n", + "done\n" + ] + } + ], + "source": [ + "public class Recursion4 {\n", + " public static void recursionCount(int n, int progress) {\n", + " if (n <= 0) {\n", + " System.out.println(\"done\");\n", + " } else {\n", + " if (n == progress) {\n", + " System.out.println(\"Reached halfway through the array.\"); // special action done only when the program has reached a certain point\n", + " }\n", + " System.out.println(n);\n", + " recursionCount(n - 1, progress); // recursive call\n", + " }\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " int n = 6;\n", + " int progress = n / 2; // progress is set to half of the array length\n", + " recursionCount(n, progress); // initial call\n", + " }\n", + "}\n", + "\n", + "Recursion4.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Popcorn Hack:** What other situations could we use capturing progression for?\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Converting Recursion to Iteration\n", + "\n", + "Any recursive process can be made iterative, however this is not needed for the AP test. It is, however, still useful.\n", + "\n", + "#### Recursive Version" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "2\n", + "1\n", + "done\n" + ] + } + ], + "source": [ + "public class Recursion5 {\n", + " public static void recursionCount(int n) {\n", + " if (n <= 0){\n", + " System.out.println(\"done\");\n", + " } else {\n", + " System.out.println(n);\n", + " recursionCount(n - 1); // recursive function\n", + " }\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " recursionCount(3); // initial call\n", + " }\n", + "}\n", + "\n", + "Recursion5.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Iterative Version" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "2\n", + "1\n", + "done\n" + ] + } + ], + "source": [ + "public class Iteration {\n", + " public static void iterationCount(int n) {\n", + " for (int i = n; i >= 1; i--) { // using a for loop to iterate, notice we go only so i is greater than 1 and not 0\n", + " System.out.println(i);\n", + " }\n", + " System.out.println(\"done\");\n", + " }\n", + " \n", + " public static void main(String[] args) {\n", + " iterationCount(3); // same initial call\n", + " }\n", + "}\n", + "\n", + "Iteration.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Popcorn Hacks:** We we go only so i is greater than or equal to 1 and not 0. Why?\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Traversing With Recursion\n", + "\n", + "You can traverse many things with recursion. Here I will show traversal through a string, array, and an `ArrayList` object.\n", + "\n", + "#### String Traversal\n", + "\n", + "You can traverse through a string, finding a specific character in the string." + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sample String: words\n", + "Character 's' is found.\n" + ] + } + ], + "source": [ + "public class FindChar {\n", + " public static boolean findChar(String s, char target, int index) {\n", + " if (index >= s.length()) { // checks for if the index we are looking for is greater than the word length\n", + " return false;\n", + " }\n", + "\n", + " if (s.charAt(index) == target) { // checks for if there is a character the index we are searching\n", + " return true;\n", + " }\n", + "\n", + " return findChar(s, target, index + 1); // recalls method, recursion\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " String sampleString = \"words\"; // example string\n", + " char targetChar = 's'; // character we are looking for\n", + "\n", + " System.out.println(\"Sample String: \" + sampleString);\n", + "\n", + " boolean isCharFound = findChar(sampleString, targetChar, 0); // checks if the character we are looking for is found, based on boolean\n", + "\n", + " if (isCharFound) {\n", + " System.out.println(\"Character '\" + targetChar + \"' is found.\"); // if it is found, show this\n", + " } else {\n", + " System.out.println(\"Character '\" + targetChar + \"' is not found.\"); // if not show this\n", + " }\n", + " }\n", + "}\n", + "\n", + "FindChar.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also find and print a certain range of characters from the string." + ] + }, + { + "cell_type": "code", + "execution_count": 158, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Word: example\n", + "Start: 0, End: 5\n", + "Characters in the specified range: examp" + ] + } + ], + "source": [ + "public class PrintRange {\n", + " public static void printRange(String s, int startIndex, int endIndex) {\n", + " if (startIndex < 0 || endIndex > s.length() || startIndex >= endIndex) { // checks for invalid declaration or end (negative index, index larger than string length, index start has reached the end)\n", + " return;\n", + " }\n", + "\n", + " System.out.print(s.charAt(startIndex)); // print the characters in the range\n", + " printRange(s, startIndex + 1, endIndex); // recursive calling\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " String sampleString = \"example\"; // string\n", + " int startIndex = 0;\n", + " int endIndex = 5;\n", + " \n", + " System.out.println(\"Word: \" + sampleString);\n", + " System.out.println(\"Start: \" + startIndex + \", End: \" + endIndex);\n", + "\n", + " System.out.print(\"Characters in the specified range: \");\n", + " printRange(sampleString, startIndex, endIndex); // initial call\n", + " }\n", + "}\n", + "\n", + "PrintRange.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Popcorn Hack:** Make it so that we can find two different ranges and print them." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [], + "source": [ + "// Enter code here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Array Traversal\n", + "\n", + "You can also traverse arrays using recursion." + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Index 2 is found in the array at position 2 with a value of 6\n" + ] + } + ], + "source": [ + "public class ArrayTraversal {\n", + " public static int findIndex(int[] arr, int targetIndex, int currentIndex) {\n", + " if (currentIndex < 0 || currentIndex >= arr.length) { // checks for invalid (index less than 0, index greater than array length)\n", + " return -1; // returns invalid\n", + " }\n", + "\n", + " if (currentIndex == targetIndex) { // if the index is found\n", + " return currentIndex;\n", + " }\n", + "\n", + " return findIndex(arr, targetIndex, currentIndex + 1); // recursion\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " int[] sampleArray = {1, 4, 6, 8, 4, 9, 12};\n", + " int targetIndex = 2; // target\n", + "\n", + " int foundIndex = findIndex(sampleArray, targetIndex, 0);\n", + "\n", + " if (foundIndex != -1) { // checks what has been outputted\n", + " int value = sampleArray[foundIndex]; // found index value\n", + " System.out.println(\"Index \" + targetIndex + \" is found in the array at position \" + foundIndex + \" with a value of \" + value); // found\n", + " } else {\n", + " System.out.println(\"Index \" + targetIndex + \" is not found in the array.\"); // not found\n", + " }\n", + " }\n", + "}\n", + "\n", + "ArrayTraversal.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### ArrayList Object Traversal\n", + "\n", + "We can also traverse the `ArrayList` object." + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Index 2 is found in the ArrayList at position 2 with a value of 6\n" + ] + } + ], + "source": [ + "import java.util.ArrayList;\n", + "\n", + "public class ArrayListTraversal {\n", + " public static int findIndex(ArrayList list, int targetIndex, int currentIndex) {\n", + " if (currentIndex < 0 || currentIndex >= list.size()) { // checks for invalid\n", + " return -1;\n", + " }\n", + "\n", + " if (currentIndex == targetIndex) { // finds index\n", + " return currentIndex;\n", + " }\n", + "\n", + " return findIndex(list, targetIndex, currentIndex + 1); // recusion\n", + " }\n", + "\n", + " public static void main(String[] args) {\n", + " ArrayList sampleList = new ArrayList<>(); // created ArrayList object\n", + " sampleList.add(1);\n", + " sampleList.add(4);\n", + " sampleList.add(6);\n", + " sampleList.add(8);\n", + " sampleList.add(4);\n", + " sampleList.add(9);\n", + " sampleList.add(12);\n", + "\n", + " int targetIndex = 2; // target\n", + "\n", + " int foundIndex = findIndex(sampleList, targetIndex, 0);\n", + "\n", + " if (foundIndex != -1) { // checks for if found or not\n", + " int value = sampleList.get(foundIndex); // gets found index from ArrayList object\n", + " System.out.println(\"Index \" + targetIndex + \" is found in the ArrayList at position \" + foundIndex + \" with a value of \" + value);\n", + " } else {\n", + " System.out.println(\"Index \" + targetIndex + \" is not found in the ArrayList.\");\n", + " }\n", + " }\n", + "}\n", + "\n", + "ArrayListTraversal.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Conclusion\n", + "\n", + "Must Knows:\n", + "- How to create recursive code (recalling a method within itself, with parameters that change)\n", + "- Record progression through recursion (completing certain tasks through the progression)\n", + "- Traversal of strings, arrays, and the `ArrayList` object\n", + "\n", + "Overall, recursion is useful as an alternative to iteration. Recursion is usually chosen for problems that can naturally be divided into parts, like sorting which will be talked about in the next part." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 10.2 Binary Search" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Binary Search vs Linear Search" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "target number - 24\n", + "\n", + "intArray - 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40\n", + "\n", + "#### Popcorn hack:\n", + "How many times iterated through for Linear Search? \n", + "\n", + "Answer:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example of Binary Search with Recursion" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Target found at index: 12\n" + ] + } + ], + "source": [ + "public class BinarySearch {\n", + " public static void main(String[] args) {\n", + " int[] intArray = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40}; // Example array\n", + " int target = 24; // Example target value\n", + " int result = recBinarySearch(intArray, 0, intArray.length - 1, target);\n", + "\n", + " if (result == -1) {\n", + " System.out.println(\"Target not found in the array.\");\n", + " } else {\n", + " System.out.println(\"Target found at index: \" + result);\n", + " }\n", + " }\n", + "\n", + " public static int recBinarySearch(int[] intArray, int lowPosition, int highPosition, int target) { //method\n", + " if (lowPosition > highPosition) {\n", + " return -1; // Element not found in the array\n", + " } else {\n", + " int midPosition = (lowPosition + highPosition) / 2;\n", + " if (intArray[midPosition] < target) {\n", + " return recBinarySearch(intArray, midPosition + 1, highPosition, target);\n", + " } else if (intArray[midPosition] > target) {\n", + " return recBinarySearch(intArray, lowPosition, midPosition - 1, target);\n", + " } else {\n", + " return midPosition; // Element found at midPosition\n", + " }\n", + " }\n", + " }\n", + "}\n", + "\n", + "BinarySearch.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Call 1\n", + "Index = 0 - 20, midPosition = 10, midPosition value = 20\n", + "\n", + "Since 24 > 20, \n", + "\n", + "then... \n", + "\n", + "lowPosition = midPosition + 1 = 11\n", + "highPosition = highPosition = 20\n", + "\n", + "#### Call 2\n", + "Index = 11-20, midPosition index = 15, midPosition value = 30\n", + "\n", + "Since 24 < 30,\n", + "\n", + "then...\n", + "\n", + "lowPosition = lowPosition = 11\n", + "high position = midPosition - 1 = 14\n", + "\n", + "\n", + "#### Call 3\n", + "Index = 11-14, midPosition index = 12, midPosition value = 24\n", + "\n", + "Since 24 = 24,\n", + "\n", + "then...\n", + "\n", + "return midPosition;\n", + "\n", + "In total, our recursive calls to the method 3 times." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Recursive Logic behind Merge Sort" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What is Merge Sort?\n", + "Merge sort is a recursive sorting algorithm that can be used to sort elements in an array or ArrayList\n", + "\n", + "- Follows a divide and conquer approach" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![Link](https://media.discordapp.net/attachments/829194398364729365/1168722443377057872/QUaMvjx.png?ex=6552cc98&is=65405798&hm=6f4a3f3612f2f1ccf4ccbe3741e9dfdc328453c10a27b83338a723ec1e68327b&=&width=337&height=325)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Notes:\n", + "```\n", + "List: [38,27,43,3,9,82,10] \n", + "\n", + "sudocode version:\n", + "mergeSort(List) {\n", + " mergeSort(left)\n", + " mergeSort(right)\n", + " merge(left & right)\n", + "\n", + "} \n", + "```\n", + "\n", + "- Must finish call above it in order to finish the call\n", + "\n", + "Call 1: \n", + "1. Splitting List into half \n", + "2. Left side: [38, 27, 43, 3]\n", + "3. Must finish call 1 in order to do the right side and do the merge\n", + "4. Recursively calls mergesort and splits the list in half again\n", + "\n", + "Call 2:\n", + "1. New Left side List: [38, 27]\n", + "2. Method calls are stacking on top of each other" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://media.discordapp.net/attachments/829194398364729365/1168730076439908374/image.png?ex=6552d3b4&is=65405eb4&hm=ec96b560869e656d80c5cd64322f7ddeb94f183e19340813edfbef754c8c2445&=&width=762&height=425)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Notes:\n", + "1. Element 5 can't be split into the left or the right, nor can it be merged with itself\n", + "2. Consider the left call complete!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://media.discordapp.net/attachments/829194398364729365/1168731071916036166/image.png?ex=6552d4a1&is=65405fa1&hm=9014369cfbc0598eff54e91c567bb2281c07f4fa069e416cb6a1e846be283aeb&=&width=772&height=425)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Notes:\n", + "1. Same thing applies with the right, element 25 can't be split to the left or the right, nor can it merge with itself. \n", + "2. Now we will merge them back in order: [5, 25]\n", + "\n", + "#### Important concepts: \n", + "1. When making new recursive call, we are NOT making a new list, array, or a new set of elements. \n", + "2. Basically updating all the way back to the original list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![img](https://media.discordapp.net/attachments/829194398364729365/1168731966858862733/image.png?ex=6552d576&is=65406076&hm=07b9e892835812449ea03f9e58a920611e0cb3f800bdface3f25078d64b2e4ba&=&width=725&height=425)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Notes:\n", + "1. When merging back together, it will merge back from least to greatest. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![img](https://media.discordapp.net/attachments/829194398364729365/1168732306685558844/image.png?ex=6552d5c7&is=654060c7&hm=6df03d3ea8adbcc67d5cc3a82cc617a96c56d0250ad26e51723b57850358e054&=&width=880&height=425)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Popcorn Hack: What will the final list be?\n", + "\n", + "Answer:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The mergeSort Method" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```\n", + "//sudocode\n", + "mergeSort(myArray, low, high) {\n", + " if (low < high) {\n", + " middle = (low + high)/2; //find middle\n", + " mergeSort(myArray, low, middle); //make a recursive call from low to middle (look at left hand side)\n", + " mergeSort(myArray, middle + 1, high); //once low is no longer less than high, make a new recursive call (look at right hand side)\n", + " merge (myArray, low, middle, high); //merge back together\n", + " }\n", + "}\n", + "```\n", + "\n", + "```\n", + "int [] myArray = {3, 4, 6, 8, 1, 2, 5, 7};\n", + "```\n", + "\n", + "#### Steps:\n", + "\n", + "1. Split the Array in half\n", + "2. Left side: {3, 4, 6, 8}; Right side {1, 2, 5, 7};\n", + "\n", + "![img](https://media.discordapp.net/attachments/829194398364729365/1168735841624006726/image.png?ex=6552d912&is=65406412&hm=7a20be09ae6e63eb0a0f998e5515c4ea6d02a7bb43ca0a8d4725c90cbb2d3727&=&width=662&height=139)\n", + "\n", + "3. Compare the first indexes in each individual array (which would be index 0 and index 4)\n", + "\n", + "![img](https://media.discordapp.net/attachments/829194398364729365/1168736588147212318/image.png?ex=6552d9c4&is=654064c4&hm=3d911c3eb088e3ae6b86ec96c96372f9b0314f86bbb8318a79d7719fa2d24fb3&=&width=587&height=130)\n", + "\n", + "![img](https://media.discordapp.net/attachments/829194398364729365/1168736615351459901/image.png?ex=6552d9cb&is=654064cb&hm=8fb527126ca542b16242b2c5b18e319c2703c863d25caafc657db9a61d3564ef&=&width=824&height=169)\n", + "\n", + "4. Since 1<3, our new index 0 value would be 1 when we merge the array back together\n", + "\n", + "![img](https://media.discordapp.net/attachments/829194398364729365/1168737089744023645/image.png?ex=6552da3c&is=6540653c&hm=d6f2ad0d1586f38a0fba7a3915fea07d22540a1106e461d012305eeae33697b6&=&width=578&height=121)\n", + "\n", + "![img](https://media.discordapp.net/attachments/829194398364729365/1168737204764422144/image.png?ex=6552da57&is=65406557&hm=ba40fc527b3ad870931fd9228940ff66c3540385057d435bbb9d704b0906b054&=&width=837&height=159)\n", + "\n", + "5. Since 5>3, our new index 2 value would be 3 when we merge the array back together" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Popcorn Hack: What will the final array return?\n", + "\n", + "Answer: {}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Wait, but there's an issue...\n", + "\n", + "![img](https://media.discordapp.net/attachments/829194398364729365/1168738307308519504/image.png?ex=6552db5e&is=6540665e&hm=a4b49b1f7cc3c76823db034dc491fc625b80b0e8f5c2dbfae73f5e9e27c0d39a&=&width=586&height=128)\n", + "\n", + "![img](https://media.discordapp.net/attachments/829194398364729365/1168738330993758318/image.png?ex=6552db64&is=65406664&hm=31a3de717babc4970f8234b1ed17170f092ab42b72fdcc737bba1a55f30b12ab&=&width=722&height=157)\n", + "\n", + "- After comparing index 3 and index 7, we then need to compare index 3 and index, but there is no index 8...\n", + "- Will recieve an index out of bounds exception.\n", + "\n", + "No worries! Since we are done with the sort, we can just move the rest of the elements to the end of the array because we are already done sorting. \n", + "\n", + "Index 3 will now become index 7." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Hacks\n", + "\n", + "- Create a 2D array either with just a regular array variable that you can recursively traverse through.\n", + "- Each value in this 2D array must be a string that you can individually traverse through.\n", + "- You must output a result of all string values that have a user inputted letter." + ] + }, + { + "cell_type": "code", + "execution_count": 160, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [], + "source": [ + "import java.util.Scanner;\n", + "\n", + "public class String2DArrayTraversal {\n", + " public static void main(String[] args) {\n", + " // code in here\n", + " }\n", + "\n", + " public static void traverseString2DArray(String[][] array, char letter, int row, int col) {\n", + " // code in here\n", + " }\n", + "}\n", + "\n", + "String2DArrayTraversal.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create your own merge chart (like the first image in this 10.2 lesson) with your own values from a list, array, or arraylist (doesn't matter). Explain how recursion works in the merge chat." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [], + "source": [ + "// code in here" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Java", + "language": "java", + "name": "java" + }, + "language_info": { + "codemirror_mode": "java", + "file_extension": ".jshell", + "mimetype": "text/x-java-source", + "name": "Java", + "pygments_lexer": "java", + "version": "11.0.20+8-post-Ubuntu-1ubuntu120.04" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}