public class InsertSort{
public
static void main(String[] args){
int [] sortArray = new
int []{ 5 , 7 , 4 , 2 , 9 , 8 , 3 , 6 };
System.out.println( "before sorting ,the numbers are:" );
show(sortArray);
insertSort(sortArray);
System.out.println( "after sorting,the numbers are:" );
show(sortArray);
}
public
static void insertSort( int [] intArray){
for ( int
i= 1 ;i<intArray.length;i++){
int
a = intArray[i];
for ( int
j = i- 1 ;j>= 0 ;j--){
if (intArray[j]>a){
int
temp = intArray[j];
intArray[j] =intArray[j+ 1 ];
intArray[j+ 1 ] = temp;
} else {
break ;
}
}
}
}
public
static void show( int [] intArray){
for ( int
i= 0 ;i<intArray.length;i++){
System.out.print(intArray[i]+ "\t" );
}
System.out.println();
}
}
|