Internal Sorting: Heapsort-1: Sorting by Selection
来源:互联网 发布:银光软件 编辑:程序博客网 时间:2024/05/24 00:48
Heapsort-1:堆排序-1
Animation
A run of the heapsort algorithm sorting an array of randomly permuted values. In the first stage of the algorithm the array elements are reordered to satisfy the heap property. Before the actual sorting takes place, the heap tree structure is shown briefly for illustration.
An example on heapsort.
Complexity
Pseudo code
HEAPSORT(A) BUILD-MAX-HEAP(A) for i = A.length downto 2 exchange A[1] with A[i] A.heap-size = A.heap-size - 1 MAX-HEAPIFY(A, 1)BUILD-MAX-HEAP(A) A.heap-size = A.length for i = floor(A.length/2) downto 1 MAX-HEAPIFY(A, i)MAX-HEAPIFY(A, i) l = LEFT(i) r = RIGHT(i) if l <= A.heap-size and A[l] > A[i] largest = l else largest = i if r <= A.heap-size and A[r] > A[largest] largest = r if largest != i exchange A[i] with A[largest] MAX-HEAPIFY(A, largest)LEFT(i) return 2iRIGHT(i) return 2i+1
Java program
/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 12/2/13 * Time: 10:01 PM * :)~ * Heapsort-1:Sorting by Selection:Internal Sorting */public class Main { public static int LEFT(int i){ return 2*i; } public static int RIGHT(int i){ return 2*i+1; } public static void MAX_HEAPIFY(int[] K, int i, int heapSize){ int l = LEFT(i); int r = RIGHT(i); int largest; int temp; if(l <= heapSize && K[l] > K[i]){ largest = l; }else { largest = i; } if(r <= heapSize && K[r] > K[largest]){ largest = r; } if(largest != i){ temp = K[largest]; K[largest] = K[i]; K[i] = temp; MAX_HEAPIFY(K, largest, heapSize); } } public static void BUILD_MAX_HEAP(int[] K, int heapSize){ for(int i=(int)Math.floor((K.length-1)/2); i>=1; i--){ MAX_HEAPIFY(K, i, heapSize); } } public static void HEAPSORT(int[] K){ int heapSize = K.length-1; int temp; BUILD_MAX_HEAP(K, heapSize); for(int i=K.length-1; i>=2; i--){ temp = K[i]; K[i] = K[1]; K[1] = temp; heapSize--; MAX_HEAPIFY(K, 1, heapSize); } } 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!*/ HEAPSORT(K); /*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
<< Introduction to Algorithms >> Third Edition, THOMAS H. CORMEN, CHARLES E. LEISERSON, RONALD L. RIVEST, CLIFFORD STEIN
https://en.wikipedia.org/wiki/Heapsort
0 0
- Internal Sorting: Heapsort-1: Sorting by Selection
- Internal Sorting: Heapsort-2:Sorting by Selection
- Internal Sorting: Straight selection sort: Sorting by Selection
- Internal Sorting: Quicksort-1:Sorting by Exchanging
- Internal Sorting: Quicksort-2: Sorting by Exchanging
- Internal Sorting: Radix sort: Sorting by Exchanging
- Internal Sorting: Bubble sort: Sorting by Exchanging
- Internal Sorting: List insertion: Sorting by Insertion
- Internal Sorting: Shellsort: Sorting by Insertion
- Internal Sorting: Binary insertion: Sorting by Insertion
- Internal Sorting: Straight insertion: Sorting by Insertion
- Internal Sorting: Distribution counting: Sorting by counting
- Internal Sorting: Comparison counting: Sorting by counting
- Internal Sorting: Radix List Sort: Sorting by Distribution
- Internal Sorting: List Merge Sort: Sorting by Merging
- Internal Sorting: Radix exchange sort: Sorting by Exchanging
- Internal Sorting: Straight Two-way Merge Sort: Sorting by Merging
- Internal Sorting: Natural Two-way Merge Sort: Sorting by Merging
- Python之学习笔记(GUI设计)
- OC基础学习——基本数据类型和复杂数据类型
- 线程基础-单例模式
- Android Manifest内容解析
- LeetCode之3SumClosest
- Internal Sorting: Heapsort-1: Sorting by Selection
- 第十三周 【项目1-动物这样叫】
- 树
- Nuch 1.2 +tomcat window下 搭建简单的搜索引擎
- 5 设计模式之状态模式(五)
- div居中
- RT5350硬件调试总结
- 博客第一天......
- solr DataImportHandler (数据相关操作)