Sorting Algorithm-two options for sorting

Source: Internet
Author: User

Select sorting is a Sort Algorithm Based on repeated selection. Directly select sorting is the simplest one.

Select sort directly:

Algorithm:

Suppose there is an array of a [n], first find the smallest element, save it in a [1], and then find the smallest element of the remaining n-1 elements, in a [2], repeat this process until the second largest element is found.

Code:

 

# Include <stdio. h>

Const int max = 10;

Void selectionsort (int *)
...{
Int I, J, K, temp;
For (I = 0; I <max-1; I ++)
...{
K = I;
For (j = I + 1; j <Max; j ++)
If (A [J] <A [k])
K = J;
If (K! = I)
...{
Temp = A [k];
A [k] = A [I];
A [I] = temp;
}
}
}


Int main ()
...{
Int A [Max];
Int I;
Int Nums = max;
Printf ("Please input % d numbers:", Nums );
For (I = 0; I <Max; I ++)
Scanf ("% d", & A [I]);
Selectionsort ();
For (I = 0; I <Max; I ++)
Printf ("% d", a [I]);
Return 0;
}

 

It can be seen that the minimum number of element exchanges is 0, and the maximum is n-1. at the same time, the number of element assignments in the exchange process is between 0 and 3 (n-1), and the number of comparisons with keywords are all n (n-1)/2, therefore, the time complexity of the algorithm is O (n2 ).

The main operation to select sorting is to compare keywords between elements. Therefore, to improve the Direct selection of sorting, you can start with reducing the number of comparisons.

There is a theorem:

Any algorithm based on the dual element of comparison to find the exclusive among n elements must be compared at least N-1 times (<computer programming art>)

However, after careful consideration, we can understand that this theorem applies only to the first selection step. we can improve the Direct selection algorithm by imitating the selection of sports competitions. The subsequent selection can be carried out by generating information after the first selection.

Title sorting:

The kind sorting is also called the tree-based sorting. You can clearly understand the kind of kind sorting by this name.

Algorithm:

We can use a full binary tree with N leaf nodes to organize this algorithm.

First, let the two elements compete. When the winner of the leaf node moves up a branch, a special flag is used to replace the original position (such as-∞ ). when the winner of a non-leaf node moves up, the original position of the node is filled by the winner of the node below it.

Analysis:

When a full binary tree has n leaf nodes, its depth should be log2n + 1, so the minimum (large) keyword exists, each time you select a small keyword, you only need to perform log2n comparison. Therefore, its time complexity is O (nlog2n ).

However, the disadvantages of the Golden label sorting are obvious. Additional auxiliary space is required, and there are redundant comparisons when selecting the most value. The heap sorting is also a good solution to this problem.

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.