insertalgorithm.java

来自「各式JAVA排序算法,很有价值,希望能对大学有所帮助.」· Java 代码 · 共 41 行

JAVA
41
字号
/**
 * A insertion sort demonstration algorithm
 * InsertAlgorithm.java, 19.04.97
 *
 * @author   Lars Marius Garshol
 * @version  1.00 - 19.04.97
 */
/**
 * JAVA爱好者:http://www.javafan.net
 * 免费提供Java教程、电子书籍、代码源码、开发工具等
 * 站长后注
 */
class InsertAlgorithm extends SortAlgorithm {
  void sort(int a[]) throws Exception {
    int tmp; //The number currently being sorted is stored here while we make room for it
    int tmp2;//Used for swapping
    int j;
 
    for (int i=1; i<=a.length; i++) {
      // Invariant: a[0..i-1] sorted

      tmp=a[i];

      for (j=i-1; j>=0 && a[j]>tmp; j--) {
        if (stopRequested) {
    	  return;
        }

        a[j+1]=a[j];		        

	pause(j,i);
      }

      //Now we've found a[i]'s place
      a[j+1]=tmp;
    }

  } //end of sort
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?