`
canofy
  • 浏览: 829734 次
  • 性别: Icon_minigender_1
  • 来自: 北京、四川
社区版块
存档分类
最新评论

插入 选择 冒泡排序

阅读更多
package com.canofy.structure;

import org.apache.log4j.Logger;

public class Sort {

	static Logger logger=Logger.getLogger(Sort.class);
	//插入排序法
	 private static void insertSort(int[] arraySort) {    
	        int in, out;    
	        for (out = 1; out < arraySort.length; out++) {    
	            int tmp = arraySort[out];    	               
	            for(in=out;in>0;in--){
	            	if(tmp<arraySort[in-1]){
	            		arraySort[in]=arraySort[in-1];
	            	}else{
	            		break;
	            	}
	            }
//	            in = out;
//	            while (in > 0 && tmp <= arraySort[in - 1]) {    
//	                arraySort[in] = arraySort[in - 1];    
//	                in--;    
//	            }    
	   
	            arraySort[in] = tmp;    
	                
	        }    
	    }    
	//选择排序法
	private static void selectSort(int[] arraySort) {    
        int in, out;    
        for (out = 0; out < arraySort.length-1; out++) {    
            int min = out;    
            for (in = out+1; in < arraySort.length; in++) {    
                if (arraySort[in] < arraySort[min]) {  //选择最小的数  
                    min = in;    
                }    
            }    
            swap(out, min, arraySort);    
               
        }    
    } 
	//冒泡排序法
	 private static void bubbleSortBigger(int[] arraySort) {    
	        int in, out;    
	        for (out = arraySort.length - 1; out > 0; out--) {    
	            for (in = 0; in < out; in++) {    
	                if (arraySort[in] > arraySort[in + 1]) {    
	                    //logger.info("arraySort"+in+":"+arraySort[in]);    
	                    swap(in, in + 1, arraySort);    
	                }    
	            }    
	        }    
	    }    
	   
	    private static void swap(int a, int b, int[] arraySort) {    
	        int tmp = arraySort[a];    
	        arraySort[a] = arraySort[b];    
	        arraySort[b] = tmp;    
	    }  
	
	public static void main(String[] args) {
		Logger logger=Logger.getLogger(Sort.class);
		Sort sort=new Sort();
		int[] arrayBub = { 43, 36, 11, 10, 29, 58, 15, 9 }; 
		insertSort(arrayBub); 
        for(int i=0;i<arrayBub.length;i++){
        	logger.info("arrayBub"+i+":"+arrayBub[i]);
        }		

	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics