Insert sorting, binary insert sorting, Hill sorting ideas and Comparison

Source: Internet
Author: User

Insert the basic method of sorting directly: each step inserts an element to be sorted into the appropriate position of a group of elements in the preceding order according to the size of the sorting code, until all elements are inserted.

Insert sorting: When the I-th element is inserted, V [0], V [1], V [2] ...... V [I-1], sorted. at this time, the insertion code of V [I] and V [I-1], V [I-2],... sort the codes and find the inserted position, that is, insert V [I]. The elements at the original position are removed from the back to the back.

Time Complexity: Average number of comparisons O (n2), average number of moves O (n2 ).

Direct insertion sorting is a stable sort. Most of the elements have a higher order aging rate, and the number of comparisons and moves will decrease.

ReferenceCode:

Void sort <t >:: insertsort (datalist <t> & datalist, int N)
{
If (-1 = N)
{
For (INT I = 1; I <datalist. m_ncurrentsize; I ++)
{
Insertsort (datalist, I );
}
Return;
}
Element <t> temp = datalist. m_pvector [N];
Int J;
For (j = N; j> 0; j --)
{
If (temp> datalist. m_pvector [J-1])
{
Break;
} Else
{
Datalist. m_pvector [J] = datalist. m_pvector [J-1];
}
}
Datalist. m_pvector [J] = temp;
}

Binary insert sort: an element sequence V [0], V [1], V [2] in a data table... V [N]. V [0], V [1], V [2] ...... V [I-1] is a sorted element. Insert V [I]. Use a half-fold search to find the insertion position of V [I.

Binary insertion sorting is a stable sorting. When N is large, the total number of sort code comparisons is much better than the worst case of direct insertion sorting, but it is worse than the best case. When the initial sequence of all elements is close to order by sort code, direct insert sorting is less frequently than binary insert sorting. The moving times of binary insertion sorting elements are the same as those of direct insertion sorting, which depends on the initial sequence of elements.

Reference code:

template
void sort : binaryinsert (datalist & datalist, int N)
{< br> If (-1 = N)
{< br> for (INT I = 1; I {< br> binaryinsert (datalist, I);
}< br> return;
}< br> element temp = datalist. m_pvector [N];
int left = 0, Right = n-1;
while (left <= right)
{< br> int middle = (left + right)/2;
If (temp> datalist. m_pvector [Middle])
{< br> left = middle + 1;
}else
{< br> right = middle-1;
}< BR >}< br> for (Int J = n-1; j >=left; j --)
{< br> datalist. m_pvector [J + 1] = datalist. m_pvector [J];
}< br> datalist. m_pvector [left] = temp;
}

Shell sort basic idea: Improved Direct insertionAlgorithm. The element sequence to be sorted has n elements. First, an integer gap (<n) is used as the interval to divide all elements into gap subsequences, all elements with a distance of gap are placed in the same sequence, and directly inserted and sorted in each subsequence. Then narrow down the gap, such as Gap = gap/2, and repeat the sub-sequence division and sorting. At first, due to the large gap value, each sub-sequence has fewer elements and the sorting speed is fast. After the sorting is completed, the gap value gradually decreases and the sub-sequence has more elements. However, due to the previous work basis, most elements are sorted, so the sorting speed is fast.

Hill sorting is an unstable sorting.

Reference code:

Void swap (int * a, int * B)
{
Int X;
X = *;
* A = * B;
* B = X;
}

Void insertion_sort (INT data [], int N, int increment)
{
Int I, J;
For (I = increment; I <n; I + = increment)
{
For (j = I; j> = increment & Data [J] <data [J-increment]; j-= increment)
{
Swap (& Data [J], & Data [J-increment]);
}
}
}

Void shellsort (INT data [], int N)
{
Int I, J;
For (I = n/2; I> 0; I/= 2)
{
For (j = 0; j <I; j ++)
{
Insertion_sort (data, N, I );
}
}
}

 

Another reference code:

Template <class T>
Void sort <t >:: shellsort (datalist <t> & datalist, int gap/* =-1 */)
{
If (-1 = gap)
{
Int Gap = datalist. m_ncurrentsize/2;
While (GAP)
{
Shellsort (datalist, GAP );
Gap = gap/2;
}
Return;
}
For (INT I = gap; I <datalist. m_ncurrentsize; I ++)
{
Insert (datalist, I, GAP );
}
}

Template <class T>
Void sort <t >:: insert (datalist <t> & datalist, int N, int gap)
{
For (INT I = N; I> = gap; I-= gap)
{
If (datalist. m_pvector [I] <datalist. m_pvector [I-Gap])
{
Element <t> temp = datalist. m_pvector [I];
Datalist. m_pvector [I] = datalist. m_pvector [I-1];
Datalist. m_pvector[ I-1] = temp;
}
}
}

Summary: The core of the above three sorting methods is direct insertion and sorting, which is fast for most sorted sequences. Binary insertion sorting basically changes the method of searching the insert position of elements in direct insertion. For a fully disordered sequence, the speed will be faster, but for a majority of ordered sequences, the speed will be slower, hill sorting uses direct insertion sorting for the fast speed of most ordered sequences, so that most of them are sorted first to improve the sorting efficiency.

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.