java-Select Sort

Source: Internet
Author: User
public static void Selectsort (int[] a) {if (a = = null) {return;} int n = a.length;int Minindex = 0;//A minimum number from the unordered area is placed at the end of the ordered area, initially thinking that the entire array is unordered, choosing a minimum number to place the first for (int i=0; i<n; i++) {Minindex = i;for (int j=i+1; j<n; J + +) {if (A[j]<a[minindex]) {minindex = j;}} Swap position, place the smallest element in the unordered area to the last int temp = a[i];a[i] = A[minindex];a[minindex] = temp in the ordered area; }}


Similar to the direct selection sort and insert sort, the data is divided into ordered and unordered areas.

The difference is that the insertion sort is to insert the first element of the unordered area directly into the ordered area, initially by default the first element is ordered, step by step to reduce the range of unordered extents.

Selecting a sort is to first select the smallest element in the unordered area and then place it in the first position behind the ordered area.

At the same time, the choice of sorting is also an unstable sort, for example, the sequence of 4,3,4,1,2, the first exchange will be 4 and 1 exchange, then the original sequence of the relative order of two 4 is destroyed.

java-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.