Java Learning (7), array, search algorithm, binary search method, bubble sort, select sort, insert sort, java bubble
I. Common array search algorithms
Working principle: it is also called sequential search. it searches for a given value in a column and checks Each element one by one from the beginning to find the desired element.
Example 1: Find the position where the specified number appears in the array, find the return subscript, and cannot find the return-1
1 import java. util. extends; 2 public class LinearSearch {3 public static void main (String [] argas) 4 {5 int [] array = {10,100,}; 6 System. out. println ("Enter the number you want to obtain"); 7 bytes input = new bytes (System. in); 8 int number = input. nextInt (); 9 int index =-1; // find the subscript of the number in the array, and cannot find equal to-110 for (int I = 0; I <array. length; I ++) 11 {12 if (array [I] = number) 13 {14 index = I + 1; 15 break; 16} 17} 18 if (index! =-1) 19 {20 System. out. println ("locate this number," + index + "location" in the array); 21} 22 else23 {24 System. out. println ("the number to be searched does not exist"); 25} 26} 27}View Code
Example 2: calculate the maximum and minimum values in the array
1 public class LinearSearch {2 public static void main (String [] argas) 3 {4 int [] array = {10,100, 90,65,}; 5 // calculate the maximum value in the array, minimum value 6 int max = array [0]; // maximum value 7 int min = array [0]; // minimum value 8 for (int I = 1; I <array. length; I ++) 9 {10 if (max <array [I]) 11 {12 max = array [I]; 13} 14 if (min> array [I]) 15 {16 min = array [I]; 17} 18} 19 System. out. println ("the maximum value of this array is" + max + ", and the minimum value is" + min); 20} 21}View Code
Ii. Binary Search
Working principle: it is also called the semi-query method. It compares the keywords recorded in the middle of the array with the search keywords. If the two are the same, the search is successful; otherwise, the array is divided into two subarrays by using the intermediate position record. If the keyword of the record in the intermediate position is greater than the search keyword, the previous subarray is further searched. Otherwise, the next subarray is further searched. Repeat the preceding process until it is found or cannot be found.
Note: For ordered arrays
1 import java. util. extends; 2 public class LinearSearch {3 public static void main (String [] argas) 4 {5 int [] array2 = {1, 2, 3, 4, 5, 10, 15, 18, 19,22 }; 6 System. out. println ("Enter the number you want to obtain"); 7 bytes input = new bytes (System. in); 8 int number = input. nextInt (); 9 int index =-1; // find the subscript of the number in the array. The value is-110 int start = 0; // start subscript 11 int end = array2.length-1; // terminate subscript 12 int middle; 13 while (start <= end) 14 {15 // find the element value corresponding to the intermediate subscript 16 mi Ddle = (start + end)/2; 17 int number2 = array2 [middle]; 18 // assume that the number to be searched is greater than the number to be compared in the middle 19 // remove the number on the left 20 if (number> number2) 21 {22 start = middle + 1; 23} 24 // retain the number on the left, remove the number on the right 25 else if (number <number2) 26 {27 end = middle-1; 28} 29 else30 {31 index = middle + 1; 32 break; 33} 34} 35 if (index! =-1) 36 {37 System. out. println ("locate the number, position" + index + "in the array"); 38} 39 else40 {41 System. out. println ("the number to be searched does not exist"); 42} 43 44} 45}View Code
Iii. Bubble Sorting
Working principle: Compare adjacent elements. If the first element is larger than the second element, exchange the two adjacent elements. Perform the same work on each adjacent element, from the first to the last. The final element should be the largest number. Repeat the preceding steps for all elements except the last one. Until there is no one to be compared.
1 public class BubbleSort {2 public static void main (String [] argas) 3 {4 int [] array = }; 5 // N the number of rounds compared to the number of N-1 6 for (int I = 0; I <array. length-1; I ++) 7 {8 // the number of times each round of comparison is N-1-i 9 for (int j = 0; j <array. length-i-1; j ++) 10 {11 // compare adjacent 2 numbers, small front 12 if (array [j]> array [j + 1]) 13 {14 // exchange two numbers by setting the Temporary Variable 15 int temp = array [j]; 16 array [j] = array [j + 1]; 17 array [j + 1] = temp; 18} 19} 20} 21 // output 22 for (int I = 0; I <array. length; I ++) 23 {24 System. out. print (array [I] + ","); 25} 26} 27}View Code
4. Select sorting method
Working principle: first, find the minimum element in the unordered sequence, store it to the starting position of the sorting sequence, and then continue to find the minimum element from the remaining unordered elements, and put it at the end of the sorting sequence. And so on until all elements are sorted.
1 public class SelectSort {2 public static void main (String [] argas) 3 {4 int [] array = }; 5 int min = 0; // Save the minimum element value of the subscript 6 // N number of The number of rounds for the N-1 times 7 for (int I = 0; I <array. length-1; I ++) 8 {9 min = I; 10 // search for the subscript 11 for (int j = I + 1; j <array. length; j ++) 12 {13 if (array [min]> array [j]) 14 {15 min = j; // Save the minimum number of subscript 16} 17} 18 // if the position of the I number is not on I, 19 if (I! = Min) 20 {21 int temp = array [I]; 22 array [I] = array [min]; 23 array [min] = temp; 24} 25} 26 27 for (int I = 0; I <array. length; I ++) 28 {29 System. out. print (array [I] + ","); 30} 31} 32}View Code
V. Insert sorting
Working principle: It builds an ordered sequence. For sorted data, It scans the sorted sequence from the back to the front, finds the corresponding position, and inserts it. In the process of scanning from the back to the forward, you need to repeatedly move the sorted elements backward to provide the insert space for the latest elements.
1 public class InsertSort {2 public static void main (String [] argas) 3 {4 int [] array = }; 5 for (int I = 1; I <array. length; I ++) 6 {7 int temp = array [I]; 8 // Save the subscript 9 int j = I; 10 while (j> 0 & temp <array [J-1]) 11 {12 // The number above overwrites the number below 13 array [j] = array [J-1]; 14 j --; 15} 16 array [j] = temp; // insert data 17} 18 19 for (int I = 0; I <array. length; I ++) 20 {21 System. out. print (array [I] + ","); 22} 23} 24}View Code