List> 的快速排序

时间:2022-08-30 14:26:58
      看了看快速排序的实现算法,正好这两天有用到List<Map<String,Object>> 的排序,so 写了一个关于List<Map<String,Object>>的快速排序算法 测试下  OK的
public static void sort(List<Map<String, Object>> list,String key, int low1, int high1){
	    int low = low1;
	    int high = high1;
	    double x = (Double) list.get(low).get(key);
	    Map<String,Object> temp = list.get(low);
	    if(low1<high1){
	    	while(low<high){
	    		while(low<high&&(Double)list.get(high).get(key)>=x){
	    			high--;
	    		}
	    		list.set(low, list.get(high));
	    		while(low < high && (Double)list.get(low).get(key)<=x){
	    			low++;
	    		}
	    		list.set(high, list.get(low));
		    }
	    	list.set(low, temp);
	    	if(low>low1) sort(list,key,low1,low-1);
			if(high<high1) sort(list,key,low+1,high1);
	    }
	}
	
	public static void main(String[] args){
		try {
			List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> map1 = new HashMap<String, Object>();
			Map<String, Object> map2 = new HashMap<String, Object>();
			Map<String, Object> map3 = new HashMap<String, Object>();
			Map<String, Object> map4 = new HashMap<String, Object>();
			Map<String, Object> map5 = new HashMap<String, Object>();
			Map<String, Object> map6 = new HashMap<String, Object>();
			Map<String, Object> map7 = new HashMap<String, Object>();
			Map<String, Object> map8 = new HashMap<String, Object>();
			Map<String, Object> map9 = new HashMap<String, Object>();

			map.put("number", 8.0);
			map1.put("number", 2.0);
			map2.put("number", 3.0);
			map3.put("number", 3.0);
			map4.put("number", 5.0);
			map5.put("number", 45.0);
			map6.put("number", 34.0);
			map7.put("number", 23.0);
			map8.put("number", 13.0);
			map9.put("number", 30.0);
			
			list.add(map);
			list.add(map1);
			list.add(map2);
			list.add(map3);
			list.add(map4);
			list.add(map5);
			list.add(map6);
			list.add(map7);
			list.add(map8);
			list.add(map9);
			sort(list,"number",0,list.size()-1);
			//listSort(list,"number",0,list.size()-1);
			System.out.println("list::"+list);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}