Java data structure and algorithm instance: Insert sort insertion Sort_java

Source: Internet
Author: User
/** * The idea of choosing a sort: * before each loop, the array to the left is a partially ordered sequence, * and then select the right to be sorted elements, to save their values * and then the left side of the elements compared * if less than the left element, the left element to the right one * until and the leftmost 
The comparison is complete, or the element to be arranged is not smaller than the left element * * Package Al; public class Insertionsort {public static void main (string[] args) {Insertionsort insertsort = new Ins 
    Ertionsort (); 
    Int[] elements = {14, 77, 21, 9, 10, 50, 43, 14}; 
    Sort the array insertsort.sort (elements); 
      Print the sorted array for (int i = 0; i < elements.length i++) {System.out.print (elements[i)); 
    System.out.print (""); }/** * @author * @param array to row arrays */public void sort (int[] array) {//min to save Minimum element for each round int key;  Save current element for (int i=0; i<array.length; i++) {int j = i; 
      Current position key = Array[j];  Compare current element while (J > 0 && array[j-1] > key) {array[j] = array[j-1];//shift 
     It   j--; 
     
    } Array[j] = key; 

 } 
  } 
}

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.