Common sorting algorithms

Source: Internet
Author: User

1. Insert sorting

Composed of N-1 sort, for p = 1 to P = N-1, insert sort ensures that the elements from position 0 to position P are sorted.

Time Complexity: O (N ^ 2)

Code

Void Insertionsort (elementtype A [], Int N)
{
Int J, P;
Elementtype TMP;
For (P = 1 ; P < N; P ++ )
{
TMP = A [J]; // Save a [J] because it will be inserted to a previous position.
For (J = P; j > 0 && A [J - 1 ] > TMP; j -- ) // Elements greater than a [J] Move one by one
{
A [J] = A [J - 1 ];
}
A [J] = TMP;
}
}

 

2. Hill sorting

Hill sorting uses a sequence H1, H2, H3, HT, called incremental sorting. After using the incremental HK sort, we have a [I] <A [I + HK] for each I, and all elements separated by HK are sorted.

Time Complexity: O (N ^ (1 + a), where 0 <A <1.

Code

// CodeNot very easy to understand, using a three-layer Loop
Void Shellsort (elementtype A [], Int N)
{
Int J, P, increment;
Elementtype TMP;
For (Increment = N / 2 ; Increment > 0 ; Increment /= 2 )
{
For (P = Increment; P < N; P ++ )
{
TMP = A [p];
For (J = P; j > = Increment; j -= Increment)
{
If (A [J] < A [J - Increment])
A [J] = A [J - Increment];
Else
Break ;
}
A [J] = TMP;
}
}
}

 

3. Heap sorting

Idea: create a small top heap and then perform n deletemin operations.

Time Complexity: O (nlogn). In practice, it is slower than Sedgewick's Hill sorting.

Space complexity: O (N), used to create a heap Array

 

4. Merge Sorting

Recursively sorts the array into two parts, and then merges them into an array.

Time Complexity: O (nlogn)

Space complexity: O (N)

Code

Void Merge (elementtype A [], elementtype tmparray [], Int LPOS, Int Rpos, Int Rightend)
{
Int I, leftend, numelements, tmppos;
Leftend = Rpos - 1 ;
Tmppos = LPOS;
Numelements = Rightend - LPOS + 1 ;
While (LPOS <= Leftend && Rpos <= Rightend)
Tmparray [tmppos ++ ] = (A [LPOS] < A [rpos]) ? A [LPOS ++ ]: A [rpos ++ ];
While (LPOS < Leftend)
Tmparray [tmppos ++ ] = A [LPOS ++ ];
While (Rpos < Rightend)
Tmparray [tmppos ++ ] = A [rpos ++ ];

For(I=0; I<Numelements; I++, Rightend--)
A [rightend]=Tmparray [rightend];
}

5. Quick sorting

For small arrays (n <20), fast sorting is better than inserting. A good cutoff range is n = 10. If the value is greater than 10, use a fast sort.

The following four steps are included:

① If the number of elements in S is 0 or 1, return

② Take the s intermediate element V as the pivot element

③ Divide S-{v} into two different sets: S1 (less than V) and S2 (greater than V ).

④ Return quicksort (S1), followed by V, followed by quicksort (S2)

Time Complexity: O (nlogn)

Programming philosophy: 1. Select the pivot element, take the first and the third elements in the middle, sort the elements, and rank the smallest elements at the top, and the largest elements at the end as the pivot element.

2. Place the pivot element in the right-1 position during sorting.=Left+1; J=Right-2; Begin the Exchange Process

Code

Elementtype median3 (elementtype A [], Int Left, Int Right)
{
Int Center = (Left + Right) / 2 ;
If (A [left] > A [center])
Swap ( & A [left], & A [center]);
If (A [left] > A [right])
Swap ( & A [left], & A [right]);
If (A [center] > A [right])
Swap ( & A [center], & A [right]);

Swap (&A [center],&A [Right-1]);
ReturnA [Right-1];
}

Qsort (elementtype A [],Int Left, Int Right)
{
Int I, J;
Elementtype comment;
If (Left + 9 < Right) // Arrays with more than 10 elements use the quick rank
{
Bytes = Median3 (A [], left, right );
I = Left + 1 ;
J = Right - 2 ; While (1) {
While (A [I ++ ] < Token );
While (A [J -- ] > Token );
If (I < J)
Swap ( & A [I], & A [J])
Else
Break ; }
Swap ( & A [I], & A [Right - 1 ]);
Qsort (A, left, I - 1 );
Qsort (A, I + 1 , Right );
}
Else
Insertsort (A, right - Left + 1 );
}

6. Select sort directly

Description: selects the smallest element in the array and exchanges it with the first element of the array. Then, selects the second small element in the array and exchanges it with the second element until it is completed.

Select sort to compareN (N-1)/2Times, that isN2, While the exchange only needsN-1Times

The time spent on sorted or random files is consistent, that is, the execution time is forced.

When the data items are relatively large and the keys are relatively small, it takes a lot of time to move the elements.AlgorithmElements are frequently moved.

Code

Void Sort (elementtype A [], Int N)
{
Int I, J;
Int Min;
For (I = 0 ; I < N - 1 ; I ++ )
{
Min = I;
For (J = I + 1 ; J <= N - 1 ; J ++ )
If (A [J] < A [Min]) min = J;
Swap (A [I], a [Min]);
}
}

 

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.