Java sorting algorithm (iv): Shell sort

Source: Internet
Author: User

[Basic ideas]

Group records that originally had a large number of records. Cut into a number of sub-sequences, at this time each sub-sequence to sort the number of records is less, and then in these sub-sequences are directly inserted in the order, when the whole sequence is basically ordered. Then make a direct insert sort on the entire record.

The so-called basic order, is the small keyword basic in front, large basic in the back, medium-sized 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)

The shell sort is not a stable sorting algorithm.

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Java sorting algorithm (iv): Shell sort

Related Article

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.