首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

普普通通排序法,很杂乱(第一次修改,未注解)

2012-12-22 
普通排序法,很杂乱(第一次修改,未注解)package fifthsa public class Test1 { /** * @param args */ publ

普通排序法,很杂乱(第一次修改,未注解)

package fifthsa;

public class Test1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("创建一个二十个元素的随机的数组");
int? Nu[]=new int[20];
MakeArray makeArray= new MakeArray();
makeArray.makeArray(Nu); //用Make类为函数随机赋值


System.out.println("第一种方法排列");
rankWay1 rank1= new rankWay1();
rank1.way1(Nu);
PrintOut printNu=new PrintOut();
printNu.printOut(Nu);

System.out.println("第二种方法排列");
rankWay2 rank2= new rankWay2();
rank2.way2(Nu);
printNu.printOut(Nu);

System.out.println("第三种方法排列");
rankWay3 rank3= new rankWay3();
rank3.way3(Nu);
printNu.printOut(Nu);

System.out.println("第四种方法排列");
rankWay4 rank4= new rankWay4();
rank4.way4(Nu);
printNu.printOut(Nu);



}

}
//对数组随机赋0-10000的值
class MakeArray{
public void makeArray(int Nu[]){
for (int i = 1; i <=Nu.length; i++ ) {
// Math.random() 方法是随机产生一个正号的double型的0-1的数
int t? =(int)(Math.random()*10000);? //获得的随机数转成 整型
Nu[i]=t;
}
}
}
//方法1的类
class rankWay1{
??? public void? way1( int? Nu[]){
??? int temp=0;//定义一个中转量
for (int i = 0; i < Nu.length-1; i++) {
//内层循环,开始逐个比较
for (int j = 0; j < Nu.length-i-1; j++) {
if (Nu[j]>Nu[j+1]) {
temp=Nu[j];
Nu[j]=Nu[j+1];
Nu[j+1]=temp;
}
}
? }
?? }
??? }
//方法2的类
class rankWay2{
public void way2(int Nu[]){
???? for (int i = 0; i < Nu.length; i++) {
int temp=0;
for (int j = i+1; j < Nu.length; j++) {
if(Nu[i]>Nu[j]){
temp=Nu[i];
Nu[i]=Nu[j];
Nu[j]=temp;

??????? }
}
????? }
??????? }
???? }
//方法3的类
class rankWay3{
public void way3(int Nu[]) {
for (int i = 2; i <Nu.length; i++) {
int tempNu=i;
int theOne =Nu[i];
while (theOne>=0 && Nu[tempNu]<Nu[tempNu-1] ) {
int temp=0;
temp=Nu[tempNu];
Nu[tempNu]= Nu[tempNu-1];
Nu[tempNu-1]=temp;
tempNu--;
}
}
? }
}
//方法4的类
class rankWay4{
public void way4(int Nu[]){
for (int i = 2; i < Nu.length; i++) {
int theSecond=Nu[i];
int theFirstNu=i-1;
while(theFirstNu>0&&Nu[theFirstNu]>theSecond) {
Nu[theFirstNu+1]=Nu[theFirstNu];
theFirstNu--;
???? }
Nu[theFirstNu+1]= theSecond;
}
}
?? }
//依次打印数组的类
class PrintOut{
public?? void? printOut(int Nu[]) {
for (Integer i:Nu) {??
???????????? System.out.println(i);??
??????? }??
}
}

?

热点排行