Data structure and algorithm (i) Select sort

Source: Internet
Author: User

Select sort

Select sort: First Find the smallest element in the array , and second, swap it with the first element of the group (if the first element is the smallest element then swap it yourself). Again, find the smallest element in the remaining element and swap it with the second element of the array . This is repeated until the entire array is sorted. This is the selection sort, because it is the smallest of the remaining elements that are constantly selected.

Conclusion: for an array of length n, choosing a sort requires approximately N2/2 and N-Times.

Characteristics:

    1. run time is independent of input. scanning the array once to find the smallest element does not provide any information for the next scan. This property is a disadvantage in some cases, because the person using the selection sort may be surprised to find that an array with an ordered array or a primary key that is all equal, and an array that is randomly arranged by an element, are sorted as long as the ordering time.
    2. data movement is minimal. Each interchange changes the value of the elements of the two array, so the Select sort uses n-Times Exchange- the number of interchanges and the size of the arrays are linearly related.
1  Public Static voidsort (comparable[] a) {2         //sort A in ascending order3         intL =a.length;4          for(inti = 0; i < L; i++) {5             //The subscript of the minimum value is initially I6             intMin =i;7              for(intj = i + 1; J < L; J + +) {8                 //Compare Size9                 if(A[min].compareto (a[j]) > 0) {Ten                     //assigns the subscript of the minimum value to min OneMin =J; A                 } -             } -             //Place the lowest value subscript and "I" transposition (minimum value transposition) the Exch (A, I, min); -         } -  -}

Data structure and algorithm (i) Select sort

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.