Shell sort (reduce incremental sort)

Source: Internet
Author: User

Core idea: First determine an element interval number gap, and then the sequence of participating in the order of this interval from the beginning of the 1th element into a number of sub-sequences, that is, all positions spaced gap is considered as a sub-sequence, in each sub-sequence with some sort of sorting method to sort; then decrease the number of intervals. and re-divides the whole sequence into several sub-sequences according to the new interval number, then sorts each subsequence separately, so that it goes on until the interval number gap=1;

var arr = [38,49,65,97,76,13,27,49];


Shellsort (arr);
Console.log (arr);

varShellsort =function(arr) {varI, j,temp; varn =arr.length; varGap =N;  while(gap>1) {Gap= GAP/2; Do{flag= 0;  for(i=0;i<n-gap;i++) {J= i+Gap; if(arr[i]>Arr[j]) {Temp=Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; Flag= 1; }      }    } while(flag!=0); }}

Algorithm analysis: The time complexity of the shell sort is slightly greater than O (nlog2n) near O (n1.23), unstable sort, not suitable for linked lists.

Shell sort (reduce incremental 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.