C language implementation of hill sorting (1)

Source: Internet
Author: User

 

Code

# Include <stdio. h>
# Include <stdlib. h>

Int initialstep (INT size );
Void sort (INT array [], int from, int end );
Void insertsort (INT array [], int groupindex, int step, int end );
Void move (INT array [], int startindex, int endindex, int step );
Void printarray (const char * strmsg, int array [], int nlength );

Int main (INT argc, char * argv [])
{
Int data [13] = };
Sort (data, 0, 12 );
Printarray ("shell sort:", Data, 13 );

System ("pause ");
Return 0;
}

/**
* Calculate the initial step size based on the array Length
*
* The formula for choosing step size is: 2 ^ K-1, 2 ^ (k-1)-1 ,..., 15, 7, 3, 1, where 2 ^ K minus one is the step sequence, K
* Sorting round
*
* Initial step: Step = 2 ^ K-1
* Constraints on the initial step size: the value of the initial step size of step <len-1 must be less than the length of the array and minus one (because
* When grouping the first round, try not to divide it into one group, unless the length of the array itself is smaller than or equal to 4)
*
* From the above two relationships, we can know that: 2 ^ k-1 <len-1 relation, where k is round.
* To a step expression, 2 ^ K-1 can be replaced with (Step + 1) * 2-1 (because step + 1 is equivalent to the K-1
* The Wheel step, so multiply by 2 on the basis of step + 1 is equivalent to 2 ^ K), that is, the relationship inequality between the step size and the array length is
* (Step + 1) * 2-1 <len-1
*
* @ Param Len array Length
* @ Return
*/
Int initialstep (INT size ){
/*
* The initial value is set to the minimum step in the step size formula. The maximum initial step value is derived from the minimum step size, which is calculated according to the following formula:
*,..., 2 ^ (k-1)-1, 2 ^ K-1
* If the length of an array is less than or equal to 4, the step size is 1, that is, an array with the length less than or equal to 4 is not grouped. In this case, it is directly degraded to direct insertion.
* Inbound sorting
*/
Int step = 1;

// Test whether the next step meets the conditions. If the conditions are met, the step length is set to the next step length.
While (Step + 1) * 2-1 <size-1)
{
Step = (Step + 1) * 2-1;
}

Printf ("initial step-% d \ n", step );
Return step;
}

/**
* The Implementation of The Sorting Algorithm to sort the specified elements in the array
* @ Param array the array to be sorted
* @ Param from where to start sorting
* @ Param end
* @ Param C Comparator
*/
Void sort (INT array [], int from, int end ){
Int groupindex;
// Initial step, essentially the number of groups per round
Int step = initialstep (end-from + 1 );

// The first loop is to loop the sorting round. (Step + 1)/2-1 indicates the value of the next step.
For (; Step> = 1; step = (Step + 1)/2-1 ){
// Loop each group in each round
For (groupindex = 0; groupindex <step; groupindex ++ ){

// Directly insert and sort each group
Insertsort (array, groupindex, step, end );
}
}
}

/**
* Insert sorting directly
* @ Param array: array to be sorted
* @ Param groupindex sorts the groups in each round.
* @ Param step
* @ Param end: Which element of the entire array is to be ranked?
* @ Param C Comparator
*/
Void insertsort (INT array [], int groupindex, int step, int end ){
Int I, J;
Int startindex = groupindex; // where to start sorting
Int endindex = startindex; // specifies the destination
/*
* Where is the sorting result to be calculated? Starting from the sorting element, you can use the step to determine whether the next element is within the array range,
* If the index is within the array range, the loop continues until the index exceeds the current array range.
*/
While (endindex + step) <= END ){
Endindex + = step;
}

// I starts with the second element in each group
For (I = groupindex + step; I <= end; I + = step ){
For (j = groupindex; j <I; j + = step ){
Int insertedelem = array [I];
// Search for the first element greater than the element to be inserted from the most element in the ordered array
If (array [J]> = insertedelem ){
// After finding the insertion point, move one bit from the insertion point to the next Of all elements
Move (array, J, I-Step, step );
Array [J] = insertedelem;
Break;
}
}
}
}
 

/**
* Move the array elements behind the specified step. The step size specifies the interval between each element.
* @ Param array: array to be sorted
* @ Param startindex
* @ Param endindex to which element
* @ Param step
*/
Void move (INT array [], int startindex, int endindex, int step ){
Int I;
For (I = endindex; I> = startindex; I-= step ){
Array [I + step] = array [I];
}
}

Void printarray (const char * strmsg, int array [], int nlength)
{
Int I;
Printf ("% s", strmsg );
For (I = 0; I <nlength; I ++)
{
Printf ("% d", array [I]);
}
Printf ("\ n ");
}

 

The above code is mainly modified from: http://www.javaeye.com/topic/547734
Http://www.javaeye.com/topic/547735
Other references:
Http://www.cnblogs.com/ziyifly/archive/2008/09/10/1288499.html
Http://www.java2000.net/p11750
Http://mintelong.javaeye.com/blog/467833
Http://student.zjzk.cn/course_ware/data_structure/web/paixu/paixu8.2.2.1.htm
Http://www.cnblogs.com/hualei/archive/2010/08/17/1801850.html

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.