Java several common sorting algorithm

 

 First, the so-called sequencing , is to make a bunch of records, which follow a certain size or certain keywords, increasing or decreasing lined up operations. Sorting algorithm, it is how to make the recording method according to claim arrangement. We received considerable attention to sorting algorithms in many areas, especially in processing large amounts of data. A good algorithm can save a lot of resources. In various fields taking into account the constraints and specifications data, to get a good algorithm realistic, get through a lot of reasoning and analysis.

  Second, the sorting algorithm can be divided into internal sorting and external sorting .

    Internal sorting sorts the data records in memory.

    External sorting because a great sort of data, one can not accommodate all sort of record, in the sorting process requires access to external memory.

    Common internal sorting algorithms: bubble sort, selection sort, insertion sort, Shell sort, quick sort, merge sort and so on.

Of course: The actual sorting algorithm can be more than a little so, as if understanding of other algorithms can refer to: https://baike.baidu.com/item/%E6%8E%92%E5%BA%8F%E7%AE%97% E6% B3% 95/5399605? fr = aladdin # 3

 Third, here we introduce several common sorting algorithm

  1)  bubble sort low version

  a, bubble sort, for each iteration by obtaining maximum / minimum

  b, the maximum / minimum placed at the end / head

  C, then in addition to the maximum / minimum value, the remaining data acquired during the traverse Max / Min

  d, code implementation

public  class MaoPao { 

    public  static  void   Sort ( int [] ARR) {
         for ( int I =. 1; I <arr.length; I ++) {   // a first layer of a for loop, for controlling the bubbling of times 
            for ( int J 0 =; J <-arr.length. 1; J ++) { // the second layer for loop for controlling a bubbling layer to the last
                 // if the number is greater than a previous number, the exchange of both, means bubbles go up a layer 
                IF (ARR [J]> ARR [J +. 1 ]) {
                     int TEMP = ARR [J]; 
                    ARR [J] = ARR [J +. 1 ]; 
                    ARR [J + 1'd] =  TEMP ;
                }
            }
        }
    }
}

     Bubbling bigger version

In this version, two changes to the
first point is added to a Boolean value, the second layer is replaced with a determination cycle is not performed, if there is no pairwise exchange, described later have been sorted, and has no need the recirculation, directly out of the loop, the end of the sorting.
the second layer is a second cycle is not recycled to arr.length - 1, because the outside loop i is incremented once more finally described a array sorted bulla bubble. the second layer would be no need to cycle the end of one of the most, you can end the cycle ahead

/ ** 
 * Ultimate Edition bubble sort 
 * add a Boolean variable, if not in the loop exchange value, explained that it had completed sequencing, early termination 
 * @param arr
  * / 
public  static  void sortPlus ( int [] arr) {
     IF (arr! = null && arr.length>. 1 ) {
         for ( int I = 0; I <arr.length -. 1; I ++ ) {
             // initialize a Boolean 
            Boolean In Flag = to true ;
             for ( int J = 0; J <ARR. length - I -. 1; J ++ ) {
                 IF (ARR [J]> ARR [J +. 1 ]) {
                     //Exchange 
                    int TEMP; 
                    TEMP = ARR [J]; 
                    ARR [J] = ARR [J +. 1 ]; 
                    ARR [J + 1'd] = TEMP;
                     // change In Flag 
                    In Flag = to false ; 
                } 
            } 
            IF (In Flag) {
                 BREAK ; 
            } 
        } 
    } 
}

  2) Selection Sort

    a, the first value as a minimum value

  b, then find the minimum value and the subsequent comparison and subscript

  c, switching the start value and the minimum value in the traversal of

  d, Description: each pass, when the front of the identified minimum, as an ordered list, the latter as unordered list, and then find the minimum value of each iteration unordered list.

  e, code implementation

public static void main(String[] args) {

        int arr[] = {6, 5, 3, 2, 4};

        //选择
        for (int i = 0; i < arr.length; i++) {
            //默认第一个是最小的。
            int min = arr[i];
            //记录最小的下标
            int index = i;
            //通过与后面的数据进行比较得出,最小值和下标
            for (int j = i + 1; j < arr.length; j++) {
                if (min > arr[j]) {
                    min = arr[j];
                    index = j;
                }
            }
            //然后将最小值与本次循环的,开始值交换
            int temp = arr[i];
            arr[i] = min;
            arr[index] = temp;
            //说明:将i前面的数据看成一个排好的队列,i后面的看成一个无序队列。每次只需要找无需的最小值,做替换
        }
    }

  3)插入排序

     a、默认从第二个数据开始比较。

  b、如果第二个数据比第一个小,则交换。然后在用第三个数据比较,如果比前面小,则插入(狡猾)。否则,退出循环

  c、说明:默认将第一数据看成有序列表,后面无序的列表循环每一个数据,如果比前面的数据小则插入(交换)。否则退出。

  d、代码实现

public static void main(String[] args) {

        int arr[] = {7, 5, 3, 2, 4};

        //插入排序
        for (int i = 1; i < arr.length; i++) {
            //外层循环,从第二个开始比较
            for (int j = i; j > 0; j--) {
                //内存循环,与前面排好序的数据比较,如果后面的数据小于前面的则交换
                if (arr[j] < arr[j - 1]) {
                    int temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                } else {
                    //如果不小于,说明插入完毕,退出内层循环
                    break;
                }
            }
        }
    }

  4)希尔排序(插入排序变种版)

  a、基本上和插入排序一样的道理

  b、不一样的地方在于,每次循环的步长,通过减半的方式来实现

  c、说明:基本原理和插入排序类似,不一样的地方在于。通过间隔多个数据来进行插入排序。

  d、代码实现

public static void main(String[] args) {

        int arr[] = {7, 5, 3, 2, 4};

        //希尔排序(插入排序变种版)
        for (int i = arr.length / 2; i > 0; i /= 2) {
            //i层循环控制步长
            for (int j = i; j < arr.length; j++) {
                //j控制无序端的起始位置
                for (int k = j; k > 0  && k - i >= 0; k -= i) {
                    if (arr[k] < arr[k - i]) {
                        int temp = arr[k - i];
                        arr[k - i] = arr[k];
                        arr[k] = temp;
                    } else {
                        break;
                    }
                }
            }
            //j,k为插入排序,不过步长为i
        }
    }

  5)快速排序

  a、确认列表第一个数据为中间值,第一个值看成空缺(低指针空缺)。

  b、然后在剩下的队列中,看成有左右两个指针(高低)。

  c、开始高指针向左移动,如果遇到小于中间值的数据,则将这个数据赋值到低指针空缺,并且将高指针的数据看成空缺值(高指针空缺)。然后先向右移动一下低指针,并且切换低指针移动。

  d、当低指针移动到大于中间值的时候,赋值到高指针空缺的地方。然后先高指针向左移动,并且切换高指针移动。重复c、d操作。

  e、直到高指针和低指针相等时退出,并且将中间值赋值给对应指针位置。

  f、然后将中间值的左右两边看成行的列表,进行快速排序操作。

  g、代码实现

public class Sort {
     public static void main(String[] args) {

        int arr[] = {7, 5, 3, 2, 4, 1, 8, 9, 6};

        //快速排序
        int low = 0;
        int high = arr.length - 1;
        quickSort(arr, low, high);  
    }
    public static void quickSort(int[] arr,int begin,int end) {
        //先定义两个参数接收排序起始值和结束值
        int a = begin;
        int b = end;
        //先判断a是否大于b

        if (a >= b) {
            //没必要排序
            return;
        }
        //基准数,默认设置为第一个值
        int x = arr[a];

        //循环
        while (a < b) {
            //从后往前找,找到一个比基准数x小的值,赋给arr[a]
            //如果a和b的逻辑正确--a<b ,并且最后一个值arr[b]>x,就一直往下找,直到找到后面的值大于x
            while (a < b && arr[b] >= x) {
                b--;
            }
            //跳出循环,两种情况,一是a和b的逻辑不对了,a>=b,这时候排序结束.二是在后面找到了比x小的值
            if (a < b) {
                //将这时候找到的arr[b]放到最前面arr[a]
                arr[a] = arr[b];
                //排序的起始位置后移一位
                a++;
            }

            //从前往后找,找到一个比基准数x大的值,放在最后面arr[b]
            while (a < b && arr[a] <= x) {
                a++;
            }
            if (a < b) {
                arr[b] = arr[a];
                //排序的终止位置前移一位
                b--;
            }
        }
        //跳出循环 a < b的逻辑不成立了,a==b重合了,此时将x赋值回去arr[a]
        arr[a] = x;
        //调用递归函数,再细分再排序
        quickSort(arr,begin,a-1);
        quickSort(arr,a+1,end);
    }
}

  6)归并排序

 

  a、将列表按照对等的方式进行拆分

  b、拆分小最小快的时候,在将最小块按照原来的拆分,进行合并

  c、合并的时候,通过左右两块的左边开始比较大小。小的数据放入新的块中

  d、说明:简单一点就是先对半拆成最小单位,然后将两半数据合并成一个有序的列表。

  e、代码实现

public static void main(String[] args) {

        int arr[] = {7, 5, 3, 2, 4, 1,6};

        //归并排序
        int start = 0;
        int end = arr.length - 1;
        mergeSort(arr, start, end);
    }

    public static void mergeSort(int[] arr, int start, int end) {
        //判断拆分的不为最小单位
        if (end - start > 0) {
            //再一次拆分,知道拆成一个一个的数据
            mergeSort(arr, start, (start + end) / 2);
            mergeSort(arr, (start + end) / 2 + 1, end);
            //记录开始/结束位置
            int left = start;
            int right = (start + end) / 2 + 1;
            //记录每个小单位的排序结果
            int index = 0;
            int[] result = new int[end - start + 1];
            //如果查分后的两块数据,都还存在
            while (left <= (start + end) / 2 && right <= end) {
                //比较两块数据的大小,然后赋值,并且移动下标
                if (arr[left] <= arr[right]) {
                    result[index] = arr[left];
                    left++;
                } else {
                    result[index] = arr[right];
                    right++;
                }
                //移动单位记录的下标
                index++;
            }
            //当某一块数据不存在了时
            while (left <= (start + end) / 2 || right <= end) {
                //直接赋值到记录下标
                if (left <= (start + end) / 2) {
                    result[index] = arr[left];
                    left++;
                } else {
                    result[index] = arr[right];
                    right++;
                }
                index++;
            }
            //最后将新的数据赋值给原来的列表,并且是对应分块后的下标。
            for (int i = start; i <= end; i++) {
                arr[i] = result[i - start];
            }
        }
    }

  7)其他排序

 比如Arrays工具类提供的排序方法。它内部实现也是快速排序

private static void arraysSort(int[] a){
    Arrays.sort(a);
  }

还有就是将数组转为list,使用集合的排序方法,但是这无异于兜圈子,因为集合底层也是数组

private static void listSort(int[] a){
    List<Integer> integers = Ints.asList(a);
    Collections.sort(integers);
    integers.toArray(new Integer[a.length]);
  }

参考博客:https://www.cnblogs.com/ll409546297/p/10956960.html

 

Guess you like

Origin www.cnblogs.com/Young111/p/11300929.html