<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    【原】算法:冒泡排序+0后置算法

     

    /*
     * @(#)bubbleSort.java    1.01 09/11/24
     *
     * Copyright 2009 Three Stone.company, Inc. All rights reserved.
     * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     * this function implement as(-9 -4 0 7 4 -2 0 3 8)to(-9 -4 -2 3 4 7 8 0 0)sort code.
     
    */
    package edu.sort;

    public class bubbleSort {

        
    /**
         * 
    @see
         * 
    @param args
         * 
    @author GL
         * @category:實現(xiàn)隨機給出帶有正負(fù)整數(shù)的數(shù)組由小到大的排序(負(fù)數(shù)->正數(shù)->0)
         
    */
        
    public static void main(String[] args) {
            
    // TODO Auto-generated method stub
            int array[] = new int[20];//初始化數(shù)組
            for (int i = 0; i < array.length; i++) {
                
    int number = (int) (Math.random() * 10 * (Math.pow(-1, (int) (Math
                        .random() 
    * 10))));//初始化隨機正負(fù)數(shù)
                array[i] = number;
            }
            System.out.println(
    "初始序列為:");
            printNumbers(array);
            array 
    = bubble(array);//排序方法調(diào)入
            System.out.println("最后的排序為:");
            printNumbers(array);

        }
        
    /**
         * 
    @param array[]
         * 
    @author GL
         * @category 冒泡排序并將所有0置后
         
    */
        
    public static int[] bubble(int array[]) {
            
    int length = array.length;
            
    for (int i = 0; i < length; i++) {
                
    for (int j = length - 1; j > i; j--) {
                    
    if (array[j] < array[j - 1]) {//冒泡原理"大小"換成"小大"
                        int temp = array[j];
                        array[j] 
    = array[j - 1];
                        array[j 
    - 1= temp;
                    }
                }
            }
            System.out.println(
    "冒泡排序結(jié)果為:");
            printNumbers(array);
            
            
    int count = 0;//記錄數(shù)組中0的個數(shù)
            int start = 0;//記錄0在數(shù)組中的初始出現(xiàn)位置
            for (int i = 0; i < array.length; i++) {
                
    if (array[i] == 0 && array[i - 1!= 0) {
                    start 
    = i + 1;
                    count 
    = 1;
                    System.out.println(
    "" + start + "位出現(xiàn)0");
                } 
    else if (array[i] == 0 && array[i - 1== 0) {
                    count
    ++;
                }
            }
            System.out.println(
    "0的個數(shù)為:" + count + "");
            
    if (start != 0 && count != 0) {//按照0的初始出現(xiàn)位置與0的個數(shù)順次移動元素
                for (int i = (start - 1); i < array.length; i++) {
                    
    if (i < (array.length - count)) {
                        array[i] 
    = array[i + count];
                    } 
    else {                //最后將數(shù)組后count位置0
                        array[i] = 0;
                    }
                }
            }
            
    return array;
        }
        
        
    /**
         * 
    @param array[]
         * 
    @author GL
         * @category 輸出情況討論
         
    */
        
    public static void printNumbers(int array[]) {
            
    for (int i = 0; i < array.length; i++) {
                
    if ((((i + 1% 10== 1&& array[i] >= 0) {//首位
                    System.out.print(" " + array[i] + "  ");
                } 
    else if ((i + 1% 10 != 0 && array[i + 1< 0) {//非尾負(fù)數(shù)
                    System.out.print(array[i] + " ");
                } 
    else if ((i + 1% 10 != 0 && array[i + 1>= 0) {//非尾正數(shù)
                    System.out.print(array[i] + "  ");
                } 
    else {
                    System.out.println(array[i]);
    //尾數(shù)
                }
            }
        }
    }

    posted on 2009-11-24 14:15 龍櫻 閱讀(315) 評論(0)  編輯  收藏 所屬分類: j2se類


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    <2009年11月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(3)

    隨筆分類(13)

    隨筆檔案(13)

    文章分類(1)

    文章檔案(1)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲欧洲精品在线| 国产免费黄色大片| 亚洲成AV人片在线观看无| 特黄特色的大片观看免费视频| 妞干网手机免费视频| 另类专区另类专区亚洲| 日韩一级视频免费观看| 亚洲国产成人精品无码区二本| 国产精品久久亚洲一区二区| 国产aa免费视频| 成人嫩草影院免费观看| 亚洲色WWW成人永久网址| 免费国产午夜高清在线视频| 亚洲天堂男人天堂| 成人免费激情视频| 亚洲精品网站在线观看不卡无广告| 老妇激情毛片免费| 亚洲中文久久精品无码ww16| 久久免费动漫品精老司机| 亚洲乱码在线播放| 日本免费福利视频| 香蕉视频在线免费看| 亚洲黄色网址大全| 精品国产免费观看一区| a级毛片免费观看网站| 亚洲AV无码一区二区二三区软件 | 一级做a爱过程免费视频高清| 青娱分类视频精品免费2| 亚洲成a∨人片在无码2023| 亚洲人成电影网站国产精品| 国产精品免费看久久久| 亚洲一线产区二线产区区| 又粗又硬又黄又爽的免费视频| 中国在线观看免费的www| 亚洲AV无码精品蜜桃| 亚洲成a人片在线观看日本麻豆| 最新亚洲卡一卡二卡三新区| 亚洲国产高清在线一区二区三区| 日本人成在线视频免费播放| 亚洲熟妇无码一区二区三区| 亚洲精品乱码久久久久久按摩|