Java Sorting algorithm

Source: Internet
Author: User

1. Insert Sort

The insertion sort is one of the simplest sorting algorithms, consisting of N-1, which must be guaranteed that the elements at position 0 to P are sorted.

The specific implementation code is as follows:

 PackageK;ImportJava.util.Scanner; Public classTest2 { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); //get the input string and remove all the spaces in the middleString [] str = In.nextline (). Split ("\\s+"); int[] inter=New int[Str.length]; //Convert a string to a number         for(inti=0;i<inter.length;i++) {Inter[i]=integer.valueof (Str[i]); }                //Sortsort (inter); //results after sorting the output         for(inti=0;i<inter.length;i++) {System.out.printf ("%d", Inter[i]); }    }    /*** Sort *@paramInter*/    Private Static voidSortint[] Inter) {        intp;  for(p=1;p<inter.length;p++)        {            intJ; inttemp=Inter[p];  for(J=p-1;j>=0 && temp<inter[j];j--) {inter[j+1]=Inter[j]; } inter[j+1]=temp; }            }}

The insertion sort is implemented in sort ().

The principle can be illustrated with one of the following pictures:

The insertion sort algorithm is implemented above. The time complexity is O (N).

Java sorting algorithm

Related Article

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.