Internal Sorting: Shellsort: Sorting by Insertion
来源:互联网 发布:地产基金 知乎 编辑:程序博客网 时间:2024/05/13 18:55
Shellsort:希尔排序
Animation
Shellsort with gaps 23, 10, 4, 1 in action.
The step-by-step process of replacing pairs of items during the shell sorting algorithm.
Complexity
Algorithm D
Algorithm D (Shellsort).Records
sorting is complete, their keys will be in order,
sequence of increments
where
sorting time. This algorithm reduces to Algorithm S when
D1. [Loop on
algorithm.
D2. [Loop on
(We will use a straight insertion method to sort elements that are
apart, so that
essentially the same as steps S2 through S5, respectively, in Algorithm S.)
D3. [Set up
D4. [Compare
D5. [Move
step D4.
D6. [
Data table
Java program
In this program, R1,…,RN weresimplified to K1,…,KN.
/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 11/23/13 * Time: 10:01 PM * :)~ * Shellsort: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!*/ for(int h=N/2; h>=1; h/=2){ for(int j=h+1; j<=N; j+=h){ int Key = K[j]; int i = j-h; while (i > 0){ if(Key >= K[i]){ K[i+h] = Key; break; }else { K[i+h] = K[i]; i-=h; } } if(i <= 0){ K[i+h] = Key; } } } /*Output sorted Ks*/ System.out.println("Sorted Ks:"); for(int i=1; i<=N; i++){ System.out.println(i+":"+K[i]); } }}
Outputs
UnsortedKs:1:5032:873:5124:615:9086:1707:8978:2759:65310:42611:15412:50913:61214:67715:76516:703SortedKs: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
https://en.wikipedia.org/wiki/Shellsort
- Internal Sorting: Shellsort: 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: Multiple list insertion: Sorting by Insertion
- Internal Sorting: Two-way insertion: Sorting by 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
- Internal Sorting: Comparison counting: Sorting by counting
- Insertion Sorting
- Internal Sorting: Radix List Sort: Sorting by Distribution
- Internal Sorting: List Merge Sort: Sorting by Merging
- Internal Sorting: Radix exchange sort: Sorting by Exchanging
- 编程 - 专业名词
- C语言关于线性表的相关操作
- ios 与 js 交互 stringByEvaluatingJavaScriptFromString的使用方法
- 多屏幕多分辨率的支持和一些概念
- hdoj 1829 A Bug's Life 【带权并查集】
- Internal Sorting: Shellsort: Sorting by Insertion
- 从大小为n的数组中随机选出m个整数,要求被选中的概率相同
- ASP.NET之基本对象
- 超简洁Cygwin安装配置
- JavaScript学习8:DOM进阶
- 第十一周 【项目2 - 职员有薪水了】深复制
- gSoap代码生成
- 开始写博客
- this is for test