Array element Selection sort

Source: Internet
Author: User

Topic Analysis:

By observing that this topic is implemented to sort the array elements {13,46,22,65,3}

    1. When it comes to array ordering, it is necessary to compare the size of the element values, and by discovering that we want to finish the sort by several comparisons.
    2. The first element to be compared with each lap is compared to the last element of the array, followed by the element of the array, and the small value is placed in the first array element, and after the loop of the array, the minimum element value is swapped to the first element.
    3. Once the array is recycled, the second small element value is swapped to the second element. In this way, the array elements are sorted after the arrays have been looped in circles. This sort of ordering is what we call the selection sort.

Problem Solving steps:

    1. Use a For Loop (outer loop) to specify the number of laps to loop (by illustration, the number of laps in an array loop is 1)
    2. In each lap, the first element to be compared through the For loop (inner loop) and the array element following the element are compared to the last element of the array, and the small value is placed in the first array element
    3. In each lap, the first element to be involved in the comparison is determined by the circle loop. As shown

A) when the first loop element is compared, the first element to be compared is the first element of the array, which is the element indexed to 0

b) Second loop element comparison, the first element to be compared to the second element of the array, that is, an element indexed to 1

c) and so on, it concludes that the first element to be compared is the nth element of the array, that is, the element that is indexed as n-1, when the nth loop element is compared.

The code is as follows:

//Select Sort Public Static voidSelectsort (int[] arr) {    //function//the outer loop is used to control the number of loops in the array     for(inti = 0; i < arr.length-1; i++) {        //The inner loop is used to perform the comparison of element values, exchanging small element values into the first element to be compared.         for(intj = i+1; J < Arr.length; J + +) {            if(Arr[i] >Arr[j]) {                inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; }        }    }}

Array element Selection 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.