java 冒泡排序

来源:互联网 发布:系统优化方案 编辑:程序博客网 时间:2024/05/16 08:49
public class BubbleSort {
    /**
     * 冒泡排序
     * @param strVoid
     */
    
    public void bubbleSort(String[] strVoid)
    {
        int j = strVoid.length-1;
        int lastExchange = j;
        int k =0;
        String temp;        
        while(k<strVoid.length)
        {
            for(int i=0;i<j;i++)
            {
                  if(strVoid[i].compareTo(strVoid[i+1])>0)
                  {
                      temp = strVoid[i];
                      strVoid[i] = strVoid[i+1];
                      strVoid[i+1] = temp;
                      lastExchange = i;
                  }
            }
            j = lastExchange;            
            k++;
        }
        return;
    }
    
    
    public static void main(String[] args){
        String[] strVoid=new String[]{"11","66","22","0","55","22","0","32"};
        BubbleSort sort  = new BubbleSort();
        
        sort.bubbleSort(strVoid);
        
        for(int i=0;i<strVoid.length;i++){
            System.out.print(strVoid[i]+" ");
            }        
        
        }


原创粉丝点击