Internal Sorting: Binary insertion: Sorting by Insertion
2015-05-31 16:53
405 查看
Java program
/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 11/21/13 * Time: 10:01 PM * :)~ * Binary insertion:Sorting by Insertion:Internal Sorting */ public class Main { public static void main(String[] args) { int N = 16; int[] K = new int[17]; /*Prepare the data*/ K[1] = 503; K[2] = 87; K[3] = 512; K[4] = 61; K[5] = 908; K[6] = 170; K[7] = 897; K[8] = 275; K[9] = 653; K[10] = 426; K[11] = 154; K[12] = 509; K[13] = 612; K[14] = 677; K[15] = 765; K[16] = 703; /*Output unsorted Ks*/ System.out.println("Unsorted Ks:"); for(int i=1; i<=N; i++){ System.out.println(i+":"+K[i]); } System.out.println(); /*Kernel of the Algorithm!*/ int dest = 0; for(int j=2; j<=N; j++){ int Key = K[j]; int i = 1; int k = j-1; while (i <= k){ int mid = (i+k)/2; if(K[mid] < Key){ i = ++mid; dest = i; }else if(K[mid] == Key){ dest = ++mid; break; }else{ dest = mid; k = --mid; } } for(int n=j; n>dest; n--){ K = K[n-1]; } K[dest] = Key; } /*Output sorted Ks*/ System.out.println("Sorted Ks:"); for(int i=1; i<=N; i++){ System.out.println(i+":"+K[i]); } } }
Outputs
Unsorted Ks: 1:503 2:87 3:512 4:61 5:908 6:170 7:897 8:275 9:653 10:426 11:154 12:509 13:612 14:677 15:765 16:703 Sorted Ks: 1:61 2:87 3:154 4:170 5:275 6:426 7:503 8:509 9:512 10:612 11:653 12:677 13:703 14:765 15:897 16:908
Reference
<< Theart of computer programming: Sorting andSearching >> VOLUME 3, DONALD E.KNUTH相关文章推荐
- Sorting Array Values in PHP(数组排序)
- Fckeditor XML Request error:internal server error (500) 解决方法小结
- MYSQL的binary解决mysql数据大小写敏感问题的方法
- HTTP 错误 500.19- Internal Server Error 错误解决方法
- nginx提示:500 Internal Server Error错误的解决方法
- citrix Internal Error报错
- C#中的internal关键字
- 排序binary tree
- eclipse C/C++ 问题(launch failed,binary not found)
- launch failed.Binary not found in Linux/Ubuntu解决方案
- g++遇害,谁是凶手?
- GridView应用三---GridView正反双向排序:
- jquery UI入门手册
- leach协议在NS 2.34上的安装(半转半改)
- leach协议在NS 2.34上的安装(半转半改)
- POJ 1007 解题报告 DNA Sorting
- poj 1094 Sorting It All Out
- using datagrid
- C语言常用排序全解
- sicily 题目之 1154 1200 1324