Selection sort of classical algorithms in Java (Selectionsort)

Source: Internet
Author: User

Selection sort of classical algorithms in Java (Selectionsort)

A) Principle: Each trip selects the smallest element from the records to be sorted, in the order of the ordered sequence, until all records have been sorted. That is: Each trip in n-i+1 (I=1,2,...n-1) The record of selecting the lowest key word as the ordinal sequence of the first a record. Based on this idea, the algorithm mainly has simple selection sorting , tree selection sorting and heap sorting . (This only describes the simple selection sort that is commonly used)

b)   Simple selection sorting basic idea: given array: Int[] arr={< span> n data } 1 data, and arrr[1] interchange ; 2 data Arr[2]~arr[n] data to Span style= "FONT-FAMILY:CALIBRI;" >R[2] interchange ; and so on, > In order to sort data Arr[i]~arr[n] data and r[i] Exchange until all sorts are complete.

C) Example: Array int[] arr={5,2,8,4,9,1};

-------------------------------------------------------

First trip Sort: raw data:5 2 8 4 9 1

Minimum data 1, put 1 in the first place, namely 1 and 5 interchange position,

Sorted results:1 2 8 4 9 5

-------------------------------------------------------

Second trip sort:

data other than 1 {2 8 4 9 5} are compared,2 is the smallest,

Sorted results:1 2 8 4 9 5

-------------------------------------------------------

Third trip Sort:

comparisonof data {8 4 9 5} except 1,2 ,4 min,8 and 4 Exchange

Sorted results:1 2 4 8 9 5

-------------------------------------------------------

Sequence Four:

Comparison of data {8 9 5} except for 1,2,4 ,5 Min,8 and 5 swap

Sorted results:1 2 4 5 9 8

-------------------------------------------------------

The order of five times:

Other than 1,2,4,5 and other data {9 8} are compared, 8 min,8 and 9 swap

Sorted results:1 2 4 5 8 9

-------------------------------------------------------

Note: The method of getting the minimum number of each order : For loop comparison, define a third variable temp, first two number comparison, put the smaller number in temp , and then use the temp to compare with the rest of the data, if there is a smaller than temp data, use it instead of the data in the original temp . With reference to the following code example, I'm sure you've learned the For loop before you sort it out, so it's especially easy to understand.

code example:

//Select Sort Public classSelectionsort { Public Static voidMain (string[] args) {int[] arr={1,3,2, $, $, -, A}; System. out. println ("before swapping:");  for(intNum:arr) {System. out. Print (num+" "); }                //selecting an optimization for sorting         for(inti =0; I < arr.length-1; i++) {//make the first order of the trip            intK =i;  for(intj = k +1; J < Arr.length; J + +) {//Select the smallest record                if(Arr[j] <Arr[k]) {k= J;//Make a note of where the minimum value is currently found                }            }            //At the end of the inner loop, that is, to find the smallest number of cycles, then Exchange            if(i! = k) {//Exchange A[i] and A[k]                inttemp =Arr[i]; Arr[i]=Arr[k]; ARR[K]=temp; }} System. out. println (); System. out. println ("after Exchange:");  for(intNum:arr) {System. out. Print (num+" "); }    }}

Operation Result:

Select the time complexity for sorting: The comparison number of simple selection sorts is independent of the initial ordering of the sequence. Assuming that the sequence to be sorted has n elements, the number of comparisons is always n (N-1)/2. The number of moves is related to the initial ordering of the sequence. When the sequence is positive, the minimum number of moves is 0. When the sequence is reversed, the maximum number of moves is 3N (N-1)/2.

So, in summary, the time complexity of simple sorting is O (N2).

Selection sort of classical algorithms in Java (Selectionsort)

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.