A sort algorithm of Java insertion sorting algorithm

Source: Internet
Author: User
Tags array sort

Package Net.qh.test.sort;import java.util.arraylist;import Java.util.calendar;import java.util.list;/** * Created by Administrator on 2016/03/01. */public class Insert {public int[] sort (int[] arr) {if (arr = = NULL | | arr.length <= 1) {retur        n Arr;        } int length = Arr.length;            for (int i = 1; i < length;i++) {int k = i-1;            int temp = Arr[i];                while (k >= 0) {if (Arr[k] > Temp) {arr[k+1] = arr[k];                }else{break;                } Arr[k] = temp;            k--;    }} return arr;        } public static void Main (string[] args) {list<integer> intlist = new arraylist<integer> ();        for (int i = 0; i < 100000; i++) {intlist.add (i);        } int[] arr = new int[intlist.size ()];      for (int j = 0; J < Arr.length; J + +) {Arr[j] = Intlist.get (j);  } for (int temp:arr) {System.out.print (temp + ",");        } System.out.println ("");        Long start = Calendar.getinstance (). Gettimeinmillis ();        arr = new Insert (). sort (arr);        Long end = Calendar.getinstance (). Gettimeinmillis ();        System.out.println ("milliseconds required:" + (End-start));        for (int temp:arr) {System.out.print (temp + ","); }}/** 1.         The sorting algorithm is a simple insert sorting algorithm.         2. Sorting ideas: Through the traversal, the smallest to find out, record the subscript, and then exchange with the destination position.                            3. Compared to bubble sort: It's a very simple sort algorithm like bubble sort.                            When a new record is inserted into an ordered array, a sequential array is compared, the large data is moved back, and the new record is assigned the appropriate position.         The bubbling sort is done with a full interchange, and the insertion sort is done in half-exchange, faster than the bubbling sort.         4. Simple selection order of sorts: O (n^2), is a stable sort. 5. Real-Combat testing: 4-core CPU and 8G of memory, 100,000 integer elements of the array sort, reverse order requires 5646 milliseconds, ordered: 5798 milliseconds */}

  

A sort algorithm of Java insertion sorting algorithm

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.