Code
# Include <stdio. h>
# Include <stdlib. h>
Void shellsort (int A [], int index );
Void printarray (const char * strmsg, int array [], int nlength );
Int main (INT argc, char * argv [])
{
Int data [13] = };
Shellsort (data, 13 );
// Printarray ("shell sort:", Data, 13 );
System ("pause ");
Return 0;
}
/* Hill sorting ideas:
Three Layers of loops are required,
The first layer of loops is used to control the change of step size, and each step is sorted every time it is reduced;
The second-layer loop is used to control each set divided by step;
The layer-3 Loop is used to insert and sort individual sets;
Note: Observe the relationship between the step size and the number of groups through drawing. For arrays with 13 elements,
When the step size is 8, the array can be divided into eight groups, five of which have two elements, and three have only one element.
When the step size is 7, the array can be divided into seven groups. One group has one element, and the other six groups have two elements.
When the step size is 6, the array can be divided into six groups. One group has three elements, and the other five groups have two elements.
When the step size is 5, the array can be divided into five groups, three of which have three elements, and two of which have two elements.
When the step size is 4, the array can be divided into four groups. One group has four elements, and three groups have three elements.
*/
Void shellsort (int A [], int index ){
Int I, j, k; // cyclic count variable
Int temp; // temporary variables
Int change; // whether the data is changed
Int datastep; // The split step of the Set
Int pointer; // The processing position.
Datastep = (INT) index/2; // The Interval Length of the initial set
While (datastep! = 0) // The sequence can still be split
{
Printf ("===================================\ N ");
// Process each set
/* (J represents the position of the second element of each group before J doubles the value of datastep;
Before J is increased to three times that of datastep, J represents the position of the third element of each group, and so on.
If J-datastep <0 indicates that J is already the first element of this group .) */
For (j = datastep; j <index; j ++ ){
Change = 0;
Temp = A [J]; // Save the elements to be sorted in the current set to the Temporary Variable
// (Calculate the position of the first element)
Pointer = J-datastep; // calculates the position of the last element in the list of sorted elements in the current set.
K = 0;
// Insert and sort the values in the Set (edge comparison and backward shift)
While (temp <A [pointer] & pointer> = 0 & pointer <= index ){
Printf ("Current swapping element: % d (A [% d])-% d (A [% d]) \ n", a [pointer], pointer, A [pointer + datastep], pointer + datastep );
// Move the sorted elements that are larger than the elements to be sorted
A [pointer + datastep] = A [pointer];
Printarray ("result of this exchange:", A, index );
// Calculate the position of the next sorted element to be compared
Pointer = pointer-datastep;
Change = 1;
K ++;
}
Printf ("This set step is % d, the number of exchanges is % d \ n", datastep, k );
// (Insert the elements to be sorted to the last vacant space)
A [pointer + datastep] = temp;
If (Change ){
// Print the current sorting result
Printarray ("current round sorting result:", A, index );
}
Printf ("------------------------ \ n ");
}
Datastep = datastep/2; // calculate the Interval Length of the next split
}
}
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 ");
}