Algorithmic Learning (Java language Description)--Insert Sort __ Encoding

Source: Internet
Author: User

The basic operation of the direct insertion sort (Straight insertion sort) is to insert a record into an ordered table, resulting in a new ordered table with a record plus 1.


The implementation code is given below:


	public void Insertsort (int a[]) {
		int i, J;
		for (i = 2; i < a.length i++) {
			if (A[i] < a[i-1]) { 			//If the latter value is less than the previous value, insert the small value a[i] into the ordered child table
				a[0] = a[i];
 
  //Set Sentry for
				(j = i-1; A[j] > A[0]; j--) {
					a[j + 1] = A[j]; 	Record post
				}
				a[j + 1] = a[0];		Insert to correct location
			}

		}
 


Test code:

public static void Main (String args[]) {
		int[] arr= new int[]{0, 5, 3, 4, 6, 2};<span style= "White-space:pre" >< C16/></span>//0 adds
		new Insertsort () Insertsort (arr) 
		
		to set up sentries. for (int i=1;i<arr.length;i++) {
			System.out.print (arr[i]+ "");
		}
		System.out.println ();		
	}

Test results:




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.