驼峰排序-java

此排序方法由冒泡算法变种而来,原理就是简单的映射了地址.

private static List<Integer> SortList(List<Integer> list) {        
    int count = list.size();        
    int middleIndex = count / 2;
    for (int x = 0; x < count; x++) {            
    	for (int y = count - 1; y > x ; ) {                
    		int left = count != 2 ? middleIndex : 0 + (y % 2 == 0 ? y / -2 : y / 2 + 1);                
    		y--;                
    		int right = count != 2 ? middleIndex : 0 + (y % 2 == 0 ? y / -2 : y / 2 + 1);                                						 
    		if (list.get(right) < list.get(left)) {                    
    			int temp = list.get(left);                    
    			list.set(left,list.get(right));                    
    			list.set(right,temp);                
    		}           
    	}        
    }        
    return list;    
}

操作结果:
驼峰排序-java_第1张图片

你可能感兴趣的:(算法)