程序的执行入口:
import java.util.*;
public class Sort {
public static void main(String[] args ){
int[] a={12,45,67,89,123,-45,67};
int N=a.length;
Scanner scanner =new Scanner(System.in);
System.out.println("请输入一个整数判断是否在相应的数组中:");
int number=scanner.nextInt();
Sort1 b=new Sort1();
b.sort(N,a);
Search d=new Search();
int count=d.search(number,N,a);
if(count>N/2)
System.out.printf("%d不在数组中",number);
else
System.out.printf("%d不在数组中",number);
}
}
程序的search部分:
public class Search {
int search(int number,int N,int[] a){
int count=0;
int start=0,end,middle;
end=N;
middle=(start+end)/2;
while(number!=a[middle]){
if(number>a[middle])
start=middle;
else if(number<a[middle])
end=middle;
middle=(start+end)/2;
count++;
if(count>N/2)
break;
}
return count;
}
}
选择法排序:
public class Sort1 { //选择法排序
void sort(int N,int[] a){
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++){
if(a[j]<a[i]){
int t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}
}
}
判断一个数是否在相应的数组中:
import java.util.*;
public class Sort {
public static void main(String[] args ){
int[] a={12,45,67,89,123,-45,67};
int N=a.length;
Scanner scanner =new Scanner(System.in);
System.out.println("请输入一个整数判断是否在相应的数组中:");
int number=scanner.nextInt();
Sort1 b=new Sort1();
b.sort(N,a);
Search d=new Search();
int count=d.search(number,N,a);
if(count>N/2)
System.out.printf("%d不在数组中",number);
else
System.out.printf("%d不在数组中",number);
}
}
心得体会:
java程序只有一个程序执行入口,从建立对象到执行调用对象,时时刻刻都是对象。从开始就要养成建立工程的感觉,慢慢来,就会接近实际的项目!
坚持就是胜利@!