如何編寫代碼實(shí)現(xiàn)二分查找與冒泡排序

這篇文章主要介紹“如何編寫代碼實(shí)現(xiàn)二分查找與冒泡排序”,在日常操作中,相信很多人在如何編寫代碼實(shí)現(xiàn)二分查找與冒泡排序問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何編寫代碼實(shí)現(xiàn)二分查找與冒泡排序”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、虛擬空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、黃平網(wǎng)站維護(hù)、網(wǎng)站推廣。

public class BinarySearch {

    /**
     *
     * @param arr 升序的數(shù)組
     * @param start
     * @param end
     * @param findVal
     * @return
     */
    public static int binarySearch(int[] arr,int start,int end,int findVal){
       int mid = (start+end)/2;
       int midVal = arr[mid];
       if(midVal==findVal){
           return mid;
       }else if(midVal<findVal){
           return binarySearch(arr,mid+1,end,findVal);
       }else {
           return binarySearch(arr,start,mid-1,findVal);
       }
    }

    public static void main(String[] args) {
        int[] arr = {1,10,12,20,100};
        System.out.println(binarySearch(arr,0,arr.length-1,1));
        System.out.println(binarySearch(arr,0,arr.length-1,20));
    }

}
public class SortTest {

    public static void bubbleSortAsc(int[] arr) {
        int N = arr.length;
        for (int i = 0; i < N - 1; i++) {
            for (int j = 0; j < N - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {2, 1, 2, 10, 7, 5};
        bubbleSortAsc(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

}

到此,關(guān)于“如何編寫代碼實(shí)現(xiàn)二分查找與冒泡排序”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

當(dāng)前標(biāo)題:如何編寫代碼實(shí)現(xiàn)二分查找與冒泡排序
轉(zhuǎn)載來于:http://muchs.cn/article0/jchjoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、品牌網(wǎng)站建設(shè)營(yíng)銷型網(wǎng)站建設(shè)、云服務(wù)器、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)