Internal Sorting: Binary insertion: Sorting by Insertion
来源:互联网 发布:网上开票软件 编辑:程序博客网 时间:2024/05/24 03:18
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[n] = 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:5032:873:5124:615:9086:1707:8978:2759:65310:42611:15412:50913:61214:67715:76516:703Sorted Ks:1:612:873:1544:1705:2756:4267:5038:5099:51210:61211:65312:67713:70314:76515:89716:908
Reference
<< Theart of computer programming: Sorting andSearching >> VOLUME 3, DONALD E.KNUTH
0 0
- Internal Sorting: Binary insertion: Sorting by Insertion
- Internal Sorting: List insertion: Sorting by Insertion
- Internal Sorting: Straight insertion: Sorting by Insertion
- Internal Sorting: Shellsort: Sorting by Insertion
- Internal Sorting: Multiple list insertion: Sorting by Insertion
- Internal Sorting: Two-way insertion: Sorting by Insertion
- Insertion Sorting
- Sorting Algorithms ------[Insertion Sort]
- 【Leetcode】Insertion Sort List (Sorting)
- Sorting Algorithm-Straight Insertion Sort
- introduction to algorithms sorting lesson 1 insertion
- Internal Sorting: Heapsort-2:Sorting by Selection
- Internal Sorting: Quicksort-2: Sorting by Exchanging
- Internal Sorting: Heapsort-1: Sorting by Selection
- Internal Sorting: Radix sort: Sorting by Exchanging
- Internal Sorting: Quicksort-1:Sorting by Exchanging
- Internal Sorting: Bubble sort: Sorting by Exchanging
- Internal Sorting: Distribution counting: Sorting by counting
- gsoap代码生成和代码使用
- python的多进程编程(1)
- 黑马程序员——java学习日记2——Thread
- Hibernate各种主键生成策略与配置详解
- 基于echarts实现图表展示
- Internal Sorting: Binary insertion: Sorting by Insertion
- 第13周项目2—形状类族中的纯虚函数
- scala初探
- 柳暗花明又一春——致五月英语
- (3)mysql优化之sql语句优化
- 第十三周【项目3-立体类族共有的抽象类】
- AddressBook
- linux drm 架构
- Spring2.5学习2.2_编码剖析Spring依赖注入原理