Java Implementation Insert Sort instance _java

Source: Internet
Author: User

This article illustrates the method of Java implementation insertion sequencing. Share to everyone for your reference. The implementation methods are as follows:

Import Java.util.Arrays; 
/** 
 * Algorithm name: Insert Sort 
 * Best efficiency O (n); the worst Efficiency O (n²) is the same as bubbling and selecting, suitable for sorting small lists 
 * If the list is basically ordered, the insertion order is more efficient than bubbling and choosing. 
 * @author l.eric 
 * * * 
/public class Insertionsorting {public 
  static void Main (string[] args) { 
    //Definition of an integer array 
    int[] nums = new int[]{4,3,-1,9,2,1,8,0,6}; 
    Prints an array System.out.println without sorting 
    ("No Results before sorting:" + arrays.tostring (nums)); 
    for (int index=0; index<nums.length; index++) { 
      //Get the numeric 
      int key = Nums[index] that needs to be inserted; 
      Gets the subscript 
      int position = index; 
      Iterate over the sorted data before finding the right place to insert while 
      (position >0 && nums[position-1] > key) { 
        Nums[position] = nums[ POSITION-1]; 
        position--; 
      } 
      Nums[position] = key; 
    } 
    Print sorted results 
    System.out.println ("Sorted results:" + arrays.tostring (nums)); 
  } 

I hope this article will help you with your Java programming.

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.