The basic idea of heap sorting: to sort by establishing the maximum minimum heap according to a complete binary treeComplexity of Time: O (NLONGN)/** * */Package com;/** * @author Wenb * @time pm 03:52:55 * @date 2014-10-24*/ Public classHeapsort { Public Static voidMain (String args[]) {int[] A = {1,3,2, +,5, A,98, Wu}; Headsort (a); for(intI=0; i) {S
1. Heap Sorting
The definition of heap sort and thought can refer to Baidu Encyclopedia: Heap Sort
In a nutshell, heap sequencing is an improved sort of selection, the improvement is th
Heap ordering, like the merge sort, has a time complexity of O (NLGN), as with the insertion sort, with the spatial site: at any time, only a constant number of additional element space is required to store temporary data. The (binary) heap is an array that can be seen as an
Heap Sort
Inserts all the elements into a priority queue that inserts a lookup for the smallest element, repeating the call to delete the smallest element, so that all elements are sorted in ascending order.
Two stages of heap sequencing
Heap Order
Data order
All elements are arranged in ascendi
]) -Maxindex =index; - } - if(Left! =Minindex) + { - swap (_array[left], _array[minindex]); + if(left = = Maxindex)//avoid an error if the maximum value is _array[left] when the previous action is moved to _array[minindex] AMaxindex = Minindex;//Update the Maxindex at } - if(Right! =Maxindex) - swap (_array[right], _array[maxindex]); - } - return; -}Efficiency analysis: Algorithmic Stability: UnstableComplexity of Time: O (n^2) Space
Eight sort algorithms that must be known [java Implementation] (3) Merge sort algorithms and Heap Sort Algorithms
I. Merge Sorting Algorithm
Basic Idea:
The Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each
This article will introduce three sorting algorithms--insert sort, hill sort, heap sort. All examples of this article use ascendingI. Insert SortAlgorithmic thinkingMaintains an ordered array, comparing the data to be inserted with the number one by one of the ordered array from the last element until the appropriate p
Quick sort each time the comparison takes O (n), to make a lgn comparison, the entire sort is finally completed. So the complexity of the fast line is only O (N*LGN). In this section, we are going to talk about heap sorting algorithms. As far as I know, to really thoroughly understand an algorithm, it is best to find t
previous n-1 times have been done. The tree-shape selection sort takes advantage of the tree-shaped features to preserve some of the earlier comparative results. Therefore, the comparison can be reduced. For n sequences of keyword. In the worst case, each node needs to be LOG2 (n) at a time. Therefore, the worst-case time complexity is NLOG2 (n). Heap sorting is
can complete all priority-queue operations within the time complexity of LGN.Application of heap: heap sortThe whole process is divided into
Build heap
Extract Maximum Value
Put it at the end of the sequence
Repeat guided sorting completeThe specific algorithm is as follows:
Realize#heapclass
Hill sort (by Donald Shell):Suppose you give an array that needs to be sorted and sort by 5-interval. The drawings are as followsWe slowly began to sort by the smaller intervals (and finally the interval was only 1).Heap Sort:First look at the insertion sortvoidSelection_sort (ElementType a[],intN) { for(i =0; i )
are concentrated on the left, and the leaves on the bottom two are centered on the Right.Maintain maximum heap function Max_heapweihu (a,i), Assuming that the left and right subtree of node I is already the largest heap. then, when maintaining the heap, compare the value of the I node to the size of the left and right node values, and swap the maximum values in
Some of the content goes from:Dreamcatcher-cxSource: This article is copyrighted by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the page obvious location to give the original link.Pre-knowledgeHeap SortHeap sorting is a sort algorithm designed by using the data structure of heap , heap
Heap Sort (heap sort) specific explanation and code (c + +)This address: Http://blog.csdn.net/caroline_wendyheap sequencing consists of two steps :The first step is to build a large top heap (from large to small sort) or a small t
Objective
The average time complexity for heap sort, quick sort, and merge sort (which will write the two sorting algorithms in the next article) is O (N*logn). to figure out how to sort the
if(temp >= h->elements[child]) Break;ElseH->elements[parent] = h->elements[child];//Move the temp element to the next layer} H->elements[parent] = temp;returnMaxItem;}Select SortThe basic idea of choosing a sort (Selection sort) is that each trip selects the record with the lowest keyword in the n-i+1 (i=1,2,..., n-1) record as the first record in the ordered sequence.Simple selection
step 2
2. The child node labeled largest is Array[i], and the subtree with its root may violate the nature of the heap, so perform step 1 on the subtree.
In the following function, I use Heap_type to indicate whether the heap is the largest heap or the smallest heap, and perform different operations accordingly.
Temp
Sort
Time limit:1000ms Memory limit:32678k
Title DescriptionGive you the number of N (nInputInput data The first line is a positive integer n, and the second row has n integers.OutputOutput one line, from small to large output this n number, the middle with a space separated.Sample input51 4 3) 2 5Sample output1 2 3) 4 5#include Sort: Binary insertion s
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.