Shell sort/Reduce incremental sort (c + +)

Source: Internet
Author: User
Tags comparable

Shell sort/Reduce incremental sort (c + +)Shell sort/reduce incremental sort:

He works by comparing elements that are spaced apart, and the distances used by the comparisons are reduced as the algorithm progresses until the last order of the adjacent elements is compared. (Very complex)

If you look at the implementation code, you'll find it looks like a sort of insertion, except for a vest outside.

The following code shows the increment rule on the base of the insertion sort, with the original increment of 1 becoming delta to Gap.

Code Implementation:
1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 //Schell Sort (reduced incremental sort)7Template<typename comparable>8 voidShellsort (Vector<comparable> &a)9 {Ten      for(intGap = A.size ()/2; Gap >0; Gap/=2) One     { A          for(inti = Gap;i < A.size (); i++) -         { -Comparable TMP =A[i]; the             intj =i; -  -              for(; J >= Gap&&tmp < a[j-gap];j-=Gap) -             { +A[J] = a[j-Gap]; -             } +A[J] =tmp; A         } at     } - } -  - intMain () - { -vector<int> A = {3,4,5,8,7,1 }; in Shellrtsort (a); -      for(auto c:a) to     { +cout << c<<Endl; -     } theSystem"Pause"); *}

Shell sort/Reduce incremental sort (c + +)

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.