选择排序


empty
public class Paixu {

	public static void main(String[] args) {

		//选择排序
		int[] a = {1,3,5,7,6,4,2};
		
		for (int i = 0; i < a.length-1 ; i++) {
			for (int j = 0; j < a.length-1-i ; j++) {
				if(a[j] < a[j+1]){
					int k = a[j];
					a[j] = a[j+1];
					a[j+1] = k;
				}
			}
		}
		
		System.out.println("排序后:");
		for (int i : a) {
			System.out.print(i+" ");
		}
		
	}

}
empty
public class ListDemo {

	public static void main(String[] args) {

		List list = new ArrayList();
		list.add(new Employee("Tom",24,3000));
		list.add(new Employee("Jack",24,4000));
		list.add(new Employee("Lily",26,8000));
		list.add(new Employee("Jim",25,6000));
		list.add(new Employee("Dabao",26,8000));
		list.add(new Employee("Xiaobao",27,6000));
		
		for (int i = 0; i < list.size() -1; i++) {
			for (int j = 0; j < list.size() - 1 - i; j++) {
				 Employee e1 = list.get(j);
				 Employee e2 = list.get(j+1);
				
				 //比工资
				if(e1.getSalary() < e2.getSalary()){
					list.set(j, e2);
					list.set(j+1, e1);
				}else if(e1.getSalary() == e2.getSalary()){//当工资相等
					
					//比年龄
					if(e1.getAge() < e2.getAge()){
						list.set(j, e2);
						list.set(j+1, e1);
					}else if(e1.getAge() == e2.getAge()){//当年龄相等
						
						if(e1.getName().compareTo(e2.getName()) > 0){
							list.set(j, e2);
							list.set(j+1, e1);
						}
					}
					
				}
				
			}
			
		}
		
		for (Employee e : list) {
			System.out.println(e.getName()+":年龄"+e.getAge()+"岁,收入 "+e.getSalary()+"元");
		}
		
		
	}

}

你可能感兴趣的:(选择排序)