排序演算法 Java

來源:互聯網
上載者:User

標籤:一個   選擇   temp   key   有序   merge   select   ==   直接插入排序   

 //1、冒泡排序    public void bubbleSort(int[] arr){        for(int i=0;i<arr.length;i++){            for(int j=0;j<arr.length-i-1;j++){                if(arr[j] > arr[j+1]){                    int temp = arr[j];                    arr[j] = arr[j+1];                    arr[j+1] = temp;                }            }        }        System.out.println(arr);    }    //2、快排    public void quick(int[] arr){        quickSort(arr,0,arr.length-1);        System.out.println();    }    private void quickSort(int[] arr,int low,int high){       //每次選擇一個基準元素,比該元素小的放在左邊,小的放右邊,分成兩部分,然後遞迴兩部分        if(low < high){            int privotLoc = quickSortHelper(arr,  low,  high);  //將表一分為二            quickSort(arr,  low,  privotLoc -1);          //遞迴對低子表遞迴排序            quickSort(arr,   privotLoc + 1, high);        //遞迴對高子表遞迴排序        }    }    private int quickSortHelper(int[] arr,int low,int high){        int privotKey = arr[low];        while(low < high){            while(low < high  && arr[high] >= privotKey) high--;        //從high 所指位置向前搜尋,至多到low+1 位置。將比基準元素小的交換到低端            int temp = arr[low];            arr[low] = arr[high];            arr[high] = temp;            while(low < high  && arr[low] <= privotKey ) ++low;            temp = arr[low];            arr[low] = arr[high];            arr[high] = temp;        }        return low;     //返回分割點    }    //3、歸併排序    public void mergeSort(int[] arr){       //將數組分成兩個部分,遞迴直到每個部分都只有一個元素,然後合并相鄰的兩個部分        split(arr,0,arr.length-1);        System.out.println();    }    private void split(int[] arr,int start,int end){        if(start == end)        //分割到每個單元素為一組            return;        int mid = (start + end) / 2;        split(arr,start,mid);        split(arr,mid+1,end);        merge(arr,start,mid,mid+1,end);    }    private void merge(int[] arr,int lowStart,int lowEnd,int highStart,int highEnd){        int[] temp = new int[(lowEnd-lowStart)+(highEnd-highStart) +2];        int i=lowStart,j=highStart,k=0;        while(i<=lowEnd&&j<=highEnd){            if(arr[i] < arr[j]){                temp[k++] = arr[i];                i++;            }else{                temp[k++] = arr[j];                j++;            }        }        while(i<=lowEnd){            temp[k++] = arr[i++];        }        while (j<=highEnd){            temp[k++] = arr[j++];        }        for(int m=0;m<temp.length;m++){            arr[lowStart + m] = temp[m];        }    }    //4、直接插入排序    public void insertSort(int[] arr){     //直接按順序插入        if(arr.length==0) return;        for (int i=1;i<arr.length;i++) {            if(arr[i] < arr[i-1]){                int j= i-1;                int x = arr[i];        //複製為哨兵,即儲存待排序元素                arr[i] = arr[i-1];           //先後移一個元素                while(j>=0 && x < arr[j]){  //尋找在有序表的插入位置                    arr[j+1] = arr[j];                    j--;         //元素後移                }                arr[j+1] = x;            }        }        System.out.println();    }    //5、簡單選擇排序    /***     * 在要排序的一組數中,選出最小(或者最大)的一個數與第1個位置的數交換;然後在剩下的數當中再找最小(或者最大)的與第2個位置的數交換,依次類推,直到第n-1個元素(倒數第二個數)和第n個元素(最後一個數)比較為止。     * @param arr  數組     */    public void simpleSelectionSort(int[] arr){        for(int i=0;i<arr.length;i++){            int minIndex = i;            for(int j=i + 1;j<arr.length;j++){                if(arr[j] < arr[minIndex]){                    minIndex = j;                }            }            int temp = arr[minIndex];            arr[minIndex] = arr[i];            arr[i] = temp;        }        System.out.println();    }    //6、堆排序    /***     * 1. 如何將n 個待排序的數建成堆;     2. 輸出堆頂元素後,怎樣調整剩餘n-1 個元素,使其成為一個新堆。     * @param arr     */    public void heapSort(int[] arr){        buildHeap(arr);        for (int i = arr.length - 1; i > 0; --i)        {            //交換堆頂元素H[0]和堆中最後一個元素            int temp = arr[i];            arr[i] = arr[0];            arr[0] = temp;            //每次交換堆頂元素和堆中最後一個元素之後,都要對堆進行調整            adjustHeap(arr,0,i);        }    }    private void buildHeap(int[] arr){        //最後一個有孩子的節點的位置 i=  (length -1) / 2        for (int i = (arr.length -1) / 2 ; i >= 0; --i)            adjustHeap(arr,i,arr.length);    }    private void adjustHeap(int[] arr,int index,int length){        int child = 2*index+1; //左孩子結點的位置。(i+1 為當前調整結點的右孩子結點的位置)        int temp = arr[index];        while (child < length) {            if(child+1 <length && arr[child]<arr[child+1]) { // 如果右孩子大於左孩子(找到比當前待調整結點大的孩子結點)                ++child ;            }            if(arr[child] > arr[index]){      //// 如果較小的子結點大於父結點                arr[index] = arr[child]; // 那麼把較小的子結點往上移動,替換它的父結點                index = child;       // 重新設定s ,即待調整的下一個結點的位置                child = 2*index+1;            }else                break;            arr[index] = temp;        }    }    //7、希爾排序-插入排序        //8、基數排序

 

排序演算法 Java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.