1. Select sort
Basic idea: Select an index position of the element, and then compare with the following elements, if greater than the swap position, after the first round of comparison sort can draw the minimum value, and then use the same method to compare the remaining elements one by one.
Static void selectsort (int[] arry) { for (int times = 0; times <arry.length-1; t IMEs + +) { int minindex = times ; for (int i = times + 1; i < arry.length; i + +) { if(Arry[i] < =// switch position after each round end }}
2. Bubble sort
Basic idea: to the unsorted elements from start to finish comparing the adjacent two element size relationship, if greater than the swap position, after the first round of comparison sort can be obtained maximum value, and then use the same method to compare the remaining elements one by one.
Static void bubblesort (int[] arry) { int temp = 0; for (int j = 0; J < Arry.length-1; J + +) { for (int i = 1; i < A Rry.length-j; i++) { if(arry[i-1] >= arry[i-1]; Arry[i-1] = arry[i]; = temp; }}}
3, two-point search
When the data volume is very suitable for this method, the data needs to be sequenced when the binary method is used to find it. To better understand the algorithm, here is a game for example.
Guessing game: A friend lets you guess what he's thinking about a number from 1 to 100, and when you guess, he'll tell you one of the three results: you guessed bigger than he thought, or small, or guessed. In order to be able to guess with the fewest number of times, you must start guessing from 50. If he says you guessed small, then you can roll out which number is between 50 and 100, so immediately guess 75. But if he says it's a big guess, you can see which one says between 1 and 50, so guess 25 right away. So repeat, the range is getting smaller, until you guess.
Static intBinarySearch (int[] Arry,intkey) {intLow = 0;//Lowest Index intHigh = Arry.length;//Highest Index while(Low <= High) {//The lowest index cannot be higher than the highest index intMid = (low + high) >> 1;//Intermediate Index if(Arry[mid] < key)//key value is in the large value half areaLow = mid + 1;//set the lowest index to intermediate index +1 Else if(Arry[mid] > key)//key value is in the small value half areaHigh = mid-1;//set the highest index to intermediate index-1 Else returnmid; } return-1;
}
Several common algorithms for interviewing