Data structure and algorithm series XIX (binary search 2)

1. Introduction 1.1. Why study data structure and algorithm? ...
1. Introduction
2. Test you
3. Cases
4. Discussion and sharing

1. Introduction

1.1. Why study data structure and algorithm?

Some people say that data structure and algorithm, computer network, and operating system are the same, away from daily development, except for the interview may not be used in this life!

Some people say that I do business development. As long as I am proficient in API, framework and middleware, can not the code I write fly?

So the question comes: Why study data structure and algorithm?

#Reason 1: When interviewing, don't be held back by data structure and algorithm #Reason 2: Do you really want to be CRUD Boy all your life #Reason 3: Engineers who don't want to write open source framework and middleware are not good cooks

1.2. How to systematically learn data structure and algorithm?

I think it's better to learn data structure and algorithm. But I have two puzzles:

1. How to start learning?

2. What is there to learn?

Recommended learning methods:

#Learning methods 1. Starting from the foundation, systematic learning 2. More hands-on, each data structure and algorithm is implemented by code 3. Thinking is more important: understand the implementation idea and don't recite the code 4. Combined with daily development, corresponding application scenarios

Learning content recommendation:

There are many data structures and algorithms. Based on the practical principle, we learn classic and common data structures and algorithms

#Learning content: 1. Definition of data structure 2. Definition of algorithm 3. Complexity analysis 4. Common data structure Array, linked list, stack, queue Hash table, binary tree, heap Skip table, figure 5. Common algorithms Recursion, sorting, binary search Search, hash, greed, divide and conquer Dynamic planning, string matching

2. Test you

In the previous article: data structure and algorithm series 18 (binary search 1), we shared the idea of binary search algorithm, and realized it through circulation and recursion. The idea of binary search is very easy to understand (half search), I believe you have mastered it.

But do you remember? We left two thinking questions in the last article:

  • Implementation of binary search code, details to be noted (how to write efficient and no problem binary search code)

  • If there are duplicate numbers in the target array to be searched, how to realize binary search?

    • For example: find the element whose first value is equal to the target value

    • For example: find the element whose last value is equal to the target value

    • For example: find the first element greater than or equal to the target value

    • For example: find the last element less than or equal to the target value

#Test you: 1. Do you know how to write efficient and problem-free binary search code? 2. Can you write the common deformation code of binary search (with repeating elements)?

3. Cases

3.1. Implementation details of binary search code

#Binary search code implementation, details to be concerned: 1. Median calculation [problem guidance]: 1. We know that binary search involves three numerical changes 2. Low: low, starting from 0 3. High: high, starting from n-1 (n is the number of array elements) 4. Median: mid, normal writing: mid = (low + high) / 2 [question]: Is there any problem in writing mid = (low + high) / 2? [answer]: 1. There is a problem. If the value of low and high is large enough, then low + high may exceed int There is a risk of spillover 2. Recommended writing method: Mid = Low + (high low) / 2 3. More efficient writing method: Mid = Low + ((high - low) > > 1). Shift operation is more computer-friendly and has higher performance 2. Next search, low and high number transformation [problem guidance]: 1. If the target value is not found this time, you need to enter the next binary search 2. Change the low value or the high value to half as needed 3. Are you used to writing: low = mid; or high = mid [question]: Is there a problem with low = mid or high = mid? [answer]: 1. If there is a problem, low = mid, or high = mid will cause the problem of dead cycle 2. correct writing: low = mid + 1; or high = mid - 1

3.2. Binary search deformation (repeating element)

3.2.1. Find the element whose first value is equal to the target value
/** * Find the first element whose value is equal to the target value * @param array target array * @param n Target array size * @param target target value * @return Target value index */ public static int binarySearchFirst(int[] array,int n,int target){ // Low, high int low = 0; int high = n - 1; while(low <= high){ // Calculate median int mid = low + ((high - low) >> 1); if(array[mid] > target){ high = mid - 1; }else if(array[mid] < target){ low = mid + 1; }else{ // If mid equals 0, the first element is the target value // If mid is not equal to 0, but array[mid-1] is not equal to the target value // Indicates that the mid is also the target location if(mid == 0 || array[mid - 1] != target){ return mid; } // Otherwise, continue to find high = mid - 1; } } return -1; }
3.2.2. Find the last element whose value is equal to the target value
/** * Find the last element whose value is equal to the target value * @param array target array * @param n Target array size * @param target target value * @return Target value index */ public static int binarySearchLast(int[] array,int n,int target){ // Low, high int low = 0; int high = n - 1; while(low <= high){ // Calculate median int mid = low + ((high - low) >> 1); if(array[mid] > target){ high = mid - 1; }else if(array[mid] < target){ low = mid + 1; }else{ // If mid equals n-1, the last element is the target value // If mid is not equal to n-1, but array[mid+1] is not equal to the target value // Indicates that the mid is also the target location if(mid == (n-1) || array[mid + 1] != target){ return mid; } // Otherwise, continue to find low = mid + 1; } } return -1; }
3.2.3. Find the first element whose value is greater than or equal to the target value
/** * Find the first element whose value is greater than or equal to the target value * @param array target array * @param n Target array size * @param target target value * @return Target value index */ public static int binarySearchFirst_1(int[] array,int n,int target){ // Low, high int low = 0; int high = n - 1; while(low <= high){ // Calculate median int mid = low + ((high - low) >> 1); if(array[mid] < target){ low = mid + 1; }else { // If mid==0, the first element is the target location of the lookup // If mid!=0, if array [mid-1] < target, then mid is the target location to find // Otherwise, continue to find if(mid == 0 || array[mid - 1] < target){ return mid; } high = mid - 1; } } return -1; }
3.2.4. Find the last element whose value is less than or equal to the target value
/** * Find the last element whose value is less than or equal to the target value * @param array target array * @param n Target array size * @param target target value * @return Target value index */ public static int binarySearchLast_1(int[] array,int n,int target){ // Low, high int low = 0; int high = n - 1; while(low <= high){ // Calculate median int mid = low + ((high - low) >> 1); if(array[mid] > target){ high = mid - 1; }else{ // If mid==n-1, the last element is the target location of the lookup // If min!=n-1, if array [mid + 1] > target, mid is the target location of the search // Otherwise, continue to find if(mid == (n - 1) || array[mid + 1] > target){ return mid; } low = mid + 1; } } return -1; }
3.2.5. Test code
public static void main(String[] args) { // 1. Find the element whose first value is equal to the target value int[] array_1 = ; System.out.println("Target array:" + Arrays.toString(array_1)); int target = 8; int index_1 = binarySearchFirst(array_1,array_1.length,target); System.out.println("1.Find the target value of [first value equals to]:" + target+ " Elements of:" + index_1); // 2. Find the element whose last value is equal to the target value System.out.println("----------------Gorgeous split line------------------"); int index_2 = binarySearchLast(array_1,array_1.length,target); System.out.println("2.Find the target value of [last value equals to]:" + target + " Elements of:" + index_2); // 3. Find the first element greater than or equal to the target value System.out.println("----------------Gorgeous split line------------------"); int index_3 = binarySearchFirst_1(array_1,array_1.length,target); System.out.println("3.Find the first target value greater than or equal to:" + target + " Elements of:" + index_3); // 4. Find the last element less than or equal to the target value System.out.println("----------------Gorgeous split line-------------------"); int index_4 = binarySearchLast_1(array_1,array_1.length,target); System.out.println("4.Find the target value of [last value is less than or equal to]: " + target+ " Elements of:" + index_4); }
3.2.6. Test results D:\02teach\01soft\jdk8\bin\java com.anan.algorithm.search.BinarySearchEnhance Target array: [2, 4, 5, 6, 7, 9, 9, 9, 10] 1. Find the element of [first value equals to] target value: 8: - 1 ----------------Gorgeous split line--------------------- 2. Find the element of [last value equal to] target value: 8: - 1 ----------------Gorgeous split line--------------------- 3. Find the element with the target value of 8: 5 ----------------Gorgeous split line--------------------- 4. Find the element of [last value is less than or equal to] target value: 8: 4 Process finished with exit code 0

4. Discussion and sharing

#Test your answer: 1. Do you know how to write efficient and problem-free binary search code? [refer to Section 3.1] binary search code implementation details 2. Can you write the common deformation code of binary search (with repeating elements)? [refer to Section 3.2] binary search for deformation

5 May 2020, 00:51 | Views: 1484

Add new comment

For adding a comment, please log in
or create account

0 comments