zoukankan      html  css  js  c++  java
  • Sort Methods

    heyheyhey ~~

    It has been a long time since i come here again...whatever today i will summerize some methods of sort with java what are so important for coder. The codes below are all compiled successfully and have the right results

    . insert sort -- is divided into insert directly and Shell Sort.

    1. insert directly -- the main idea is while obtaining a new value, to compare the new value with the number of sorted array, when get the position that the value is lager than the front number and smaller than the behind number,  insert the value.

    public class InsertDirectly {

      public static void insertDirectly(int[] a) {

        for(int i = 1; i<a.length; ++i) {

          // because maybe the insert value would be complicanted , so we should define a copy for it

          int temp = a[i];

          // then go through the every number of the sorted array to find out the right position.

          // to make the j be the global variable.

          int j = i-1

          // because maybe the a[i] would be complicanted,  so a[j] shouled > temp,not a[i] 

          for(; j >= 0 && a[j] > temp; --j) {

            a[j+1] = a[j]; 

          }

          a[j+1] = temp;

        }

      }

    }

    2. Shell Sort is the special sort of insert sort, just because it has the increment-'d' ,not one ny one any more.

        main idea is just according to the 'd' to carry out the method of directly insert sort

    public class ShellSort{

      public static void shellSort(int[] a) {

        // firt step :need to init the 'd'.

        double d1 = a.length;

        // the 'd' need to be changed, so make sure the loop

        while(true) {

          //ceil -- get the bigger one 

          double d1 = Math.ceil(d1/2);

          int d = (int) d1;

          // for the outer loop

          for(int i = 0; i<d; ++i) {

            //for the increment loop

            for(int j = i+d; j < a.length; i+=d) {

              int temp = a[j];

              // for the inner loop

              int x = j-d;

              for(; x >= 0 && a[x] > temp; x-=d) {

                a[x+d] = a[x];

              }

              a[x+d] = temp;

            }

          }

          if( d == 1) break;

        }

      }

    二. select sort -- is divided into easy select sort and heap sort.

    1. easy select sort is the most easy sort --  main idea is make sure the loop of find out the lagest one or the smallest one ,and put it to the rear or head of the array

    public class EasySort {

      public static void swap(int[] a ,int x, int y) {

        int temp = a[x];

        a[x] = a[y];

        a[y] = temp;

      }

      public static void easySort(int[] a) {

        for(int i = 0; i<a.length; ++i) {

          int max = a[i];

          int flag = i;

          for(int j = i+1; j < a.length;++j) {

            if(max < a[j]) {

              max = a[j];

              flag = j;

            }

          }

          swap(a, a.length-1-i, flag);

        }

      } 

    }

    2.Heap Sort is actually the selected sort based on heap -- main idea is that build max or min heap and then change the position bettween the max or min and the last position. 

    public class HeapSort {

      // this methid is mainly to change the max value which is in the top of heap(in other word which is always the first position of the array) 

         // with the last number of this array.

      public static void swap(int[] a, int i, int j) {

        int temp = a[i];

        a[i] = a[j];

        a[j] = temp;

      }

      public static void heapSort(int[] a) {

        // from the last value of the array in order to buildMaxHeap circularly.

        for(int i = a.length-1; i > 0; --i) {

          buildMaxHeap(a, i);

          swap(a, 0, i);

        }

      }

      // next is the highlight of the heap sort ,and in this method we would to let u know how to bulid the max heap and how to reform the heap a

         // again and again.

       public static void buildMaxHeap(int[] a,int lastIndex) {

        // step 1 : get the last father node of this heap

        int lastFather = (lastIndex-1)/2;

        // step 2 : we need to find out the max value among the father node and the subnodes 

        for(int i = lastFather; i >= 0; --i) {

          // step 2.1 : first we need to find out the max value bettween the subnodes

          int biggerIndex = lastFather*2+1; //  make the left node be the max first and then judge if the right node exists

          if(biggerIndex < lastIndex) // make sure the right node exists {

            if(a[biggerIndex] < a[lastIndex]) {

              biggerIndex ++;

            }

          }

          // step 2.2 :  second we need to compare the biggerOne and the father node

          if(a[lastFather] < a[biggerIndex]) {

            // let the larger value go to the top

            swap(a, lastFather, biggerIndex);

          }

        }

      }

    }

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------

    哎,本来说上学期坚持的,结果拖到了这学期了,但是确实是最近事情比较多,接的项目也比较繁琐,不过,什么都不会抵挡我继续学习的心情,废话不多说,继续吧

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------

    Quick sort -- 快排是现在比较普遍和使用的排序工具,主要是因为它是跳跃性的交换数据的,而不是像简单排序和冒泡那样得相邻的两个数据进行交换,当然快排还是加强版的冒泡

    思想只要就是,定好一个基数后,然后从这个基数后面的开始,设下标为i,和从最后一个开始,设下标为j,遍历,在i<j的时候,找到,a[i] > 基数,和a[j]<基数的,进行交换,直到i>j,之后就交换基数和a[j],这样就可以做到,一次性将大于基数的移到基数的右边,小于基数的移到基数的左边,然后这里再返回一个j,来作为下一次进行这样一个过程的分界点.当然这里也会用到递归

    package Sort;
    
    import java.util.Scanner;
    
    /*
     * 简单介绍一下快排的思想  : 
     * */ 
    
    public class QuickSort {
        // 交换数组中的两个数字
        public static void swap(int[] a, int i, int j){
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
        
        public static void quicky_sort(int[] a, int start, int end){
            // 这一步必须要,否则会出现outofbounds的情况,因为会出现p前面或者后面只有一个数字的情况
            if(start >= end || a == null) return; 
            
            int p = partition(a, start, end);
            quicky_sort(a, start, p-1);
            quicky_sort(a, p+1, end);
            
        }
        // partition : 主要是在一个基数的循环下返回下一个批次要进行的 
        public static int partition(int[] arr, int start, int end){
            int parti = 0;
            int i = start;
            int j = end;
            
            while(i<j){
                while(arr[i] <= arr[start] && i<end){ // 后面的条件必须要,否则会出现outofbounds的错误,因为这是第一个循环,想想要是是一个一直从大到小的数组,如果没有后面的&&就会访问越界
                    i++;
                }
                while(arr[j] > arr[start] && j >= start){
                    j--;
                }
                if(i < j){ //这个也是必须的,因为会出现最后一种当i>j的情况,这个时候就没有必要交换了
                    swap(arr, i, j);
                }
            }
            
            swap(arr, start, j);
            parti = j;
            
            return parti;
        }
        public static void main(String[] args) {
            //int[] a = new int[10];
    //        Scanner in = new Scanner(System.in);
    //        for(int i=0;i<a.length; ++i){
    //            a[i] = in.nextInt();
    //        }
            int[] a = {57, 68, 59, 52, 72, 28, 96, 52, 24, 19};
            quicky_sort(a, 0, a.length-1);
            for(int i=0;i<a.length; ++i){
                System.out.print(" "+a[i]);
            }
        }
    }

    上面也有一些要注意的地方,都是我debug时候血的教训啊,没考虑到的细节有很多,比如在你进行i和j的循环的时候,因为i是第一个指标,如果你让他<end的话,那如果分出来的新的数组就是从大到小的,那么就会出现数组越界的情况了,因为,满足的话,i就会++嘛,然后后面那个if里面的看起来和在外层的条件是一样的,但绝不是多余的,因为一定要考虑到临界的情况,当i>j的时候,这个时候就不需要进行交换了,所以必然要加上的

    还有一个就是,在quicky_sort()里面要考虑到start >= end 的时候,就不需要后面的步骤了。

  • 相关阅读:
    AC自动机模板
    KMP 模板
    HDU 2746 Cyclic Nacklace
    LCS模板
    POJ 1159 Palindrome
    LIS n^2&nlogn模板
    Codeforces Round #Pi (Div. 2) C. Geometric Progression
    Codeforces Beta Round #25 (Div. 2 Only)E. Test
    Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland
    bzoj5055 膜法师
  • 原文地址:https://www.cnblogs.com/AmoryWang-JavaSunny/p/6036722.html
Copyright © 2011-2022 走看看