zoukankan      html  css  js  c++  java
  • 冒泡排序高逼格

    import java.util.Comparator;
    
    /**
     * 排序器接口(策略模式: 将算法封装到具有共同接口的独立的类中使得它们可以相互替换)
     * 
     * @author Chengpeng
     *
     */
    interface Sorter {
    
    	/**
    	 * 排序
    	 * 
    	 * @param list
    	 *            待排序的数组
    	 */
    	public <T extends Comparable<T>> void sort(T[] list);
    
    	/**
    	 * 排序
    	 * 
    	 * @param list
    	 *            待排序的数组
    	 * @param comp
    	 *            比较两个对象的比较器
    	 */
    	public <T> void sort(T[] list, Comparator<T> comp);
    }
    
    /**
     * 冒泡排序
     * 
     * 
     *
     */
    public class BubbleSorter implements Sorter {
    
    	@Override
    	public <T extends Comparable<T>> void sort(T[] list) {
    		boolean swapped = true;
    		for (int i = 1, len = list.length; i < len && swapped; ++i) {
    			swapped = false;
    			for (int j = 0; j < len - i; ++j) {
    				if (list[j].compareTo(list[j + 1]) > 0) {
    					T temp = list[j];
    					list[j] = list[j + 1];
    					list[j + 1] = temp;
    					swapped = true;
    				}
    			}
    		}
    	}
    
    	@Override
    	public <T> void sort(T[] list, Comparator<T> comp) {
    		boolean swapped = true;
    		for (int i = 1, len = list.length; i < len && swapped; ++i) {
    			swapped = false;
    			for (int j = 0; j < len - i; ++j) {
    				if (comp.compare(list[j], list[j + 1]) > 0) {
    					T temp = list[j];
    					list[j] = list[j + 1];
    					list[j + 1] = temp;
    					swapped = true;
    				}
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		Person p1 = new Person(10);
    		Person p2 = new Person(20);
    		Person p3 = new Person(80);
    		Person p4 = new Person(40);
    		Person[] list = { p1, p2, p3, p4 };
    //		new BubbleSorter().sort(list, new PersonComparator());
    		
    		//or
    		new BubbleSorter().sort(list, new Comparator<Person>() {
    
    			@Override
    			public int compare(Person o1, Person o2) {
    				return o1.score - o2.score;
    			}
    
    		});
    		for (int i = 0; i < list.length; i++) {
    			System.out.println(list[i]);
    		}
    	}
    
    }
    
    class Person {
    	int score;
    
    	public Person(int score) {
    		this.score = score;
    	}
    
    	public String toString() {
    		return String.valueOf(this.score);
    	}
    
    }
    
    class PersonComparator implements Comparator<Person> {
    
    	@Override
    	public int compare(Person o1, Person o2) {
    
    		return o2.score - o1.score;
    	}
    
    }
    
  • 相关阅读:
    OC面向对象—封装
    设计模式之类关系
    理性:中国别一厢情愿救俄罗斯(转)
    Mockito--完整功能介绍(转)
    从陌陌上市看BAT的移动保卫战(转)
    This exception may occur if matchers are combined with raw values
    RepositoryClassLoader.java
    搭建你的持续集成server
    mysql中怎样查看和删除唯一索引
    Android中Context具体解释 ---- 你所不知道的Context
  • 原文地址:https://www.cnblogs.com/chengpeng15/p/5878987.html
Copyright © 2011-2022 走看看