Common sorting algorithms for Java programming

Source: Internet
Author: User

Common sorting Algorithms for Java

First type: Insert sort

Direct Insert Sort

1, direct Insert sort

(1) Basic idea: In the set of numbers to be sorted, assume that the number of front (n-1) [n>=2] is already a row

In a good order, now you want to insert the nth number into the ordinal number in front, so that the n number

It's also in the right order. This cycle is repeated until all the rows are in order.

(2) Illustration

1  Public Static voidMain (string[] args) {2         //Sort Ascending3         int[] a={9,7,8,6,5,4,3,2,1};4         intTemp=0;5          for(inti=1;i<a.length;i++){6             intJ=i-1;7temp=A[i];8              for(; j>=0&&temp<a[j];j--){9a[j+1]=A[j];Ten             } Onea[j+1]=temp; A         } -  -System.out.println ("Test2.main ()" +arrays.tostring (a)); the}
View Code

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.