/*
稳定性:[待定](不稳定的算法结构:如果有两个相同的元素5,会导致第一个5和第二个5的位置发生改变)
*/
package seven_happy;
public class Code_Demo {
/**
* author: Ain
* model: write a code about DirectionInsert_sort
* date:2016-3-9
*/
// 定义变量值
private static final int [] a = {9,5,7,10,6,8};
private static final String OUTPUT_ago = "直接插入排序之前:";
private static final String OUTPUT_AFTER = "直接插入排序之后:";
// 主程序入口
public static void main(String[] args) {
DirectionInsert_sort(a);
System.out.println(" ");
System.out.print(OUTPUT_AFTER);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
public static void DirectionInsert_sort(int[] ARRAY_NUMBER){
//直接插入排序
System.out.print(OUTPUT_ago);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
//直接插入排序
for (int i = 1; i < a.length; i++) {
//待插入元素
int temp = a[i];
int j;
for (j = i-1; j>=0; j--) {
//将大于temp的往后移动一位
if(a[j]>temp){
a[j+1] = a[j];
}else{
break;
}
}
a[j+1] = temp;
}
}
}
稳定性:[待定](不稳定的算法结构:如果有两个相同的元素5,会导致第一个5和第二个5的位置发生改变)
*/
package seven_happy;
public class Code_Demo {
/**
* author: Ain
* model: write a code about DirectionInsert_sort
* date:2016-3-9
*/
// 定义变量值
private static final int [] a = {9,5,7,10,6,8};
private static final String OUTPUT_ago = "直接插入排序之前:";
private static final String OUTPUT_AFTER = "直接插入排序之后:";
// 主程序入口
public static void main(String[] args) {
DirectionInsert_sort(a);
System.out.println(" ");
System.out.print(OUTPUT_AFTER);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
public static void DirectionInsert_sort(int[] ARRAY_NUMBER){
//直接插入排序
System.out.print(OUTPUT_ago);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
//直接插入排序
for (int i = 1; i < a.length; i++) {
//待插入元素
int temp = a[i];
int j;
for (j = i-1; j>=0; j--) {
//将大于temp的往后移动一位
if(a[j]>temp){
a[j+1] = a[j];
}else{
break;
}
}
a[j+1] = temp;
}
}
}