1, the choice of sorting method
public class Selectionsort {public
static void Main (string[] args) {
double[] numbers={3,2,5,4,6,8,1,7,9,0};< c2/>//Call Selectionsort Select the Sort method
Selectionsort (numbers);
for (int i=0;i<numbers.length;i++)
System.out.print (numbers[i]+ " ");
System.out.println ();
Char[] chars={' B ', ' A ', ' e ', ' d ', ' C '};
Call the sort Sort method
Java.util.Arrays.sort (chars) in the Java.util.Arrays class;
for (int i=0;i<chars.length;i++)
System.out.print (chars[i]+ " ");
}
Select the Sort method (in ascending order), first find the maximum number placed at the end of the list, and then find the maximum number in the remaining number, and put it in the last few minutes. Public
static void Selectionsort (double[] list) { for
(int i=list.length-1;i>=1;i--) {
double currentmax=list[0];
int currentmaxindex=0;
for (int j=1;j<=i;j++) {
if (List[j]>currentmax) {
currentmax=list[j];
Currentmaxindex=j
}
}
if (currentmaxindex!=i) {
list[currentmaxindex]=list[i];
List[i]=currentmax}}}}
Run Result:
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
A b c d E
2, two-point search method
public class BinarySearch {public
static void Main (string[] args) {
//sorted numbers array
int[] numbers={ 1,2,3,4,5,6,7,8,9};
System.out.print (BinarySearch (numbers,3));
System.out.println ();
System.out.print (BinarySearch (numbers,10));
System.out.println ();
Call Java.util.Arrays's BinarySearch binary lookup method
System.out.print (Java.util.Arrays.binarySearch (Numbers, 7));
Two-point search method, if the array is sorted
//If the keyword is in the array, it returns the subscript, otherwise (-insertion point-1), the insertion point is defined as the point where the key is inserted into the array: the first element that is greater than this key is indexed public
static int BinarySearch (int[] list,int key) {
int low=0;
int high=list.length-1;
while (Low<=high) {
int mid= (Low+high)/2;
if (Key<list[mid])
high=mid-1;
else if (Key==list[mid]) return
mid;
else low=mid+1;
}
Return-low-1
}
}
Run Result:
2
-10
6