This article will take you to understand the principle and code implementation of selective sorting, bubble sorting and binary search method, as well as some operations on Arrays through the Arrays class
1, Select sort
selection sorting is to start from the first number, compare with all numbers in turn, put the maximum (descending) / minimum (ascending) number in the first position, and then compare the second number with the number other than the first number, put the maximum (descending) / minimum (ascending) number in the second position, and so on until the end of the penultimate number, To understand, it's the same as that physical education teachers line up according to their height, always putting the highest or the shortest at the front
Arrange the elements in the array in descending order. The implementation code is as follows:
int [] arr = {3,5,56,73,43,108,89,7,27}; for(int i = 0 ;i < arr.length - 1; i++){ for(int j = i ;j < arr.length; j++){ if(arr[i] < arr[j]){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } System.out.println(Arrays.toString(arr)); //The output results are [108, 89, 73, 56, 43, 27, 7, 5, 3];
Matters needing attention:
1. The outer loop controls the number of comparisons. At the same time, the initialization of variables also controls which number to start from. We start from the first number, that is, the number with subscript 0, and compare the number of elements - once. Suppose we want to compare the size of three numbers, we only need to compare the first two numbers to know the size of the third number, So the control condition of the outer loop is arr.length - 1
2. The inner loop control is realized from the specific comparison between the number to be compared and all other numbers. Because the numbers after comparison are not necessary to be compared, we only need to start the comparison from the number given by the external loop
3. The ascending and descending order are determined by the comparison of internal loops. If it is greater than the exchange, it wants a small value, that is, ascending order. If it is less than the exchange, it wants a large value, that is, descending order
2, Bubble sorting
bubble sorting is to start from the first number and compare it with the next two in turn. The maximum (descending) / minimum (ascending) number is kept until the end. It is equivalent to that the bodyguard protects the eldest brother all the way back. Only the maximum or minimum number can go all the way to the end. Other numbers must give way (exchange) to him. Let's use the code to learn more about it
Arrange the elements in the array in descending order. The implementation code is as follows:
int [] arr = {3,5,56,73,43,108,89,7,27}; for(int i = 0 ;i < arr.length - 1;i++){ for(int j = 0 ;j < arr.length - 1 - i ;j++){ if(arr[j] < arr[j + 1]){ int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } System.out.println(Arrays.toString(arr)); //The output results are [108, 89, 73, 56, 43, 27, 7, 5, 3];
Matters needing attention:
1. The outer loop controls the number of comparisons. Starting from the number with subscript 0, it is compared with the following number in pairs. If there are three numbers, you only need to compare them twice to know the order of these three numbers
2. The inner loop control is the specific implementation of comparison. Because the qualified numbers are put to the last every time, it starts from the number with subscript 0 and compares them with the following numbers in pairs. Because the latter numbers have been arranged, there is no need to compare. The arranged numbers are determined by i, so i can be subtracted here to improve the operation efficiency
3, Binary search method
as we all know, a piece of ordinary A4 paper can be folded in half at most seven times, and the huge toilet paper used in Guinness world records can only be folded in half at most 13 times. The two-point search method is the same as this principle. It is through this thinking to quickly find the location of elements. Each search can eliminate half of the data. In an era with so many data, This is undoubtedly a very efficient method
the premise of using the binary search method is that the array has been arranged in ascending order. The principle is that we set a subscript for the position of start, end and mid, and compare the number to be searched with the middle value of the array. If it is smaller than the middle value, it means that it is between start and mid, then mid becomes end. If it is larger than the middle value, Then mid becomes start, and a new intermediate value is given. Then compare it, and so on, and finally find the final value
Write a method to return the subscript value of the specified data under the specified array. If it does not exist, it returns - 1. The implementation code is as follows (example):
public static int findData(int[] arr, int find){ int start = 0; int end = arr.length - 1; int findIndex = -1; while(start <= end){ int mid = (start + end) / 2; if(find == arr[mid]){ findIndex = mid; break; }else if(find < arr[mid]){ end = mid - 1; }else if(find > arr[mid]){ start = mid + 1; } } return findIndex; }
Suppose we want to find whether there is data in the array. The implementation process is shown in the figure:
4, Arrays class (operations on arrays)
the Arrays class is provided in Java to operate array objects, including sorting, judging whether they are equal, replacing values, etc
1. Sort the array in ascending order
Arrays.sort() method
the implementation code is as follows (example):
int [] arr = {3,5,56,73,43,108,89,7,27}; System.out.println("**********Before sorting***********"); for (int i : arr) { System.out.println(i + " "); } System.out.println("**********After sorting***********"); Arrays.sort(a); for (int i : a) { System.out.println(i); }
Matters needing attention:
this method can only sort in ascending order
2. Judge whether the two arrays are equal
Arrays.equals() method
the implementation code is as follows (example):
String [] str1 = null; String [] str2 = null; System.out.println(Arrays.equals(str1,str2)); int[] array1 = {3}; int[] array2 = {4}; System.out.println(Arrays.equals(array1,array2)); //The output result is true or false
Matters needing attention:
return true only if the data type and data of two arrays are the same
3. Convert array to string
Arrays.toString() method
the implementation code is as follows (example):
String [] a = {"1","2","3"}; System.out.println(Arrays.toString(a)); //The output result is [1, 2, 3]
Matters needing attention:
the returned is enclosed in square brackets, and each element is separated by commas
4. Replace all elements in the array with a value
Arrays.fill() method
the implementation code is as follows (example):
String [] a = {"1","2","3"}; Arrays.fill(a,"Ha ha, it was replaced"); System.out.println(Arrays.toString(a)); //The output result is [ha ha, replaced, ha ha, replaced, ha ha, replaced]
Matters needing attention:
the data type before replacement shall be consistent with the data type after replacement
summary
this article explains the principle and code implementation of selective sorting, bubble sorting and binary search method, as well as some operations on Arrays through the Arrays class. I hope it will be helpful to you