Java Sorting algorithm--insert Sort

Source: Internet
Author: User

Import java.util.arrays;//=================================================//File name:select_sort//------------ ------------------------------------------------------------------//author:common//class Name: arrays_select//property://Method: C Lass arrays_insert{private int[] arrays;private int curnum;public arrays_insert (int max) {//Create an empty array of Max lengths super (); arrays = new Int[max];curnum = 0;} public void Insert (int value) {//empty array add element arrays[curnum] = value;curnum++;} public void display () {//display array System.out.println (arrays.tostring (Arrays));} private void Swap (int one,int) {//Interchange int temp = Arrays[one];arrays[one] = Arrays[two];arrays[two] = temp;}                        public void Insertsort () {int out,in;for (out=1;out<curnum;out++) {//Starts from 2nd, and 1th compares int temp = Arrays[out];in = out; In equals out, compare from in-1 start while (in>0 && arrays[in-1] >= temp) {//if greater than temp, move right arrays[in] = arrays[        IN-1]; Example: 2 3 1 temp=1 2 3 3 temp=1-2 2 3 temp=1--1 2 3 temp=1--in;} Arrays[in] = temp;}}}//Main class//function:select_sortpublic class Insert_sort {public static void main (string[] args) {//TODO auto-generated method stub int maxSize = 100; Arrays_insert Arrays_demo = new Arrays_insert (maxSize); Arrays_demo.insert (; arrays_demo.insert); Arrays_ Demo.insert (Arrays_demo.insert); arrays_demo.insert; Arrays_demo.display (); Arrays_demo. Insertsort (); Arrays_demo.display ();}}

bubbling N^2/2 comparison N^2/4 Exchange

Select N^2/2 more than bubble less exchange

Insert N^2/4 Comparing N^2/4 Replication

Replication is 3 times times the Exchange

Java Sorting algorithm--insert Sort

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.