Insert sorting algorithm-Direct Insertion Algorithm, semi-sorting algorithm, and Hill Sorting Algorithm (C # implementation)

Source: Internet
Author: User

Insert sortAlgorithmIt mainly includes direct Insertion Algorithm, semi-sorting algorithm (Binary Insertion Algorithm), and Hill sorting algorithm. The latter two are improved direct insertion algorithms. Therefore, directly inserting an algorithm is the basis. Here, we will insert the analysis and encoding of the algorithm directly.

Directly Insert the sort idea of the algorithm: assuming that the ordered array is small to large, array [0], array [1], array [2],..., array [N-2], array [n-1], then compare the value array [N] to the preceding ordered array from the back to the front, after finding a location smaller than the value array [N] In the ordered array, insert array [N] to this location and combine it into a new ordered array.

Direct Insertion Algorithm--CodeAs follows:

 //  Insert the sorting algorithm directly (pass the name of the array to be sorted, that is, the address of the array. The operations on the input parameter array are reflected in the real parameter array)  
Private Static Void Insertsortionfunction ( Int [] Array)
{
Try
{
Int Temp = 0 ; // Temporary Variable to store the value to be sorted
For ( Int I = 1 ; I < Array. length; I ++ ) // Insert all unordered values into the ordered array in sequence. Note: The subscript starts from 1 because the operation is performed on the same array.
{
Temp
= Array [I]; // Record the value of the sorted array to be inserted.
Int Index = I - 1 ; // Record the position at the end of the ordered array
While (Index > = 0 && Array [Index] > Temp) // Cyclically traverse the preceding ordered array and compare it with the value to be sorted in ascending order.
{
Array [Index
+ 1 ] = Array [Index]; // If index> = 0 and the value is greater than the value to be ranked, move the value here one bit backward.
Index -- ; // Index -- traverse the ordered array forward
}
Array [Index
+ 1 ] = Temp; // Due to the previous index --, temp is inserted at index + 1.
}
}
Catch (Exception ex)
{}
}

The semi-sorting algorithm is an optimization of the direct Insertion Algorithm. The core of the optimization is to view the values (A) in the middle of the ordered array and the values to be inserted (temp) through the semi-sorting algorithm) if a> = temp, turn to the left of the half to continue the half search; if a <temp, turn to the right of the half to continue the half search. Until the Left and Right subscripts are the same, the half-folded subscripts also point to the same position, and then perform the last loop. The final result is: the difference between the left and right subscripts is 1, in addition, the original subscript on the left points to a position greater than temp. The original subscript on the right points to a position smaller than temp, that is, array [biggerindex] <temp <array [smallerindex].

Semi-Sorting Algorithm-- The Code is as follows:

//Semi-sorting algorithm (passing the name of the array to be sorted, that is, the address of the array. The operations on the input parameter array are reflected in the real parameter array)
 Private     Static     Void  Binaryinsertionsortfunction (  Int  [] Array)
{
Try
{
Int Smallerindex = 0 ; // Start position of the record ordered array
Int Biggerindex = 0 ; // End position of the recorded ordered array
Int Midindex = 0 ; // Record the middle position of the obtained ordered array (the key of the half-fold method: the half-fold position)
Int Temp; // Number of records with rows
For ( Int I = 1 ; I < Array. length; I ++ ) // Insert a number into an ordered array cyclically (I starts from 1 because the operation is the same Array)
{
Temp
= Array [I]; // Record the value of the sorted array to be inserted
Biggerindex = I - 1 ;
// When smallerindex = biggerindex, enter the last cycle: smallerindex points to an array greater than temp, biggerindex points to an array smaller than temp
While (Smallerindex <= Biggerindex)
{
Midindex
= (Smallerindex + Biggerindex) / 2 ; // Determine the half-fold position
If (Array [midindex] > = Temp) // Value> = temp
{
Biggerindex
= Midindex - 1 ; // Biggerindex moves one digit forward based on midindex
}
Else
{
Smallerindex
= Midindex + 1 ; // Smallerindex moves one digit backward Based on midindex
}
}
For ( Int J = I - 1 ; J > Biggerindex; j -- ) // Move the values larger than temp in the ordered array backward by one
{
Array [J
+ 1 ] = Array [J]; //
}
Array [biggerindex
+ 1 ] = Temp; // Insert temp into biggerindex + 1 Because array [biggerindex] <temp <array [smallerindex]
}
}
Catch (Exception ex)
{}
}

Hill sorting is also an improvement of direct insertion of sorting algorithms. The basic idea is to divide unordered series into several small subsequences, and then directly insert and sort the subsequences.
The time performance is better than the direct insertion sorting algorithm, but it is an unstable sorting. the time complexity is O (nlogn ).
The hill sorting algorithm is mainly divided into three repetition cycles:
The first loop --> group by gap size. Initialization starts from array. Length/2 and decreases to 1 in sequence.
The second loop --> sort all Groups
The third re-cycle --> sort by direct insertion in the group

Hill Sorting Algorithm-- The Code is as follows:

 Private     Static    Void  Shellsortfunction (  Int  [] Array)
{
Try
{
Int Length = Array. length;
Int Temp = 0 ;
For ( Int Gap = Length / 2 ; Gap > 0 ; Gap -- ) // The first cycle is grouped by the gap size.
{
For ( Int I = 0 ; I < Gap; I ++ ) // The second loop sorts all groups.
{
For ( Int J = I; j < Length; j = J + GAP) // In the third loop, direct insertion in the group is sorted
{
Temp
= Array [J];
Int Index = J - Gap;
While (Index > = 0 && Array [Index] > Temp)
{
Array [Index
+ Gap] = Array [Index];
Index
= Index - Gap;
}
Array [Index
+ Gap] = Temp;
}
}
}
}
Catch (Exception ex)
{}
}

....

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.