Selection sort of Java base algorithm

Source: Internet
Author: User

Select sort

Select sort (Selection sort) is a simple and intuitive sorting algorithm. It works by selecting the smallest (or largest) element of the data element to be sorted each time, storing it at the beginning of the sequence until all the data elements to be sorted are exhausted.

Complexity of Time

Select the sort interchange operation between 0 and (n-1) times. The comparison operation for the selection sort is between n (n-1)/2 times. The assignment of the select sort is between 0 and 3 (n-1) times. Comparison number O (n^2), the number of comparisons is independent of the initial state of the keyword, the total number of comparisons n= (n-1) + (n-2) +...+1=n* (n-1)/2. The number of exchanges O (n), preferably, has been ordered, exchanged 0 times, the worst case exchange n-1 times, reverse exchange N/2 times. The number of exchanges is much smaller than the bubble sort, because the exchange requires more CPU time than the comparison requires more CPU time, the choice of sorting is faster than bubble sort when the N value is small.

Algorithm stability

Select Sort is an unstable sort method (for example, sequence [5, 5, 3] swaps the first [5] with [3] for the first time, causing the first 5 to move back to the second 5).

Select Sort Code

Through the outer loop, the default first number is the smallest subscript, assigned to K, through the inner loop to find a number smaller than the k subscript, and recorded, within the loop to find the minimum record of this trip, and then the smallest comparison with the outer loop, if 2 are not equal to replace the position. That is, K always keeps the subscript of the smallest number in the sorted data, and then swaps the data with the current position I.

 PackageCom.roc.select;/*** Select Sort *@authorLIAOWP **/ Public classSelectsort { Public Static voidMain (string[] args) {int[] data={99,45,32,56,12}; intI,j,k,temp=0; System.out.print ("Raw Data");        print (data);        System.err.println ();  for(i = 0; i < data.length-1; i++) {//Outer Loop, the number of cycles is length-1K=i;//The default is to take the first I as the smallest or largest and record it             for(j = i+1; J < Data.length; J + +) {//Inner Loop, number of comparisons =length-i                if(Data[j]<data[k])//If the number of J is less than K (the largest or smallest of the records just recorded), record itk=J; if(k!=i) {//if the maximum or minimum number is not self, replace the positiontemp=Data[i]; Data[i]=Data[k]; DATA[K]=temp;                    System.err.println ();                    print (data);                System.err.println (); }            }        }        //print (data);    }         Public Static voidPrintint[] Array) {         for(inti = 0; i < Array.Length; i++) {System.err.print (Array[i]+" "); }    }}

Raw Data 99 45 32 56 12

45 99 32) 56 12

32 99 45) 56 12

45 99 32) 56 12

12 99 32) 56 45

12 32 99) 56 45

12 56 99) 32 45

12 32 99) 56 45

12 32 56) 99 45

12 32 45) 99 56

12 32 45) 56 99

Selection sort of Java base algorithm

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.