Internal Sorting: Two-way insertion: Sorting by Insertion
来源:互联网 发布:网上开票软件 编辑:程序博客网 时间:2024/05/17 09:35
Data table
Java program
/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 11/22/13 * Time: 10:01 PM * :)~ * Two-way insertion:Sorting by Insertion:Internal Sorting */public class Main { public static void main(String[] args) { int N = 16; int[] K = new int[17]; int[] outputs = new int[2*N+1]; /*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; int center = (2*N+1)/2; int i = center; int k = center; int left = center; int right = center; outputs[center] = K[1]; for(int j=2; j<=N; j++){ int Key = K[j]; while(i <= k){ int mid = (i+k)/2; if(outputs[mid] < Key){ i = ++mid; dest = i; }else if(outputs[mid] == Key){ dest = ++mid; break; }else { dest = mid; k = --mid; } } if(dest >= (left+right)/2){ right++; /*right move*/ for(int n=right; n>dest; n--){ outputs[n] = outputs[n-1]; } outputs[dest] = Key; }else { left--; /*left move*/ for(int l=left; l<dest-1; l++){ outputs[l] = outputs[l+1]; } outputs[dest-1] = Key; } i = left; k = right; } /*Output sorted Ks*/ System.out.println("Sorted Ks:"); int dist = i-1; while (i<=k){ System.out.println((i-dist)+":"+outputs[i]); i++; } }}
Outputs
UnsortedKs: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
<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH
0 0
- Internal Sorting: Two-way insertion: Sorting by Insertion
- Internal Sorting: List insertion: Sorting by Insertion
- Internal Sorting: Binary 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: Straight Two-way Merge Sort: Sorting by Merging
- Internal Sorting: Natural Two-way Merge Sort: Sorting by Merging
- 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
- this is for test
- 文章标题
- Zend Optimizer的时候 装不上会出现这段提示 Your web server configurrtion seems to be corrupted and does not inclu
- Windows server 2008 R2实现多用户远程连接
- hdu 1163 Eddy's digital Roots
- Internal Sorting: Two-way insertion: Sorting by Insertion
- char a[]与char *的一些补充实验
- Cocos2d-js开发之JavaScript初探
- iOS的init、loadView、 viewDidLoad、viewDidUnload的关系
- gsoap代码生成和代码使用
- python的多进程编程(1)
- 黑马程序员——java学习日记2——Thread
- Hibernate各种主键生成策略与配置详解
- 基于echarts实现图表展示