加入插入排序

This commit is contained in:
junv
2012-09-11 16:13:28 +08:00
commit 1206906cf4
185 changed files with 6290 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="output" path="bin"/>
</classpath>
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>InsertionSort</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
@@ -0,0 +1,29 @@
package com.toozhao.sort;
public class InsertionSort {
// 定义需要排序的数组
private static int[] array = { 1, 20, 6, 3, 19, 7, 14, 12, 10 };
public static void main(String args[]) {
for (int outer = 1; outer < array.length; outer++) {
int temp = array[outer];
for (int inner = outer - 1; inner >= 0 && temp < array[inner]; inner--) {
/**
* 将最大的赋值给目前比较的数组尾部 由于这里的数组下标需要跟随循环而变化,所以只能使用 j来表示
*/
array[inner + 1] = array[inner];
// 重新赋值第二大的数
array[inner] = temp;
// 重新赋值参考值,接着下标-1
temp = array[inner];
}
}
// 便利排序后的数组
for (int flag : array) {
System.out.println(flag);
}
}
}