Java Sorting Algorithm _ 010 select sorting

Source: Internet
Author: User
Package com. arithmetic; import java. util. arrays; // selection sort is a simple and intuitive sorting method.Algorithm. // Its working principle is as follows. First, find the smallest (large) element in the unordered sequence, store it to the starting position of the sorting sequence, // then, continue searching for the smallest (large) element from the remaining unordered elements, put it at the end of the sorted sequence. // And so on until all elements are sorted. Public class test_wzs010 {public static void main (string [] ARGs) {int [] intarray = {10, 3, 5, 7, 9, 1, 4, 2, 6, 8}; choosesort (intarray); system. out. println ("\ n after sorting:" + arrays. tostring (intarray);}/*** select sort ** @ Param intarray * array to be sorted */static void choosesort (INT [] intarray) {int length = intarray. length; int temp; // used to store the smallest int minimumindex; For (INT I = 0; I <length; I ++) {temp = intarray [I]; minimumindex = I; for (Int J = I + 1; j <length; j ++) {If (intarray [J] <temp) {temp = intarray [J]; minimumindex = J ;}} intarray [minimumindex] = intarray [I]; intarray [I] = temp; system. out. println ("Number" + (I + 1) + "Times:" + arrays. tostring (intarray ));}}}

Output result:

 1st sorting: [1, 3, 5, 7, 9, 10, 4, 2, 6, 8] 2nd sorting: [1, 2, 5, 7, 9, 10, 4, 3, 6, 8] 3rd sorting: [1, 2, 3, 7, 9, 10, 4, 5, 6, 8] 4th sorting: [1, 2, 3, 4, 9, 10, 7, 5, 6, 8] 5th sorting: [1, 2, 3, 4, 5, 10, 7, 9, 6, 8] 6th sorting: [1, 2, 3, 4, 5, 6, 7, 9, 10, 8] 7th sorting: [1, 2, 3, 4, 5, 6, 7, 9, 10, 8] 8th sorting: [1, 2, 3, 4, 5, 6, 7, 8, 10, 9] 9th sorting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10th sorting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After sorting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

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.