Java algorithm (i) Direct selection sorting

Source: Internet
Author: User

first, the basic concept

first, the smallest (large) element is found in the unordered sequence, which is placed at the starting position of the sort sequence (note: to exchange the largest and smallest elements and the starting elements),

then, continue looking for the smallest (large) element from the remaining unsorted elements, and then place it at the end of the sorted sequence. And so on until all elements are Sorted.

second, the characteristics of the algorithm

Is stable: false

Average Time complexity: O (n^2)

Worst Time complexity: O (n^2)

Optimal time complexity: O (n^2)

 packagecom.jiafuwei.java;/*** Java Selection sort *@authorJFW **/ public classSelectionsort { public Static voidmain (string[] Args) {int[] collections = {36,23,56,789,342,456,34,7,234,4578,32}; Selectionsort Selectionsort=NewSelectionsort ();        Selectionsort.sort (collections);  for(inti = 0; I < collections.length; i++) {System.out.print (collections[i]+" "); }    }         public voidSortint[] Data) {         for(intk=0;k<data.length;k++){            intMin =k; //Compare the first number in the loop with the number in the back if it is greater than the next number and then record the subscript value of the subsequent Number.//notice here that the i=k is starting from behind the K loop .             for(inti=k;i<data.length;i++){                if(data[min]>data[i]) {min=i; }            }            //record a number in this loop.            inttemp =data[k]; //turn a number in this loop into the smallest numberdata[k] =data[min]; //change the position of the previous minimum number to the first number at that timedata[min] =temp; }    }}

Java algorithm (i) Direct selection sorting

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.