C + +, C #, Java Algorithm learning diary----Hill Sort (shellsort) __web

Source: Internet
Author: User

Hill sort is an interpolation sort, also known as narrowing incremental ordering, which is a more efficient algorithm for direct insertion ordering, the basic idea:

Hill sort is to group records by a certain increment of the subscript (that is, by a certain step). Use a direct insert sort algorithm for each group; as the increments decrease (halve each time), each group contains more and more keywords, and when the increment is reduced to 1 o'clock (equivalent to a direct insert sort), the entire file is grouped into a group, The algorithm terminates.

That's what I understand, Hill sort can be said to be a sort of jump type (the direct insertion sort is each compared with the previous step by 1), the process is roughly divided into 2 stages, when the step is not 1--the fuzzy sort stage, the time step equals 1--the exact sort stage (that is, a direct insert sort). The previous fuzzy sort stage is to pave the back for the exact sort, in the previous article also said that the ranking is to determine the position of the elements, so in the fuzzy sort process, the same use of the two-point method to determine the position of the elements, the exact sort of the back when there is no need for a great move, so to achieve high efficiency.

For example: for a[] ={63, 4, 24, 1, 3, 15}, first we use the start step steps =length/2=3 each element in the array, and so on, exchange, that is to say, a[0] compared to a[0+step=3, a[0]>a[3] swap position, A [0]=1,a[3]=63 (in this case, 1 jumped to the first place), and then proceed to the next group of comparison A[1]>a[4] (subscript are different 3, the front than the rear of the big before Exchange), and then a[2] and a[5] Compare a[2]>a[5], exchange, The first comparison is complete. (At this point the array is roughly in order) step by half, steps = Step/2=1, at which point the direct insertion sort (specific reference direct insertion sort) does not require a large number of shifts on the sort completion.

      below we randomly generate 10,000 numbers and do hill sort and direct insertion, output the first 10 elements, and then compare the time used by the two sorts. C + + instance:

#include <iostream> #include <cstdlib> #include <vector>//Add array container #include <ctime>//Join time header File #

Include<cstdio> using namespace std;  void Shellsort (vector<int> array,int Length) {clock_t start,end; Definition start, end time int step=length/2,temp,i,j;
		  Define start step, Start=clock ();//Start timer while (step>=1) {for (i=step;i<length;i++) {temp=array[i]; Here when the step=1 is not much like a direct insert sort of for (J=i-step;j>=0&&temp<array[j];j=j-step) {array[j+step]=array[
		 J];
	  
	  } array[j+step]=temp; step=step/2;//step is gradually shortened} end=clock ()//Record end time//output Top 10 for (i=0;i<10;i++) {cout<<array[
   i]<< "";
   } cout<<endl;
cout<< "Hill sort When:" <<end-start<< "milliseconds" <<endl;  
	} void Insertsort (vector<int> array,int Length) {int i,j,key;
	clock_t Start,end;  Start=clock ()//Record start time for (i=1;i<length;i++)//OK to insert element {key = Array[i]; Key is to insert element for(j=i;j>0&&array[j-1]>key;j--)///Find the position to insert, end of loop, find insertion position {array[j]=array[j-1];//shift backwards to make room for insertion } Array[j]=key;
   Insert element} end=clock ()//Record end time for (i=0;i<10;i++) {cout<<array[i]<< "";
  } cout<<endl;
cout<< "Direct Insert sort:" <<end-start<< "MS" <<endl;
  } void Main () {vector<int> array;
  Vector<int> array2; for (int i=1;i<=10000;i++)//To sort 10,000 random numbers {Array.push_back (rand ());///To load random numbers into an array that is to join the queue} int length=array.size ( );
 Get the length array2=array; Shellsort (array,length);  Hill sort Insertsort (array2,length);
 Direct Insert Sort}


Results:

See, there are many times the difference. C # instance:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text; Using System.Threading; Sleep using System.Diagnostics; To add this reference there is no stopwatch namespace ShellSort2 {class Sort {//Hill Sort method public void Shellsort (list<int> Array) {int Length = array.
            COUNT/2;
            int step=length/2,i,j,temp; Stopwatch watch = new stopwatch ();//Timing Statement watch.
            Start ();
                    while (step >= 1) {for (i = step; i < Length; i++) {
                    temp = Array[i]; for (j = i-step J >= 0 && temp < array[j]; j = j-step) {AR
                    Ray[j + Step] = Array[j];
                
                Array[j + step] = temp;
            Step = STEP/2; } watch.
            Stop (); Console.WriteLine ("First 10 elements:" + String.Join (""), arrAy. Take (10). ToList ());//Output First 10 Console.WriteLine ("Hill Sort When:" + watch.)
        
        Elapsedmilliseconds);
            The static void Main (string[] args) {list<int> array =new list<int> (); for (int i=0;i<10000;i++) {thread.sleep (1);//This requires a thread to be paused, otherwise a large number of random numbers are equally array. ADD (new Random (int) DateTime.Now.Ticks).
            Next (1,100000));
            Sort Sorter = new sort (); Sorter.
        Shellsort (array);
 }
    }
}

Results:


Java instance:

Oh. My computer on the Java can not start, I am sorry, I will add the next article.

Any suggestions or comments welcome message ^_^

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.