Java Sorting Algorithm _ 003 insert sorting

Source: Internet
Author: User
Package WZS. sort; import Java. util. arrays; // Insert the sorting directly. // basic idea: In the number of groups to be sorted, assume that the preceding (n-1) [n> = 2] The number is already in the correct order. Now we need to insert the nth number into the preceding order number so that the N number // is in the correct order. This repeats until all the rows are sorted. Public class test_wzs003 {public static void main (string [] ARGs) {int intarray [] = {10, 3, 5, 7, 9, 1, 4, 2, 6, 8}; insertsort (intarray);}/*** insert sort * @ Param intarray */Private Static void insertsort (INT [] intarray) {int temp = 0; for (INT I = 1; I <intarray. length; I ++) {temp = intarray [I]; Int J = I-1; for (; j> = 0 & temp <intarray [J]; j --) {intarray [J + 1] = intarray [J];} intarray [J + 1] = temp; system. out. println ("+ I +" Times: "+ arrays. tostring (intarray);} system. out. print ("\ n:" + arrays. tostring (intarray ));}}

Output result:

1st sorting: [3, 10, 5, 7, 9, 1, 4, 2, 6, 8] 2nd sorting: [3, 5, 10, 7, 9, 1, 4, 2, 6, 8] 3rd sorting: [3, 5, 7, 10, 9, 1, 4, 2, 6, 8] 4th sorting: [3, 5, 7, 9, 10, 1, 4, 2, 6, 8] 5th sorting: [1, 3, 5, 7, 9, 10, 4, 2, 6, 8] 6th sorting: [1, 3, 4, 5, 7, 9, 10, 2, 6, 8] 7th sorting: [1, 2, 3, 4, 5, 7, 9, 10, 6, 8] 8th sorting: [1, 2, 3, 4, 5, 6, 7, 9, 10, 8] 9th 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.