Data structure sorting (Hill sort)

Source: Internet
Author: User
Tags array length rounds

//sort--Hill sort method#include <stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>/*emphasis: Online, the Book of the Hill sorting method has problems Hill sort does not press an increment D, divides an array into small arrays, inserts a sort on each array, the theory does not apply the following code does not conform to the time complexity of hill sort Real Hill sort steps the first step through the industry general d =  Array length/3 + 1; To find out the increment D second step: Take the first element of the array, make a new array by the interval of Delta D, and sort the array incrementally. Step three: Get the increment d = D/3 + 1 again; Or get the first element of the array to make a new array of increments of D, to sort the array in increments fourth step: Repeat step three note: ① each insertion is a new array that corresponds to the first element. There is no array for the second element (or an array of nth elements) to sort ② Hill sort when d=1 still executes a round, when d=1 is actually an insertion order for the entire array I personally feel that this is a few more steps than the insertion sort.*///Position ExchangevoidMyswap (int*arr,intAintb) {    inttemp =Arr[a]; Arr[a]=Arr[b]; ARR[B]=temp;}//Print ArrayvoidPrint (int* Arr,intnum) {    if(arr = =NULL) {printf ("the incoming parameter cannot be empty! \ n"); return; }    inti =0;  for(inti =0; i < num; i++) {printf ("%5d", * (arr +i)); } printf ("\ n");}//Hill SortvoidShellsort (int*arr,intLen) {    //define the number of elements per interval    intGap =Len; inti =0, j=0, temp=0, k=0;  Do    {        //The interval of the industry unification experiment will be 1/3 average of the length of the array the best case after several times, the convergence is 1Gap = Gap/3+1; //The first set of data plus D1 Gets the data for each element in the first set of data in the corresponding position in the subsequent group         for(i = gap; i < len; i + =Gap) {k=i; Temp=Arr[k]; //at this point, the element of each interval D1 is exactly an array of I loops to perform an insert sort on the elements in this new array//I-gap is the largest subscript of the subarray now the subscript of the element to be inserted is I             for(j = i-gap; J >=0&& Arr[j] >= temp; J-=Gap) {                //Insert SortArr[j + gap] =Arr[j]; K=J; } Arr[k]=temp; }        //Print Arrayprintf"\ n%d rounds \ n", GAP);    Print (arr, Len); }  while(gap>1);}voidShellSort2 (int*arr,intLen) {    //define the number of elements per interval    intGap =Len; inti =0, j =0, temp =0, k =0, m=0;  Do    {        //The interval of the industry unification experiment will be 1/3 average of the length of the array the best case after several times, the convergence is 1Gap = Gap/3+1;  for(M =0; M < Gap; m++)        {            //The first set of data plus D1 Gets the data for each element in the first set of data in the corresponding position in the subsequent group             for(i = gap+m; i < len; i + =Gap) {k=i; Temp=Arr[k]; //at this point, the element of each interval D1 is exactly an array of I loops to perform an insert sort on the elements in this new array//I-gap is the largest subscript of the subarray now the subscript of the element to be inserted is I                 for(j = i-gap; J >=0&& Arr[j] >= temp; J-=Gap) {                    //Insert SortArr[j + gap] =Arr[j]; K=J; } Arr[k]=temp; }        }        //Print Arrayprintf"\ n%d rounds \ n", GAP);    Print (arr, Len); }  while(gap>1);}voidTest () {inti =0; intarr[Ten] = {0 }; //defining time-type variablestime_t ts; //generate random number seedSrand ((unsignedint) Time (&ts));  for(i =0; I <Ten; i++) {Arr[i]= (int) (rand ()% -); }    //Print Arrayprintf"\ n raw data----\ n"); Print (arr,Ten); //Hill Sortprintf"the data after hill is sorted \ n"); //Shellsort (arr,10);ShellSort2 (arr,Ten); Print (arr,Ten);}voidMain () {Test (); System ("Pause");}

Data structure sorting (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.