Review of data structure--"insert sort"-java implementation

Source: Internet
Author: User

Rookie in a rookie, face looking for work, review the data structure, write the insertion sorting algorithm record, write a little every day, accumulating it! 650) this.width=650; "src=" Http://img.baidu.com/hi/tsj/t_0040.gif "alt=" T_0040.gif "/>


Import Java.util.Scanner;

/**

*

* @author DL

* Data structure Review of the insertion sequencing practice Program

*

*/

public class Sorttest {

public static void Main (String [] args) {

Scanner sc = new Scanner (system.in);

int totalnums = Sc.nextint ();//Read into the number of data to be sorted

Allocate space for read-in data, allocating one more, where the first position is inputdata[0] as Sentinel

int [] Inputdata = new int[totalnums+1];

for (int i=1;i<=totalnums;i++) {

inputdata[i]=sc.nextint ();

}

Show (Inputdata);

Insertsort (Inputdata);

Show (Inputdata);

}


/**

* Insert Sort Program

* @param array to sort

*/

static void Insertsort (int [] array) {

for (int i = 2; i < Array.Length; i++) {

array[0]=array[i];//copy the elements to be compared to the array[0] position as Sentinel

Int J;

Compare from the first left of the selected element until you find an element larger than it, jumping out of the loop

for (j=i-1;j>0;j--) {

if (Array[0]<array[j]) {

array[j+1]=array[j];//the position of the element smaller than the Sentinel moves backwards

}else{

Break

}

}


Code Lite version

/*for (j=i-1;array[0]<array[j];j--) {

ARRAY[J+1]=ARRAY[J];

}*/

array[j+1]=array[0];//inserts the current element to be compared to the location found

}

}


/**

* Output array elements

* @param array to display

*/

static void show (int [] array) {

for (int i = 1; I <array.length; i++) {

System.out.print (array[i]+ "");

}

System.out.println ("\ n");

}

}


This article is from the "Holding" blog, please be sure to keep this source http://dengliu.blog.51cto.com/13212652/1962041

Review of data structure--"insert sort"-java implementation

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.