插入排序
来源:互联网 发布:python 文本挖掘技术 编辑:程序博客网 时间:2024/05/29 17:53
package it.cast.sort;
public class Charu {
public static void charu(int[] n) {
int j;
for (int i = 1; i < n.length; i++) {
int temp = n[i];
for (j = i - 1; j >= 0 && n[j] > temp; j--) {
n[j+1] = n[j];
}
n[j + 1] = temp;
}
}
public static void test(int a[]){
for (int i = 1; i < a.length; i++) {
int temp=a[i];
int j=i-1;
for (; j >=0&&a[j]>temp; j--) {
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
public static void main(String[] arge) {
int a[] = { 1, 9, 3, 7, 4, 6, 8, 5 };
System.out.println("请输出结果");
for (int j = 0; j < a.length; j++) {
System.out.println(a[j]);
}
double begin = System.currentTimeMillis(); // 这段代码放在程序执行前
for (int i = 0; i < 100000000; i++) {
test(a);
}
double end = System.currentTimeMillis() - begin; // 这段代码放在程序执行后
System.out.println("耗时:" + end + "毫秒");
}
}
public class Charu {
public static void charu(int[] n) {
int j;
for (int i = 1; i < n.length; i++) {
int temp = n[i];
for (j = i - 1; j >= 0 && n[j] > temp; j--) {
n[j+1] = n[j];
}
n[j + 1] = temp;
}
}
public static void test(int a[]){
for (int i = 1; i < a.length; i++) {
int temp=a[i];
int j=i-1;
for (; j >=0&&a[j]>temp; j--) {
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
public static void main(String[] arge) {
int a[] = { 1, 9, 3, 7, 4, 6, 8, 5 };
System.out.println("请输出结果");
for (int j = 0; j < a.length; j++) {
System.out.println(a[j]);
}
double begin = System.currentTimeMillis(); // 这段代码放在程序执行前
for (int i = 0; i < 100000000; i++) {
test(a);
}
double end = System.currentTimeMillis() - begin; // 这段代码放在程序执行后
System.out.println("耗时:" + end + "毫秒");
}
}
0 0