![]() |
#2
林月儿2015-04-29 17:11
package ninthpackage;
public class mathAlgorithm { public static void main(String[] args) { //产生长度为20的数组,并随机产生里面的数值。 MaoPaoPaiXu bubble; int a[]= new int[20]; for(int k=0;k<a.length;k++) { a[k]=(int)(100*Math.random()); } bubble=new MaoPaoPaiXu(a); //如何采用下面冒泡排序的方法进行排序 int b[]=bubble.sorted(); for(int i:b) System.out.print(" "+i); System.out.println(); //如何采用下面定义的折半查找法在上面的方法中返回的排序过后的数组中查找其中n=68这个数的位置。 int n=68; int found=new ZheBanChaZhaoFa().bisearch(b, n); if(found!=-1) System.out.println("place:"+found); else System.out.println("not found!"); } } //冒泡排序 /*随机产生20个0~100的随机整数,用冒泡排序的方法按其从小到达的顺序排列排列 *并且对排序前后的数组按照每行10个数方式进行输出 * * * */ class MaoPaoPaiXu { int b[]; public MaoPaoPaiXu(int a[]){ int count=a.length; for(int i=0;i<count;i++){ for(int j=count-1;j>i;j--){ if (a[j]<a[j-1]){ int temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } } } System.out.println("20个随机数按照冒泡排序由小到大排列"); for(int i=0;i<count;i++){ System.out.println(a[i]); } //如何返回已经排过序的数组a b=a; } public int[] sorted(){ return b; } } //折半查找法 /*在MaoPaoPaiXu的方法的基础上,采用这般查找法查找给定的数在排序后的数组中的位置 * 如果查到该数,输出信息:X在数组的第Y个位置上的程序。 * 其中,X代表给定的数,Y代表该数在数组中的位置。 * * */ class ZheBanChaZhaoFa { public static int bisearch(int a[],int n){ int low =0; int high=a.length-1; int mid =(low+high)/2; if(n>a[high]||n<a[low]) return -1; while(low<=high){ if(a[low]== n) return low; else if(a[high]==n) return high; else if(a[mid]==n) return mid; else{ if(n>a[mid]){ low=mid+1; mid=(low+high)/2; } else{ high=mid-1; low=low+1; } } } return -1; } } |
我是用Eclipse编的
把三个class都放在一个package下。
我想在mathAlgorithm中定义一个长度为20的数组,并随机产生20个0~100的随机整数。
然后调用另一个class文件的冒泡排序的方法来实现排序,并返回mathAlgorithm.class中。
然后在mathAlgorithm.class中,再调用折中查找的class文件中的方法,实现查找?
package ninthpackage;
public class mathAlgorithm {
public static void main(String[] args) {
//产生长度为20的数组,并随机产生里面的数值。
int a[]= new int[20];
for(int k=0;k<a.length;k++)
{
a[k]=(int)(100*Math.random());
}
int n=68;
//如何采用下面冒泡排序的方法进行排序
//如何采用下面定义的折半查找法在上面的方法中返回的排序过后的数组中查找其中n=68这个数的位置。
}
}
//冒泡排序
package ninthpackage;
/*随机产生20个0~100的随机整数,用冒泡排序的方法按其从小到达的顺序排列排列
*并且对排序前后的数组按照每行10个数方式进行输出
*
*
*
*/
public class MaoPaoPaiXu {
public MaoPaoPaiXu(int a[]){
//随机产生20个0~100之间的随机整数
/*int a[]= new int[20];
for(int k=0;k<a.length;k++)
{
a[k]=(int)(100*Math.random());
}
*/
int count=a.length;
for(int i=0;i<count;i++){
for(int j=count-1;j>i;j--){
if (a[j]<a[j-1]){
int temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
System.out.println("20个随机数按照冒泡排序由小到大排列");
for(int i=0;i<count;i++){
System.out.println(a[i]);
}
//如何返回已经排过序的数组a
}
}
//折半查找法
package ninthpackage;
/*在MaoPaoPaiXu的方法的基础上,采用这般查找法查找给定的数在排序后的数组中的位置
* 如果查到该数,输出信息:X在数组的第Y个位置上的程序。
* 其中,X代表给定的数,Y代表该数在数组中的位置。
*
*
*/
public class ZheBanChaZhaoFa {
public static int bisearch(int a[],int n){
int low =0;
int high=a.length-1;
int mid =(low+high)/2;
if(n>a[high]||n<a[low])
return -1;
while(low<=high){
if(a[low]== n)
return low;
else if(a[high]==n)
return high;
else if(a[mid]==n)
return mid;
else{
if(n>a[mid]){
low=mid+1;
mid=(low+high)/2;
}
else{
high=mid-1;
low=low+1;
}
}
}
return -1;
}
}