Java selection Sort

Source: Internet
Author: User

Select sort (Selection sort) is a simple and intuitive sorting algorithm. It works as follows. First, find the smallest (large) element in the unordered sequence, place it at the beginning of the sort sequence, and 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.

For example, there is an array of int[] arrs=new int[]{2,5,4,3,2,1}

First, we find the smallest value in the array, stored in the array of the first bit arrs[0] current arrs[0]=2, respectively, and the index position for the 1,2,3,4,5 element comparison, where and arrs[5]=1 comparison, get the minimum value, and exchange the result of the first order is { 1,5,4,3,2,2} Then we look for the second smallest element in the rest of the array (that is, the minimum element in the remaining array), which is placed after the first bit of the array, that is, the position of arrs[1], because the array is now {1,5,4,3,2,2} Take arrs[1] The value of the element (note, the value of this position will change) and the index of the 2,3,4,5 element comparison arrs[1]=5 compare arrs[2]=4, exchange position {1,4,5,3,2,2}arrs[1]=4 Compare arrs[3]=3, swap position { 1,3,5,4,2,2}arrs[1]=3 compare arrs[4]=2, swap position {1,2,5,4,3,2}arrs[1]=2 Compare arrs[5]=2, no interchange for {1,2,5,4,3,2} in this, We have placed the second position in the second place of the array, below we are the third position of the number, select the third small value due to the last order, so the array is now the {1,2,5,4,3,2} take arrs[2] element value (note, the value of this position will change) and index 3, 4,5 the value of the elements of the comparison arrs[2]=5 compare arrs[3]=4, the interchange that occurs, the set of {1,2,4,5,3,2}arrs[2]=4 compare arrs[4]=3, the interchange that occurs, the array of {1,2,3,5,4,2}arrs[2]= 3 Compare arrs[5]=3, the occurrence of the interchange, the group of {1,2,2,5,4,3} in this, we have the third position of the array to hold the third small value sequentially loop ...

To the last obtained array is {1,2,2,3,4,5}

The length of the entire array is 6, so long as we select the first 5 places to store the value, then the value of the last position will not be compared, because the last number is only the last position to put, but also the position of the entire array is the focus of the selection, the element in this position and the elements behind it is compared, When the comparison is not complete, the value of the selected position is not fixed, as if we were to select the value in the second position, when there is no and the subsequent elements are compared, with the comparison, the second position arrs[1] value has been changed, The second small value is 2 until the array comparison is finalized.

1 Code

2
3 PublicclassSelectsort {
4
5 PublicStatic voidMain (string[] args) {
6
7int[] Arrs =Newint[] {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
8System.out.println ("Before array sorting");
9PrintArray (ARRS);
Ten
OneSelectsort (ARRS);
A
-System.out.println ("After array sorting");
-PrintArray (ARRS);
the
-}
-
-//Select sorting Algorithm
+PrivateStaticvoidSelectsort (int[] arrs) {
- //the selected index
+ for(inti = 0; i < arrs.length-1; i++) {
A//The elements that take this index are compared to the elements behind them, where, for each comparison, the value of the arrs[i] position element can be exchanged
at for(intp = i + 1; P < arrs.length; p++) {
-if(Arrs[i] > Arrs[p]) {
-inttemp = Arrs[i];
-Arrs[i] = arrs[p];
-ARRS[P] = temp;
-}
in}
-}
to}
+
-//Print Array Method
the PublicStaticvoidPrintArray (int[] arrs) {
*System.out.print ("[");
$ for(inti = 0; i < arrs.length; i++) {
Panax NotoginsengSystem.out.print (Arrs[i]);
-if(I! = arrs.length-1) {
theSystem.out.print (",");
+}
A}
theSystem.out.println ("]");
+}
-
$}

Java 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.