java 排序算法(1)

冒泡排序:

public class BubbleSort { public static void main(String[] args) { int []array={63,4,24,1,3,15}; BubbleSort sorter=new BubbleSort(); }

public void sort(int []array){ for(int i=1;i<array.length;i++){ for (int j=0;j<array.length-i;j++){ if(array[j]>array[j+1]){ int temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; }

 

 

对象排序:

public class Test { public static A a1 = new A("001", "a", "哈哈~", 7); public static A a2 = new A("001", "c", "哈哈~", 9); public static A a3 = new A("001", "b", "哈哈~", 3); public static A a4 = new A("000", "d", "哈哈~", 6); public static void main(String[] args) { A[] voA = new A[] { a1, a2, a3, a4 }; java.util.Arrays.sort(voA, new Mycomparator()); A[] newA = new A[voA.length]; for (int i = 0; i < voA.length; i++) System.out.println("[" + i + "]:" + voA[i].A1 + " " + voA[i].A2 + " " + voA[i].A3 + " " + voA[i].A4); }}class A { String A1; String A2; String A3; int A4; public A(String oA1, String oA2, String oA3, int oA4) { this.A1 = oA1; this.A2 = oA2; this.A3 = oA3; this.A4 = oA4; }}class Mycomparator implements java.util.Comparator { public int compare(Object o1, Object o2) { A a1 = (A) o1; A a2 = (A) o2; if(a1.A1.compareTo(a2.A1)!=0) { return a1.A1.compareTo(a2.A1); } else if(a1.A2.compareTo(a2.A2)!=0) { return a1.A2.compareTo(a2.A2); } else if(a1.A3.compareTo(a2.A3)!=0) { return a1.A3.compareTo(a2.A3); } else { return a1.A4 >a2.A4?1:a1.A4==a2.A4?0:-1; } }}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值