按List中对像某属性排序算法4---JAVA简单选择排序
- - ITeye博客按List中Person类的age属性进行排序. * User: fuliguo
* Date: 12-7-29
* Time: 下午13:09
* To change this template use File | Settings | File Templates. MyPersonListSortTest_SimpleSelection类:.
按List中Person类的age属性进行排序。
Person类:
package com.flg; /** * Created with IntelliJ IDEA. * User: fuliguo * Date: 12-7-29 * Time: 下午13:09 * To change this template use File | Settings | File Templates. */ public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public Person(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
MyPersonListSortTest_SimpleSelection类:
package com.flg; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * 简单选择排序 * Created with IntelliJ IDEA. * User: fuliguo * Date: 12-7-29 * Time: 下午13:10 * To change this template use File | Settings | File Templates. */ public class MyPersonListSortTest_SimpleSelection { /** * 获得要测试的list * @return list */ public List getList(){ List list = new ArrayList<Person>() ; Random random = new Random(); int j; /* for(int i=0;i<10;i++){ j = 1+Math.abs(random.nextInt()%29); //30以内随机数 //System.out.println("随机数-->>"+j); list.add(new Person("张"+j,j)); //年龄随机增长 } */ list.add(new Person("张6",6)); list.add(new Person("张9",9)); list.add(new Person("张12",12)); list.add(new Person("张18",18)); list.add(new Person("张16",16)); list.add(new Person("张20",20)); list.add(new Person("张3",3)); list.add(new Person("张27",27)); list.add(new Person("张30",30)); list.add(new Person("张30",30)); return list; } /** * 控制台输出遍历list对像顺序 * @param list */ public void printList(List list) { Person p; for(int i=0;i<list.size();i++){ p = (Person)list.get(i); System.out.println(p.getName()+"---"+p.getAge()); } } private void swap(List list,int i,int j){ Person p1 = (Person)list.get(i); Person p2 = (Person)list.get(j); list.set(i,p2); list.set(j,p1); } /** * 对list进行排序:简单选择排序 * * 简单选择排序就是通过n-i次比较,从n-i+1个记录中选出最小的记录, * 并和第i(1<=i<=n)个记录交换 * 和冒泡相比,减少了比较次数 * 比较次数为:n-1+n-2+...1=n(n-1)/2次 * * @param list */ public void listSort1(List<Person> list,boolean isAsc){ int comparaCount = 0;//统计比较次数 int swapCount = 0;//统计交换次数 Person p1,p2; int i,j,min; for( i=0;i<list.size();i++){ min = i; for( j=i+1;j<list.size()-1;j++){ p1 = (Person)list.get(min); p2 = (Person)list.get(j); comparaCount ++;//统计比较次数 if(!(isAsc^(p1.getAge()>p2.getAge()))){ //非运算,非真即假 min = j; } } if(i!=min){ swap(list,i,min);//i和min位置上的对象交换 swapCount++;//统计交换次数 } } //若使用统计比较数comparaCount和统计交换次数swapCount //要改成固定方式生成对象,针对同一组对象进行比较, // 否则随机生成,前后没有可比性 System.out.println("listSort1===>comparaCount:"+comparaCount+"||swapCount:"+swapCount); } public static void main(String [] args) throws InvocationTargetException, IllegalAccessException { MyPersonListSortTest_SimpleSelection m = new MyPersonListSortTest_SimpleSelection(); //排序isAsc:true 升序,false:降序 boolean isAsc = true; List list = m.getList(); System.out.println("==========排序前========="); m.printList(m.getList()); m.listSort1(list, isAsc); //排序isAsc:true 升序,false:降序 System.out.println("listSort1==========排序后========="); m.printList(list); } }