An array is a set of elements of the same data type arranged in a certain order, that is, a variable of the same type is named with a name,
Then use the numbers to differentiate the set of their variables, which is called the array name, and the number is called subscript. Each variable that makes up an array is called the component of an array,
A form of organizing several variables of the same type in an orderly form.
The collection of these ordered, homogeneous data elements is called an array.
Here are a few books summarizing several commonly used array sorting, hit soy sauce. And I want to be helpful to everyone.
Array Linear lookup : Also called sequential lookup. Searches for a given number of values in a column,
Step through each element from one end until the process of finding the desired element
The code is as follows :
Public classTest1 { Public Static voidMain (string[] args) {int[] array={1,2,3,6,4,5}; System.out.println ("Please enter the number you want to query"); Scanner SS=NewScanner (system.in); intinput=Ss.nextint (); intIndex=-1;//since the array subscript is starting from 0, if the subscript or-1 indicates//the array did not find the value you entered for(inti = 0; I <array.length; i++) { if(Array[i]==input) {//to determine the data that is entered in an arrayindex=i+1;//i means array subscript +1 description is the position of the array Break; } } if(Index!=-1) {System.out.println ("The value you entered is found in the array, in the position" +index+ "); }Else{System.out.println ("There is no value in the array you want to find"); } }}
Two-Way search : Also known as binary search method. Compare the keywords in the middle position of the array with the LOOKUP keyword
If the two are equal, the search succeeds, otherwise the array is divided into 2 sub-arrays using the intermediate positional records.
If the key in the middle position record is greater than the lookup keyword, the previous sub-array is further found,
Otherwise, the latter sub-array is further searched. Repeat the process until you find or cannot find the
The code is as follows:
Public classTest2 { Public Static voidMain (string[] args) {int[] Array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 33, 55, 88 }; intFirst = 0;//Start numberScanner Scanner =NewScanner (system.in); System.out.println ("Please enter a number"); intnum =Scanner.nextint (); intlast = array.length-1;//Last number intindex =-1; intMiddle;//Middle Number while(Last >=First ) {Middle= (first + last)/2; if(Array[middle] = =num) {Index= Middle;//If you just guessed the middle number , Break; } if(Num >Array[middle]) { First= middle + 1;//the number of guesses is greater than the middle number//The median before the first number of the split number is assigned, and the average number is again calculated } if(Num <Array[middle]) { Last= Middle-1; } } if(Index! =-1) {System.out.println ("You want to check the number in" + (Index + 1) + "bit"); }Else{System.out.println ("I didn't find it."); } }}
Bubble sort : Compare adjacent elements if the first one is bigger than the second, swap them both positions subscript
Do the same for each adjacent element, starting with the last pair from the first pair to the end.
Repeat the above steps for all elements except the last element. Until there are no pairs of arrays to compare.
The code is as follows:
Public classTest4 { Public Static voidMain (string[] args) {int[] array={1,2,3,9,7,6,4,5}; //n number of wheels compared to N-1 for(inti = 0; i < array.length-1; i++) { //the number of comparisons per round was n-1-i; for(intj = 0; J < array.length-1-I; J + +) { //Compare adjacent two numbers, small front if(array[j]>array[j+1]){ //two numbers do swap locations by setting temporary variables inttemp=Array[j]; ARRAY[J]=array[j+1]; Array[j+1]=temp; } } } //output An array of sorted rows for(inti = 0; i < Array.Length; i++) {System.out.println (array[i]); } }}
Select the sorting method: first find the smallest element in the unordered sequence,
To the starting position of the sorting sequence, and then continue to find the smallest element from the remaining unsorted elements, and then place it at the end of the sort sequence.
And so on until all elements are sorted
The code is as follows:
Public classTEST5 { Public Static voidMain (string[] args) {int[] array={1,3,5,2,6,7,4,9}; intmin=0;//Save Minimum element value subscript for(inti = 0; i < array.length-1; i++) {min=i;//find the lowest element in the array subscript for(intj = i+1; J < Array.Length; J + +) { if(array[min]>Array[j]) {min=j;//Save the decimal subscript } } //if the position of the I minimum number is not on I then Exchange if(i!=min) { inttemp=Array[i]; Array[i]=Array[min]; Array[min]=temp; } } for(inti = 0; i < Array.Length; i++) {System.out.println (array[i]); } }}
Insert Sort:
* It is done by constructing an ordered sequence, for unsorted data, in the sorted sequence from backward to forward scanning,
* Find the appropriate location and insert. From the backward forward scanning process, the sequencing elements need to be repeatedly moved backwards,
* Provides insert space for the latest elements
The code is as follows:
public class Test6 {
public static void Main (string[] args) {
Int[] array= {4,3,6,8,1,7};
for (int i = 0; i < Array.Length; i++) {
int temp=array[i];
Save the subscript.
int j=i;
while (J>0&&temp<array[j-1]) {
The above number overrides the number below it
ARRAY[J]=ARRAY[J-1];
j--;
}
array[j]=temp;//Inserting data
}
for (int i = 0; I <array.length; i++) {
System.out.println (Array[i]);
}
}
}
Common Array Sorting methods