Java sorting algorithm (iv): Hill sort

Source: Internet
Author: User

[Basic idea]

The records that originally have a large number of records are grouped into several sub-sequences, at which time the number of records to be sorted in each subsequence is less, and then the sequence of direct insertion in these subsequence sequence, when the entire series is basically ordered, and then a direct insertion of the whole record to sort.

The so-called basic order, is the small keyword basic in front, large basic in the back, the basic in the middle, like {2, 1, 3, 6, 4, 7, 5, 8, 9} This can be called basic order.


[Java Implementation]

public class Shellsort {public static void main (string[] args) {int[] arr = {6, 5, 3, 1, 8, 7, 2, 4}; System.out.println ("Before sorting:"); for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i] + "");} Hill sort Shellsort (arr); System.out.println (); System.out.println ("After sorting:"); for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i] + "");}} /** * Hill sort */private static void Shellsort (int[] arr) {int j;for (int gap = ARR.LENGTH/2; gap > 0; gap = gap/2) {fo R (int i = gap; i < arr.length; i++) {int tmp = arr[i];for (j = i; J >= Gap && tmp < ARR[J-GAP]; j = J -gap) {Arr[j] = Arr[j-gap];} ARR[J] = tmp;}}}}
[Algorithm description]

Hill sort time Complexity:O (NLOGN)

Hill sort is not a stable sorting algorithm.

Java sorting algorithm (iv): Hill sort

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.