Skip to content

Commit

Permalink
deploy: 02110e6
Browse files Browse the repository at this point in the history
  • Loading branch information
kiannp44 committed Apr 27, 2023
1 parent 2d6939e commit ecb5243
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 105 deletions.
10 changes: 5 additions & 5 deletions assets/css/style.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/search-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"post0": {
"title": "Binary Sort Lesson",
"content": "Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. This topic can be tested on the AP Exam but it isnt commonly seen. . | Binary search is one of the fastest searching algorithms. When compared to a linear search, it is much faster the bigger the array is . | . Which of the following is a prerequisite for using binary search in Java? | The array must be sorted in ascending order | The array must be sorted in descending order | The array can be in any order | The array must have at least one element | . What is the worst-case scenario for binary search in Java? | The element being searched for is at the beginning of the array | The element being searched for is at the end of the array | The element being searched for is not in the array | The element being searched for is in the middle of the array | . Which of the following data structures is best suited for binary search? | Linked List | Array | Stack | Queue | . What is returned if the target is not present in the array? | False | 0 | -1 | The index of the element closest in value to the target | . What is returned if the target is present multiple times in the array? | Binary Search does not work with duplicate values | The index of the first occurrence will be returned | The index of the middle occurrence will be returned | The index of one of the occurrences will be returned | . Answers: 1, 3, 2, 3, 4 . Binary Search Hack 1 . def binary_search(arr, low, high, target): """ This function performs binary search recursively on a sorted integer array to find a target value. Args: arr: A sorted integer array. low: The lower index of the subarray being searched. high: The upper index of the subarray being searched. target: The integer value being searched for. Returns: The index of the target value if found, else -1. """ # Check if the subarray has been exhausted if high >= low: # Calculate the middle index of the subarray mid = low + (high - low) // 2 # If the target is found at the middle index, return it if arr[mid] == target: return mid # If the target is smaller than the middle element, # search the left half of the subarray elif arr[mid] > target: return binary_search(arr, low, mid - 1, target) # If the target is larger than the middle element, # search the right half of the subarray else: return binary_search(arr, mid + 1, high, target) else: # If the subarray has been exhausted and the target is not found, return -1 return -1 # Test the function arr = [1, 3, 5, 7, 9, 23, 45, 67] target = 45 result = binary_search(arr, 0, len(arr)-1, target) if result != -1: print(f"Element {target} is present at index {result}") else: print(f"Element {target} is not present in the array") . | def binary_search(arr, low, high, target): <identifier> expected | def binary_search(arr, low, high, target): <identifier> expected | def binary_search(arr, low, high, target): <identifier> expected | def binary_search(arr, low, high, target): <identifier> expected | def binary_search(arr, low, high, target): ';' expected | # Check if the subarray has been exhausted illegal character: '#' | # Calculate the middle index of the subarray illegal character: '#' | # If the target is found at the middle index, return it illegal character: '#' | # If the target is smaller than the middle element, illegal character: '#' | # search the left half of the subarray illegal character: '#' | # If the target is larger than the middle element, illegal character: '#' | # search the right half of the subarray illegal character: '#' | # If the subarray has been exhausted and the target is not found, return -1 illegal character: '#' | # Test the function illegal character: '#' . public static void mergeSort(int[] array) { if (array.length > 1) { int mid = array.length / 2; int[] leftArray = Arrays.copyOfRange(array, 0, mid); int[] rightArray = Arrays.copyOfRange(array, mid, array.length); mergeSort(leftArray); mergeSort(rightArray); int i = 0, j = 0, k = 0; while (i < leftArray.length && j < rightArray.length) { if (leftArray[i] < rightArray[j]) { array[k] = leftArray[i]; i++; } else { array[k] = rightArray[j]; j++; } k++; } while (i < leftArray.length) { array[k] = leftArray[i]; i++; k++; } while (j < rightArray.length) { array[k] = rightArray[j]; j++; k++; } } } public static int binarySearch(int[] array, int target) { int left = 0; int right = array.length - 1; while (left <= right) { int mid = (left + right) / 2; if (array[mid] == target) { return mid; } else if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { int[] array = {5, 6, 3, 1, 8, 9, 4, 7, 2}; mergeSort(array); int index = binarySearch(array, 7); if (index != -1) { System.out.println("Index of 7 is: " + index); } else { System.out.println("7 is not found in the array"); } } . Merge Sort Hack . class MergeSort { void merge(String arr[], int l, int m, int r) { // Find the sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ String[] L = new String[n1]; String[] R = new String[n2]; /* Copy data to temp arrays */ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarray array int k = l; while (i < n1 && j < n2) { if (L[i].compareTo(R[j]) > 0) { arr[k] = R[j]; j++; } else { arr[k] = L[i]; i++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } void sort(String arr[], int l, int r) { if (l < r) { // Find the middle point int m = l + (r - l) / 2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void print(String arr[]) { for (String word : arr){ System.out.print(word + " "); } } //tester method public static void main(String args[]) { String[] arr = new String[]{"Cow", "Dog", "Bird", "Sheep", "Goat", "Pig", "Quail", "Crow" }; System.out.println("Given Array:"); print(arr); MergeSort ob = new MergeSort(); ob.sort(arr, 0, arr.length - 1); System.out.println(" nSorted Array:"); print(arr); } } MergeSort.main(null); . Given Array: Cow Dog Bird Sheep Goat Pig Quail Crow Sorted Array: Bird Cow Crow Dog Goat Pig Quail Sheep .",
"content": "Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. This topic can be tested on the AP Exam but it isnt commonly seen. . | Binary search is one of the fastest searching algorithms. When compared to a linear search, it is much faster the bigger the array is . | . Which of the following is a prerequisite for using binary search in Java? | The array must be sorted in ascending order | The array must be sorted in descending order | The array can be in any order | The array must have at least one element | . What is the worst-case scenario for binary search in Java? | The element being searched for is at the beginning of the array | The element being searched for is at the end of the array | The element being searched for is not in the array | The element being searched for is in the middle of the array | . Which of the following data structures is best suited for binary search? | Linked List | Array | Stack | Queue | . What is returned if the target is not present in the array? | False | 0 | -1 | The index of the element closest in value to the target | . What is returned if the target is present multiple times in the array? | Binary Search does not work with duplicate values | The index of the first occurrence will be returned | The index of the middle occurrence will be returned | The index of one of the occurrences will be returned | . Answers: 1, 3, 2, 3, 4 . Hack 1 . public static void mergeSort(int[] array) { if (array.length > 1) { int mid = array.length / 2; int[] leftArray = Arrays.copyOfRange(array, 0, mid); int[] rightArray = Arrays.copyOfRange(array, mid, array.length); mergeSort(leftArray); mergeSort(rightArray); int i = 0, j = 0, k = 0; while (i < leftArray.length && j < rightArray.length) { if (leftArray[i] < rightArray[j]) { array[k] = leftArray[i]; i++; } else { array[k] = rightArray[j]; j++; } k++; } while (i < leftArray.length) { array[k] = leftArray[i]; i++; k++; } while (j < rightArray.length) { array[k] = rightArray[j]; j++; k++; } } } public static int binarySearch(int[] array, int target) { int left = 0; int right = array.length - 1; while (left <= right) { int mid = (left + right) / 2; if (array[mid] == target) { return mid; } else if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { int[] array = {5, 6, 3, 1, 8, 9, 4, 7, 2}; mergeSort(array); int index = binarySearch(array, 7); if (index != -1) { System.out.println("Index of 7 is: " + index); } else { System.out.println("7 is not found in the array"); } } . Hack 2 . public static void mergeSort(int[] array) { if (array.length > 1) { int mid = array.length / 2; int[] leftArray = Arrays.copyOfRange(array, 0, mid); int[] rightArray = Arrays.copyOfRange(array, mid, array.length); mergeSort(leftArray); mergeSort(rightArray); int i = 0, j = 0, k = 0; while (i < leftArray.length && j < rightArray.length) { if (leftArray[i] < rightArray[j]) { array[k] = leftArray[i]; i++; } else { array[k] = rightArray[j]; j++; } k++; } while (i < leftArray.length) { array[k] = leftArray[i]; i++; k++; } while (j < rightArray.length) { array[k] = rightArray[j]; j++; k++; } } } public static int binarySearch(int[] array, int target) { int left = 0; int right = array.length - 1; while (left <= right) { int mid = (left + right) / 2; if (array[mid] == target) { return mid; } else if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { int[] array = {5, 6, 3, 1, 8, 9, 4, 7, 2}; mergeSort(array); int index = binarySearch(array, 7); if (index != -1) { System.out.println("Index of 7 is: " + index); } else { System.out.println("7 is not found in the array"); } } . Merge Sort Hack . class MergeSort { void merge(String arr[], int l, int m, int r) { // Find the sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ String[] L = new String[n1]; String[] R = new String[n2]; /* Copy data to temp arrays */ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarray array int k = l; while (i < n1 && j < n2) { if (L[i].compareTo(R[j]) > 0) { arr[k] = R[j]; j++; } else { arr[k] = L[i]; i++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } void sort(String arr[], int l, int r) { if (l < r) { // Find the middle point int m = l + (r - l) / 2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void print(String arr[]) { for (String word : arr){ System.out.print(word + " "); } } //tester method public static void main(String args[]) { String[] arr = new String[]{"Cow", "Dog", "Bird", "Sheep", "Goat", "Pig", "Quail", "Crow" }; System.out.println("Given Array:"); print(arr); MergeSort ob = new MergeSort(); ob.sort(arr, 0, arr.length - 1); System.out.println(" nSorted Array:"); print(arr); } } MergeSort.main(null); . Given Array: Cow Dog Bird Sheep Goat Pig Quail Crow Sorted Array: Bird Cow Crow Dog Goat Pig Quail Sheep .",
"url": "https://kiannp44.github.io/KianFastPages/collegeboard/2023/04/26/lab1.html",
"relUrl": "/collegeboard/2023/04/26/lab1.html",
"date": " • Apr 26, 2023"
Expand Down
Loading

0 comments on commit ecb5243

Please sign in to comment.