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