1. Exam Points
- Analyze the
behaviorof a recursive algorithm.- Analyze the
resultof a recursive algorithm.- Analyze the
resultof implementingrecursive binary searching.- Analyze
how many times divide or mergewill be executed inmerge sort.
2. Knowledge Points
(1) Use Recursion for Searching and Sorting
Recursioncan be used totraverseString objects, arrays, and ArrayList objects.- Commonly sued
searching algorithms:Linear Search: check each element in order, the search can start from either end (first to last, last to first), applies to any list.
Binary Search: (apply only to sorted collection) : start from middle, cut in half for each round, applies only to sorted list.
(2) Binary Search
- Data must be in
sortedorder to use thebinary search algorithm. Binary searchstarts at the middle of a sorted array or ArrayList and eliminates half of the array or ArrayList in each recursive call until the desired value is found or all elements have been eliminated.- If a collection contains 5 elements, the 3rd one is the middle element.
- If a collection contains 4 elements, the 2nd one is the middle element.
- Binary search is
typically more efficientthan linear search. - The binary search algorithm can be written either
iterativelyorrecursively. Process analysis:
- Code implementation using recursion -
Example 1 (return -1 if not found):
// return the index of the target if found // or return arr.length if not found public static int binarySearch(int[] arr, int low, int high, int target) { int mid = (low + high) / 2; // find the index of the middle element if (low > high) { // if low is greater than high, means not found, return -1 return -1; } else if (target > arr[mid]) { // if target is greater, search in the second half return binarySearch(arr, mid + 1, high, target); } else if (target < arr[mid]) { // if target is lesser, search in the first half return binarySearch(arr, low, mid - 1, target); } else { // arr{mid] == num // if target is found, return its index return mid; } } public static void main(String[] args) { int[] data = { 1, 2, 3, 4, 5 }; // here target is 2 int indexOfTarget = binarySearch(data, 0, data.length - 1, 2); System.out.println(indexOfTarget); } - Code implementation using recursion -
Example 2 (return low if not found):
// return the index of the target if found // or return arr.length if not found public static int binarySearch(int[] arr, int low, int high, int target) { int mid = (low + high) / 2; // find the index of the middle element if (low > high) { // if low is greater than high, means not found, return low return low; } else if (target > arr[mid]) { // if target is greater, search in the second half return binarySearch(arr, mid + 1, high, target); } else if (target < arr[mid]) { // if target is lesser, search in the first half return binarySearch(arr, low, mid - 1, target); } else { // arr{mid] == num // if target is found, return its index return mid; } } public static void main(String[] args) { int[] data = { 1, 2, 3, 4, 5 }; // here target is 2 int indexOfTarget = binarySearch(data, 0, data.length - 1, 2); System.out.println(indexOfTarget); } - Conclusion:
- when the target is found in the collection, mid is returned, which is the index where target appears, and mid is also the number of elements that is less than target.
- 如果集合中找到了要查找的目标元素,返回值为mid,是目标元素对应的索引,也是小于查找目标的元素的个数。
- when the target is not found in the collection, returning low or -1 means not found, low also means the number of elements that is less than target, while -1 simply means not found.
- 如果集合中未找到要查找的目标元素,可以返回low或-1,表示未找到。返回的low也是小于查找目标的元素的个数。如果返回的不是low,而是-1,仅仅是表示未找到元素。
(3) Merge Sort
Merge sortis a recursive sorting algorithm that can be used to sort elements in an array or ArrayList.- Merge sort
repeatedly dividesan array into smaller subarrays until each subarray is one element andthen recursively mergesthe sorted subarrays back together in sorted order to form the final sorted array. Process demonstration: