Insert Sorting Algorithm (direct, half-fold, Hill)

Source: Internet
Author: User

Insert sort

 

Insert directly sortedAlgorithmThe idea is:

 Base on the first element of the array, Index = 1 starting from the second element, and then n.

Then, compare the order from subscript 0 to subscript index-1 with the subscript index of the element to be inserted, find the appropriate insert position, and then place the current element to this position.
Two cycles: one is from the second element, followed by N

The second is to find a suitable location, move the array, and insert elements.

Two processes: one is to find the Insert Location

Second, place the corresponding elements in the insert position.

Time Complexity: O (N ^ 2)

The algorithm is as follows:

1. Follow the process:

 BoolSort_by_insert (Int* SRC,IntLen) 
{
IntIndex =1;
IntI, j, temp;
While(Index <Len)
{
Temp = SRC [Index];

//Insert Location
For(I =0; I <index; I ++)
{
If(SRC [I]> temp)
{
Break;
}
}

//Move Array records
For(J = index; j> I; j --)
{
SRC [J] = SRC [J-1];
}
SRC [I] = temp;

Index ++;
}

Return True;
}

Merge two processes

 BoolSort_by_insert_ex (Int* SRC,IntLen) 
{
IntIndex =1;
IntI, temp;
While(Index <Len)
{
Temp = SRC [Index];
For(I = index; I>0; I --)
{
If(SRC [I-1]> Temp)
{
SRC [I] = SRC [I-1];
}
Else
{
Break;
}
}
SRC [I] = temp;
Index ++;
}

Return True;
}
3. Improved insertion sorting
 
The insertion sorting algorithm mainly involves two processes:ComparisonAndMobile. To increase the speed, you can only improve the speed from these two aspects.
 
1-fold mixed sorting (Reduce the number of comparisons)
 BoolSort_by_half_insert (Int* SRC,IntLen) 
{
IntIndex =1;
IntI, j, temp;
While(Index <Len)
{
Temp = SRC [Index];

//Insert Location
I =0;
J = index;
While(I <j)
{
//Half for comparison
If(Temp <= SRC [(I + J )/2])
{
J = (I + J )/2-1;
}
Else
{
I = (I + J )/2+1;
}
}

//Move Array records
For(J = index; j> I; j --)
{
SRC [J] = SRC [J-1];
}
SRC [I] = temp;

Index ++;
}

Return True;
}
 
 
 
Sishir sorting
 
Basic Idea: divide the columns to be sorted into several groups according to the same interval, and insert and sort each group separately.
 
 
 Bool Sort_by_shell (Int * SRC, Int Len)
{
Int Start = 1 ;
Int I, step, temp;

// Obtains the maximum value of an incremental sequence in the H = 3 * H + 1 mode.
For (Step = 0 ; Step <= Len/ 9 ; Step = Step * 3 +1 )
;
For (Step; Step> 0 ; Step/= 3 )
{
// Sort by hill
For (START = step; Start <Len; Start ++)
{
Temp = SRC [start];
// Group sorting
For (I = start-step; I> = 0 ; I-= step)
{
If (SRC [I]> temp)
{
SRC [I + step] = SRC [I];
}
Else
{
Break ;
}

SRC [I] = temp;
}
}
}

Return 0 ;
}
 


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.