Sort Algorithms Algorithm -shell (displacement method)

Personally I feel that the shift method is grouped by Hill sort, insertion sort sort

Comments are previous exchange method.

And this sort of really fast

package com.ebiz.sort;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author YHj
 * @create 2019-07-30 8:53
 * shell排序-交换法
 */
public class Shell{

    public static void main(String[] args) {

        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr[i] = (int) (Math.random() * 800000);
        }



        String s = new new the SimpleDateFormat ( "the MM-dd-YYYY HH: mm: SS") the format (. new new a Date ()); 
        System.out.println ( "before sorting =" + S); 

        the getResult (ARR); 


        String L = new new the SimpleDateFormat . ( "the mM-dd-YYYY HH: mm: SS") the format ( new new a Date ()); 
        System.out.println ( "sorted =" + L); 

        // System.out.println (of Arrays.toString ( ARR)); 

        // second packet 5/2 = 22 groups, each interval of the step 2 is two numbers,
         // set number 5, the outer loop, a fourth group, a total of 8 Ci
 //         for (int I = 2; I <arr.length; I ++) {
 //             // established to compare the number of step size
 //            for (int I = J-2; J> = 0; = J-2) {
 //                 IF (ARR [J]> ARR [J + 2]) {
 //                     int ARR TEMP = [J + 2];
 / /                     ARR [J + 2] = ARR [J];
 //                     ARR [J] = TEMP;
 //                 }
 //             }
 // 
//         }
 //         System.out.println (of Arrays.toString (ARR)); 

        / / 3rd packet 2/2 = 1 1 ,, each interval group number two steps is 1,
         @ a group number 10, the outer loop, a group 9, a total of 9
 //         for (int. 1 = I; I <arr.length; I ++) {
 //             // establish the number of steps to be compared
 //             for (int. 1-I = J; J> = 0; J-=. 1) {
//                 IF (ARR [J]> ARR [J +. 1]) {
 //                     int ARR TEMP = [J +. 1];
 //                     ARR [J +. 1] = ARR [J];
 //                     ARR [J] = TEMP;
 //                 }
 //             }
 // 
//         }
         // System.out.println (of Arrays.toString (ARR)); 

    } 

    public  static  void the getResult ( int [] ARR) {
 //         // first packet 10 / 2 = 55 groups, the number of two spaced steps each group 5,
 //         // a set of two numbers, the outer loop a group 1, a total of five times
 //         for (int I = 5; I <arr.length; I ++) {
 //             //Establishing the number of steps to be compared
 //             for (int. 5-I = J; J> = 0; J-=. 5) {
 //                 IF (ARR [J]> ARR [+ J. 5]) {
 //                     int ARR = TEMP [+ J. 5];
 //                     ARR [+ J. 5] = ARR [J];
 //                     ARR [J] = TEMP;
 //                 }
 //             }
 // 
//         } 

        // controlling the number of component
 //         for (int I = arr.length / 2; I> =. 1; I / = 2) {
 //             // number of times each control comparisons
 //             for (int I = J; J <arr.length; J ++ ) {
 //                 // each compare and exchange
 @                for (int JI = K; K> = 0; = - K-I) {
 //                     IF (ARR [K]> ARR [K + I]) {
 //                         int ARR TEMP = [K + I];
 //                         ARR [K + I] = ARR [K];
 //                         ARR [K] = TEMP;
 //                     }
 //                 }
 // 
//             }
 // 
// 
//         } 

        // the shell sort - shift method, i.e., insertion sort
         // control packet 
        for ( int GAP = arr.length / 2; GAP> 0; GAP / = 2 ) {
             // for all groups are sorted 
            for ( intGAP = I; I <arr.length; I ++ ) {
                 // the number to be sorted 
                int J = I;
                 int TEMP = ARR [J];
                 // comparing 
                the while (J-GAP> = 0 && TEMP <ARR [J - GAP]) { 
                    ARR [J] = ARR [J- GAP]; 
                    J - = GAP; 
                } 
                ARR [J] = TEMP; 
            } 

        } 


    } 

}

 

Guess you like

Origin www.cnblogs.com/jiushixihuandaqingtian/p/11313073.html