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

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);  
        }  
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值