zoukankan      html  css  js  c++  java
  • 排序算法 Java

     //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、基数排序
  • 相关阅读:
    HDU 2852 KiKi's K-Number (主席树)
    HDU 2089 不要62
    Light oj 1140 How Many Zeroes?
    Bless You Autocorrect!
    HDU 6201 transaction transaction transaction
    HDU1561 The more ,The better (树形背包Dp)
    CodeForces 607B zuma
    POJ 1651 Mulitiplication Puzzle
    CSUOJ 1952 合并石子
    Uva 1599 Ideal path
  • 原文地址:https://www.cnblogs.com/271934Liao/p/8228108.html
Copyright © 2011-2022 走看看