Selection sort of algorithm basis

Source: Internet
Author: User

  The selection of sorting algorithm is the entry algorithm, I believe that learning data structure and algorithm of the students are from the beginning of this sort of bar. Freshman Contact Select sorting algorithm when it feels good simple (compared to bubble sort), because it is implemented in a way closer to the way people think. As the name implies, choose the sorting algorithm, at least there will be a "select" Process it? Yes, the selection sort is set to two regions, one for the ordered area, and the other to the unordered area. Either the sorted area and the wait sort area are the same. In the selection sort, each loop selects a minimum (or maximum) element in the unordered area, swapping it with the current element, and if there is no value smaller (or larger) than the current element in the unordered area, it is already the smallest value in the unordered area, and its appropriate position is the current position.

For example, there is an array of length 4 a,{3,2,4,1}. So our first order is to determine the first position, that is, the position of the subscript 0, at this time the position of the subscript 0 is 3, followed by the element comparison, found 2:3 small, then the subscript 0 element and the element of the small mark 1 Exchange position, at this time a[0] is 2, continue to compare found a[3] smaller than a[1], Then a[0] and a[3] swap locations, and so on.

  

1  PackageSelectionsort;2 3 Importjava.util.Arrays;4 /*5 * Select Sort6 * This example is ascending7  */8  Public classSelectsort {9      Public Static voidSelectsort (int[] Array) {Ten          One  A          for(inti = 0; i < Array.Length; i++) { -             /* - * Each trip sort By default this position is the minimum value, if a smaller one is found later, the two exchange positions the              */ -              for(intj = i + 1; J < Array.Length; J + +) { -                 if(Array[j] <Array[i]) { -                     intTMP =Array[i]; +Array[i] =Array[j]; -ARRAY[J] =tmp; +                 } A             } at  -         } -  -     } -  -      Public Static voidMain (string[] args) { in  -         int[] arr = {2,1,7,3,5}; toSystem.out.println ("Before sorting:" +arrays.tostring (arr)); +          - Selectsort (arr); theSystem.out.println ("After sorting:" +arrays.tostring (arr)); *  $     }Panax Notoginseng}

Selection sort of algorithm basis

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.