Advanced sorting-hill sorting

Source: Internet
Author: User

For thousands of data items, the sort of arrays of medium size performs well, unlike the fast sort and the other time complexity is O (n * logn) the sorting algorithm of is so fast. Therefore, it is not the optimal choice for sorting very large files, but the time complexity of selecting sorting and inserting sorting is O (n²) it is much faster to sort, and it is very easy to implement, the code is short

Hill sorting is also a kind of insert sorting. In insert sorting, if the smallest number is at the end, there are too many copies, and Hill solves this problem, it is also an N-incremental sorting. Its idea is to increase the interval between elements in the insertion sorting and sort the inserted elements in these separated elements, after these data items are sorted in a descending order, the hill sorting algorithm reduces the interval of data items and then sorts them accordingly. The interval between data items during sorting is called increment, which is often expressed by the letter H.

For an array to be sorted by hill immediately, the start interval should be larger, and the interval will not be decreased until the interval changes to 1.

Interval sequence:

The number quality in the interval sequence is usually considered very important-except for 1, they do not have a common number. This constraint makes it more likely that each sort will maintain the effect of the previous sort, for different intervals, there is an absolute condition, that is, the decreasing interval must be equal to 1. therefore, the last step is a normal sort of insert.

The examples listed below are derived from the law of H = H * 3 + 1:

 1 package com.jll.sort; 2  3 public class ShellSort { 4     int[] arr; 5     int size; 6      7     public ShellSort() { 8         super(); 9     }10     11     public ShellSort(int size) {12         this.size = size;13         arr = new int[size];14     }15 16 17 18     /**19      * @param args20      */21     public static void main(String[] args) {22         ShellSort ss = new ShellSort(10);23         for(int i=0;i<10;i++){24             ss.arr[i] = (int) ((Math.random()*100)+1);25             System.out.print(ss.arr[i]+" ");26         }27         ss.shellSort();28         System.out.println();29         System.out.println("after sort:");30         for(int i=0;i<10;i++){31             System.out.print(ss.arr[i]+" ");32         }33         34     }35     36     public void shellSort(){37         int h = 1;38         while(h<=size/3){39             h = h*3+1;40         }41         for(;h>0;h=(h-1)/3){42             for(int i=h;i<size;i++){43                 int temp = arr[i];44                 int j = i;45                     while(j>h-1&&arr[j-h]>temp){46                         arr[j]=arr[j-h];47                         j-=h;48                     }49                     arr[j]=temp;50                 }51             }52         }53     }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.