Skip to content

Commit

Permalink
Lab 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KinerBoi committed Apr 26, 2023
1 parent fd390f4 commit 1aa8f44
Show file tree
Hide file tree
Showing 2 changed files with 484 additions and 0 deletions.
299 changes: 299 additions & 0 deletions _notebooks/2023-04-26-Lab1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lab 1 Hacks"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unsorted array:\n",
"apple cantelope egg banana blueberry kiwi \n",
"Sorted array:\n",
"apple banana blueberry cantelope egg kiwi \n",
"states:\n",
"Ohio-DeWine, California-Newsom, New York-Hochul, Massachusetts-Healey, Indiana-Holcomb, Hawaii-Honolulu\n",
"\n",
"Sorted states:\n",
"California-Newsom, Hawaii-Honolulu, Indiana-Holcomb, Massachusetts-Healey, New York-Hochul, Ohio-DeWine\n",
"\n"
]
}
],
"source": [
"public class State {\n",
" public String name;\n",
" public String governor;\n",
"\n",
" public State(String nameInput, String governorInput) {\n",
" name = nameInput;\n",
" governor = governorInput;\n",
" }\n",
"\n",
" public String getName() {\n",
" return name;\n",
" }\n",
"\n",
" public String getGovernor() {\n",
" return governor;\n",
" }\n",
"}\n",
"\n",
"public class MergeSort {\n",
" public void toString(State[] states) {\n",
" String output = \"\";\n",
" for (int i = 0; i < states.length; i++) {\n",
" if (i == states.length - 1) {\n",
" output += states[i].getName() + \"-\" + states[i].getGovernor();\n",
" } else {\n",
" output += states[i].getName() + \"-\" + states[i].getGovernor() + \", \";\n",
" }\n",
" }\n",
" System.out.println(output);\n",
" }\n",
"\n",
" public void sort(String arr[], int l, int r) {\n",
" // As long as the component is large enough to be divide, keeping running the functions\n",
" if (l < r) {\n",
" // Find midpoint of current component, and run functions on it\n",
" int m = l + (r - l) / 2;\n",
"\n",
" sort(arr, l, m);\n",
" sort(arr, m + 1, r);\n",
" merge(arr, l, m, r);\n",
" }\n",
" }\n",
"\n",
" public void merge(String[] arr, int l, int m, int r) {\n",
" // Size of first half\n",
" int n1 = m - l + 1;\n",
" // Size of second half\n",
" int n2 = r - m;\n",
"\n",
" // Left array\n",
" String[] L = new String[n1];\n",
" // Right array\n",
" String[] R = new String[n2];\n",
"\n",
" for (int i = 0; i < n1; ++i) {\n",
" L[i] = arr[l + i];\n",
" }\n",
" for (int j = 0; j < n2; ++j) {\n",
" R[j] = arr[m + 1 + j];\n",
" }\n",
" \n",
" int i = 0, j = 0;\n",
"\n",
" // Initial index of main array\n",
" int k = l;\n",
" while (i < n1 && j < n2) {\n",
" // Use compareTo function to compare alphabetical standings of words. Merge sort takes place, as words with characters earlier\n",
" // in the alphabet are added to arr first\n",
" if (L[i].compareTo(R[j]) <= 0) {\n",
" arr[k] = L[i];\n",
" i++;\n",
" }\n",
" else {\n",
" arr[k] = R[j];\n",
" j++;\n",
" }\n",
" k++;\n",
" }\n",
"\n",
" // Add any remaining elements in the left array\n",
" while (i < n1) {\n",
" arr[k] = L[i];\n",
" i++;\n",
" k++;\n",
" }\n",
"\n",
" //Add any remaining in the right array\n",
" while (j < n2) {\n",
" arr[k] = R[j];\n",
" j++;\n",
" k++;\n",
" }\n",
" } \n",
"\n",
" public void sort2(State arr[], int l, int r) {\n",
" // As long as the component is large enough to be divide, keeping running the functions\n",
" if (l < r) {\n",
" // Find midpoint of current component, and run functions on it\n",
" int m = l + (r - l) / 2;\n",
"\n",
" sort2(arr, l, m);\n",
" sort2(arr, m + 1, r);\n",
" merge2(arr, l, m, r);\n",
" }\n",
" }\n",
"\n",
" public void merge2(State[] arr, int l, int m, int r) {\n",
" // Size of first half\n",
" int n1 = m - l + 1;\n",
" // Size of second half\n",
" int n2 = r - m;\n",
"\n",
" // Left array\n",
" State[] L = new State[n1];\n",
" // Right array\n",
" State[] R = new State[n2];\n",
"\n",
" for (int i = 0; i < n1; ++i) {\n",
" L[i] = arr[l + i];\n",
" }\n",
" for (int j = 0; j < n2; ++j) {\n",
" R[j] = arr[m + 1 + j];\n",
" }\n",
" \n",
" int i = 0, j = 0;\n",
"\n",
" // Initial index of main array\n",
" int k = l;\n",
" while (i < n1 && j < n2) {\n",
" // Use compareTo function to compare alphabetical standings of words. Merge sort takes place, as words with characters earlier\n",
" // in the alphabet are added to arr first\n",
" if (L[i].getName().compareTo(R[j].getName()) <= 0) {\n",
" arr[k] = L[i];\n",
" i++;\n",
" }\n",
" else {\n",
" arr[k] = R[j];\n",
" j++;\n",
" }\n",
" k++;\n",
" }\n",
"\n",
" // Add any remaining elements in the left array\n",
" while (i < n1) {\n",
" arr[k] = L[i];\n",
" i++;\n",
" k++;\n",
" }\n",
"\n",
" //Add any remaining in the right array\n",
" while (j < n2) {\n",
" arr[k] = R[j];\n",
" j++;\n",
" k++;\n",
" }\n",
" }\n",
"\n",
" public static void main(String[] args) {\n",
" String[] words = {\"apple\", \"cantelope\" , \"egg\", \"banana\", \"blueberry\", \"kiwi\"};\n",
" System.out.println(\"Unsorted array:\");\n",
" for (String item : words) {\n",
" System.out.print(item + \" \");\n",
" }\n",
" System.out.println(\"\");\n",
" MergeSort myObj = new MergeSort();\n",
" myObj.sort(words, 0, words.length - 1);\n",
" System.out.println(\"Sorted array:\");\n",
" for (String item : words) {\n",
" System.out.print(item + \" \");\n",
" }\n",
" System.out.println(\"\");\n",
" State ohio = new State(\"Ohio\", \"DeWine\");\n",
" State california = new State(\"California\", \"Newsom\");\n",
" State newYork = new State(\"New York\", \"Hochul\");\n",
" State massachusetts = new State(\"Massachusetts\", \"Healey\");\n",
" State indiana = new State(\"Indiana\", \"Holcomb\");\n",
" State hawaii = new State(\"Hawaii\", \"Honolulu\");\n",
" State[] states = {ohio, california, newYork, massachusetts, indiana, hawaii};\n",
" System.out.println(\"states:\");\n",
" myObj.toString(states);\n",
" System.out.println(\"\");\n",
" myObj.sort2(states, 0, states.length - 1);\n",
" System.out.println(\"Sorted states:\");\n",
" myObj.toString(states);\n",
" System.out.println(\"\");\n",
" }\n",
"}\n",
"\n",
"MergeSort.main(null);\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Index of 45: 6\n"
]
}
],
"source": [
"public class BinarySearch {\n",
" //recursively searches through the sorted array to find 45\n",
" public static int binarySearch(int[] arr, int start, int end, int target) {\n",
" \n",
" // find the middle index of the array\n",
" int mid = (start + end) / 2;\n",
"\n",
" // middle element of the array is the target return index\n",
" if (arr[mid] == target) {\n",
" return mid;\n",
" }\n",
" // if the middle element is greater than the target, search the left half of the array\n",
" else if (arr[mid] > target) {\n",
" return binarySearch(arr, start, mid - 1, target);\n",
" }\n",
" // if the middle element is less than the target, search the right half of the array\n",
" else {\n",
" return binarySearch(arr, mid + 1, end, target);\n",
" }\n",
" }\n",
"\n",
" //tester\n",
" public static void main(String[] args) {\n",
" int[] arr = {1, 3, 5, 7, 9, 23, 45, 67};\n",
" int target = 45;\n",
" int index = binarySearch(arr, 0, arr.length - 1, target);\n",
" System.out.println(\"Index of 45: \" + index);\n",
" //should return index 6\n",
" }\n",
"}\n",
"BinarySearch.main(null);\n"
]
}
],
"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": "17.0.5+8-Ubuntu-2ubuntu120.04"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 1aa8f44

Please sign in to comment.