2. Sort the 10 numbers in the selected sorting mode.

Source: Internet
Author: User
[Principle] First, find the smallest (or largest) element in the unordered sequence, and store it in the starting position of the sequence. Then, find the smallest (or largest) element from the remaining unordered elements and put it at the end of the sorted sequence. And so on until all elements are sorted. [Complexity and stability]

(1) Select the sorting time complexity
The time complexity of sorting is O (n2 ).
Suppose there are n numbers in the sorted series. The time complexity of traversing a trip is O (n), and The N-1 needs to be traversed. Therefore, the time complexity of sorting is O (n2 ).

(2) Select sorting Stability
Sorting is a stable algorithm that satisfies the definition of a stable algorithm.
Algorithm stability -- assume that a [I] = A [J] exists in the sequence. If a [I] is before a [J], and after sorting, A [I] is still before a [J. The sorting algorithm is stable!

 

[Code]

# Include <iostream> using namespace STD; Template <class T> // use void selection_sort (T array [], const int size) for the template; int main () {int A [10]; cout <"Please input 10 numbers:" <Endl; For (INT I = 0; I <10; I ++) // input content {CIN> A [I];} selection_sort (A, sizeof (a)/sizeof (INT); // call the function, note the meaning of sizeof (a)/sizeof (INT) for (Int J = 0; j <10; j ++) // output Array {cout <A [J] <"" ;}return 0 ;}template <class T> void selection_sort (T array [], cons T int size) {int min; // determines the minimum value of the array subscript int temp; // the median value for (INT m = 0; m <size-1; m ++) // determine the boundary of the outer loop (that is, the maximum value of the loop) {min = m; // determine the subscript of the minimum value of the array and fix the sorted for (INT n = m + 1; n <size; n ++) // The inner loop is used to determine the minimum data {If (array [N] <array [Min]) // to determine the minimum value {min = N; // determine the minimum array subscript} If (Min! = M) // exchange data {temp = array [m]; array [m] = array [Min]; array [Min] = temp ;}}}

 

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.