--java implementation of direct insertion sorting algorithm for sorting series

Source: Internet
Author: User

Direct insertion Sorting algorithm

Basic idea:

The n order of the elements to be sorted as an ordered table and an unordered table, the beginning of the ordered table has only one element, the unordered table has n-1 elements; The sorting process takes the first element out of an unordered table, inserts it into an ordered table, makes it a new ordered table, and repeats the entire sort process n-1 times.

 Instance:

0. Initial state 3,1,5,7,2,4,9,6 (a total of 8)

Ordered tables: 3; unordered table: 1,5,7,2,4,9,6

1. First loop, remove the first number 1 from the unordered table, insert it into the ordered table so that the new sequence is still in order

ordered table:1,3; unordered table: 5,7,2,4,9,6

 2. second cycle, remove the first number 5 from the unordered table, insert it into the ordered table, and make the new sequence still orderly

Ordered table:1,3,5; unordered table: 7,2,4,9,6

  3. third cycle, remove the first number 7 from the unordered table, insert it into the ordered table, and make the new sequence still orderly

Ordered table:1,3,5,7; unordered table: 2,4,9,6

4. fourth cycle, remove the first number 2 from the unordered table, insert it into the ordered table so that the new sequence is still in order

Ordered table:1,2,3,5,7; unordered table: 4,9,6

5. fifth cycle, remove the first number 4 from the unordered table, insert it into the ordered table so that the new sequence is still in order

ordered table:1,2,3,4,5,7; unordered table: 9,6

6. Sixth cycle, remove the first number 9 from the unordered table, insert it into the ordered table so that the new sequence is still in order

ordered table:1,2,3,4,5,7,9; unordered table: 6

7. seventh cycle, remove the first number 6 from the unordered table, insert it into the ordered table so that the new sequence is still in order

Ordered table:1,2,3,4,5,6,7,9; unordered table: (empty)

Java implementations: 

Package sort;
/**
* Implementation of direct Insert sort * stabilization algorithm *@authorThe ginkgo leaves of the season*/ Public classInsertsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub intA[] = {3,1,5,7,2,4,9,6}; NewInsertsort (). Insertsort (a); } /*** Implementation of direct insertion sorting algorithm *@parama*/ Private voidInsertsort (int[] a) {//TODO auto-generated Method StubSystem.out.println ("——————————————————— Direct insertion sorting algorithm —————————————————————"); intn =a.length; inti,j; for(i=1;i<n;i++){ /*** Temp is the number in the sequence table to be inserted for this cycle*/ inttemp =A[i]; /*** Find the correct location for temp to insert a sequence table*/ for(J=i-1;j>=0 && a[j]>temp;j--){ /*** element is moved back, ready to insert temp*/a[j+1] =A[j]; } /*** Insert Temp*/a[j+1] =temp; Print (a,n,i); } printresult (A,n); } /*** Print the final result of sorting *@paramA *@paramN*/ Private voidPrintresult (int[] A,intN) {System.out.print ("Final sort Result:"); for(intj=0;j<n;j++) {System.out.print (" "+A[j]); } System.out.println (); } /*** Print the result of each cycle of sorting *@paramA *@paramN *@paramI*/ Private voidPrintint[] A,intNinti) {//TODO auto-generated Method StubSystem.out.print ("+i+" Times: "); for(intj=0;j<n;j++) {System.out.print (" "+A[j]); } System.out.println (); }}

 Running results show:

 

 (This article is only for learning exchange, if there are better ideas, welcome to leave comments for everyone to explore learning ~) 

--java implementation of direct insertion sorting algorithm for sorting series

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.