5 sorting algorithms (bubble, select, insert, fast, merge)

1 bubble sort

Comparison of the adjacent elements, the small to the front, (each round to find the largest array on the back, the back row of the array elements are sorted sort does not participate in the next round).
The following array [7,8,5,1,3] sorts the elements inside.
7 8 5 1 3

1.1: 785,137 and 8 are compared, because 7 <8 positions of the two elements so that the same
1.2: 571,388 and 5 are compared, since 8> 5 so interchanging two elements
1.3: 751,838 and 1 are compared, since 8> 1 so that positions of the two elements are interchangeable
1.4: 75 138 Similarly, the position and the exchange 83, to give the maximum number of 8, and do not participate under a sort
2.1: 57 138 Similarly the second round of sorting to obtain the maximum number is 7, placed at the end, a maximum value of each obtained sequentially, so that small numbers in front of a large
number on the back Finally, get to be an array of [1,3,5,7,8].

/ ** 
     * @Description comparing adjacent elements, the small to the front, (each round to find out the number of 
     the largest group * on the back, the back row of the array elements good sequence does not participate in the next round of sorting) 
     * @ bubbingSort MethodName   
     * @date 2019/5/27 14:51 
     * @Param [ARR] 
     * @return void 
     * @throws 
     * / 
    public  static  void bubbingSort ( int [] ARR) {
         // the outer loop ordering number of passes 
        for ( int I = 0; I <arr.length -. 1; I ++ ) {
             for ( int J = 0; J <arr.length -. 1 - I; J ++ ) {
                 // inner loop controls how many times each trip sort 
                IF ( arr [j] <arr [j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
                System.out.println("bubbingSort " + i + Arrays.toString(arr));
            }
        }
    }

 

2 Select Sort

1. Each element of the array is compared with the first element, if this element is less than the first element, the two switching elements
2. Rule 1 cycle, to find the smallest element, placed in a first position
3. After completion of n-1 comparison sorting wheel
/ ** 
     * @Description 1. Comparative each element of the array to the first element, if this element is less 
     * to the first element, the two switching elements 
     * cycle 2. Rule 1, find the minimum element, placed in the first position 
     * n-1 wheel 3. after completion of the comparison sort 
     * @MethodName SelectSort 
     * @date 2019/5/27 14:48 
     * @Param [ARR] 
     * @return void 
     * @throws 
     * / 
    public  static  void SelectSort ( int [] ARR) {
         for ( int I = 0; I <arr.length; I ++ ) {
             int value = ARR [I];
             int position = I;
             // inner loop to find the minimum number, and records the minimum number of positions 
            for (int J = I +. 1; J <arr.length; J ++ ) {
                 // Get the number after each time with a minimum number to a minimum number after comparing 
                IF (ARR [J] < value) { 
                    value = ARR [J ]; 
                    position = J; 
                } 
            } 
            // inner loop end of the exchange 
            ARR [position] = ARR [I]; 
            ARR [I] = value; 
            System.out.println ( "SelectSort" + I + of Arrays.toString (ARR) ); 
        } 

    }

 

3 Insertion Sort

1. The array is divided into two parts, the first one by comparison with the front portion of the rear portion of each element is inserted in a reasonable position
2. insertion sort efficiency is higher than bubble sort and selection sort 
 
78513 array is divided into two portions 7 and 8,5,1,3
1: 785 138> 7, the position of the same
2: 578 135 <5 8 && <7, so prepended 5 7 and 8 
3: 157 831 <8 && 1 <1 7 && <5, the 1 to the front of 5,7,8
4: 13578 The front element 3 and Comparative sequentially, to give 3 <5,1> 3, 3 so between 1 and 5, the end 
/ ** 
     * 1. @Description array divided into two parts, the first one by a front portion and rear portion of each 
     comparator element *, inserted in a reasonable position Arrays.sort (); row fast 
     * 2. insertion sort efficiency is higher than bubble sort and selection sort 
     * @MethodName insertSort 
     * @date 2019/5/27 14:54 
     * @Param [ARR] 
     * @return void 
     * @throws 
     * / 
    public  static  void insertSort ( int [] ARR) {
         int insertNum;
         for ( int I =. 1; I <arr.length; I ++ ) {
             // begin inserted from the first number 
            = insertNum ARR [I];
             int J = I -. 1 ;
             //Will be greater than insertNum moved rearward (equivalent to the exchange position) 
            the while (J> = 0 && ARR [J]> insertNum) { 
                ARR [J +. 1] = ARR [J]; 
                J - ; 
            } 
            ARR [J +. 1] = insertNum; 
            System.out.println ( "insertSort" + I + of Arrays.toString (ARR)); 
        } 

    }

4 Quick Sort

By ordering the trip data to be sorted into two separate portions, wherein a portion of all of the data than the other portion of all the data to be small, then this method to quickly sort the data two portions respectively, the entire sorting process can be recursively, in order to achieve the entire data becomes an ordered sequence.
10 4 7 8 5 9 3 12 11
1: a reference number selected from 10, sorted for the first time, to put the left less than 10, greater than 10 to put the right to obtain new array [4,7,8,5,9,3,10,12,11] to about 10 as a reference divided into two parts, [4,7,8,5,9, 3], 10, [12, 11], respectively, on both sides of the array quick sort, to sort the first element of the array as a reference.
Current data [4,7,8,5,9,3], 10, [12,11]
2: [4,7,8,5,9,3]以第一个元素4作为基准排序得到[3,4,5,7,8,9];后面的数组为[11,12],结束。
当前数据为[3],4,[5,7,8,9],10,11,12,因为3为单个的,所以[3]不需再进行排序,目前只需对[5,7,8,9]进行处理
3: [5,7,8,9],以第一个元素5作为基准排序,得到5,[,7,8,9]当前数据为3,4,5,[7,8,9],10,11,12
4: 类似步骤3,分别把7,8,9给独立出来,最终得到数据3,4,5,7,8,9,10,11,12
 /**
     * @Description 选择一个基数,小于基数的在左边,大于基数在右边 通过一趟排序将要排序的数据分割成独立的两部分,其中
     * 一部分的所有数据都比另外一部分的所有数据都要小,然后再按
     * 此方法对这两部分数据分别进行快速排序,整个排序过程可以递
     * 归进行,以此达到整个数据变成有序序列
     * @MethodName quickSort
     * @Date 16:10 2019/5/27
     * @Param [a]
     * @Return void
     * @Throws
     */
    public static void quickSort(int[] arr, int low, int high) {
        int i, j, temp, t;
        if (low > high) {
            return;
        }
        i = low;
        j = high;
        //temp就是基准位
        temp = arr[low];
        while (i < j) {
            //先看右边,依次往左递减
            //{5, 2, 7, 3, 8};
            //5<8 y-1 5《3 不成立 y=3(找比基数小的位置)
            while (temp <= arr[j] && i < j) {
                j--;
            }
            //再看左边,依次往右递增
            //{5, 2, 7, 3, 8}; 5>2 i++ i=2 5>7 i=2(找比基数大的位置)
            while (temp >= arr[i] && i < j) {
                i++;
            }
            //如果满足条件则交换   //将小的替换到左边 大的到右边
            if (i < j) {
                t = arr[j];
                arr[j] = arr[i];
                arr[i] = t;
            }

        }
        //最后将基准为与i和j相等位置的数字交换
        arr[low] = arr[i];
        arr[i] = temp;
        System.out.println("quickSort " + i + Arrays.toString(arr));
        //递归调用左半数组
        quickSort(arr, low, j - 1);
        //递归调用右半数组
        quickSort(arr, j + 1, high);

    }

5归并排序

该算法是采用分治法(DIVIDE AND CONQUER)的一个非常典型的应用,且各层分治递归可以同时进行
/**
     * @Description 分而治之(divide - conquer);每个递归过程涉及三个步骤
     * 第一, 分解: 把待排序的 n 个元素的序列分解成两个子序列, 每个子序列包括 n/2 个元素.
     * 第二, 治理: 对每个子序列分别调用归并排序MergeSort, 进行递归操作
     * 第三, 合并: 合并两个排好序的子序列,生成排序结果.
     * @MethodName mergeSort
     * @Author yanp.fan001
     * @Date 17:23 2019/5/27
     * @Param [a, low, high]
     * @Return int[]
     * @Throws
     */
    public static int[] mergeSort(int[] a, int low, int high) {
        int mid = (low + high) / 2;
        if (low < high) {
            mergeSort(a, low, mid);
            mergeSort(a, mid + 1, high);
            //左右归并
            merge(a, low, mid, high);
        }
        return a;
    }

    public static void merge(int[] a, int low, int mid, int high) {
        int[] temp = new int[high - low + 1];
        int i = low;
        int j = mid + 1;
        int k = 0;
        // 把较小的数先移到新数组中
        while (i <= mid && j <= high) {
            if (a[i] < a[j]) {
                temp[k++] = a[i++];
            } else {
                temp[k++] = a[j++];
            }
        }
        // 把左边剩余的数移入数组
        while (i <= mid) {
            temp[k++] = a[i++];
        }
        // 把右边边剩余的数移入数组
        while (j <= high) {
            temp[k++] = a[j++];
        }
        // 把新数组中的数覆盖nums数组
        for (int x = 0; x < temp.length; x++) {
            a[x + low] = temp[x];
        }
    }

 

 

Guess you like

Origin www.cnblogs.com/fanBlog/p/10932117.html