//Take the larger child nodes in the left and right child nodes of I - if(J -j + +; the - if(A[i] -Swap (A[i], a[j]); - Else + Break; -} +} A at //From right to do, from bottom to top - voidHeapsort (intA[],intN -{ - ///First step: Establish Dagen (I is the first non-leaf node, n is the number of nodes) - for(inti = N/2; I >= 1; i--) -{ inHeapadjust (A, I, N); -} to + for(inti = n; I >= 1; i--) -{ the //Second step: Swap
First use 2 as the benchmark, using the I-j two pointers are scanned from both sides, separating the elements smaller than 2 and the elements larger than 2. First, compare 2 and 5, 5:2, J shift left.2 2 4 9 3 6 7 1 5 compare 2 and 1. 1 is less than 2, so put 1 in the 2 position2 1 4 9 3 6 7 1 5 compare 2 and more than 2, so move 4 to the back (original 1 position)2 1 4 9 3 6 7 4 5 Compare 2 and 7,2 and 6,2 and 3. 2 and 9, all greater than 2. satisfies the condition and therefore does not change
/** (max) heap down adjustment algorithm * * Note: array implementation of the heap, the nth node of the left child's index value is (2n+1), the right child's index is (2n+2). * where n is an array subscript index value, and the 1th number in the array corresponds to n 0. * * Parameter Description: * A--the array to be sorted * Start--The starting position of the node being lowered (typically 0, indicating
Four, (1) heap sorting
The first time to listen to heap sorting is in 107lab listen to brother, but did not say how to achieve. At that time just looked at the data structure of the two-tree, but also thought to be through the pointer to establish a two-tree way to achieve, feel very difficult.
In fact, the heap sort
Topic Information1098. Insertion or Heap Sort (25)Time limit (MS)Memory Limit 65536 KBCode length limit 16000 BAccording to Wikipedia:Insertion sort iterates, consuming one INPUT element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the
One, what is a heapA heap is a complete binary tree with the following properties. Each node is greater than or equal to the value of its left and right child nodes, called the Big Top heap.Or the value of each node is less than or equal to the value of the left and right child nodes, called the small top heap.Second, if you sort by heapAs defined by the heap, th
Title Addresshttps://pta.patest.cn/pta/test/16/exam/4/question/6765-14 insertion or Heap Sort (25 points)According to Wikipedia:Insertion Sort Iterates, consuming one INPUT element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the locati
Hill SortPackage Shellsort;import utils.sortutils; Public classDemo { Public Static void Shellsort(int[] a) {intTemp,j;intincrement = A.length; Do{increment = increment/3+1;//If it is a direct insert sort, the increment here are 1. //system.out.println (increment); for(inti = increment; i if(A[i]//Staged in temp for(J=i-increment; j>=0 a[j]>temp; j-=increment) {A[j+increment] = a[j];//record and move back to
The previous article used the Quicksort method to sort, but if the rapid sorting method for high repetition rate of slice, time complexity will soar, the speed is quite slow, so try to heap sorting, experimental results, feel very good. Here is the code, you can refer to, This is the building of the big top pile. Two fork tree features: Last Non-leaf node: root = LENGTH/2 (root down rounding when length is
In the external links below, fromHill sort
The essence of the hill sort is the grouping insert sort, which is also known as narrowing the incremental sort, because of the DL. The shell was named after it was introduced in 1959.
In fact, the so-called increment is the first number after the beginning of a
elements labeled 1~n in a set of arrays sorted by a heap sorting algorithm $ */Panax Notoginseng voidHeap_sort (Type array[],intN) - { the inti; + time_t start,end; AStart =clock (); the for(i=n/2; i>=1; i--) + { -Sift (array,i,n);//Build the initial heap $ } $ for(i=n;i>=2; i--)//controlling the sorting process - { - type temp; thetemp = array[1]; -array[1] =Array[i];WuyiArray[i]
#include The heap can be seen as a complete binary tree, and the small heap sort takes advantage of the smallest of keywords in the top record of a small root heap, making it easy to select the smallest keyword from the current unordered area and further record the operation.C + +
How to Implement heap sorting in Python and how to implement python heap sorting
This example describes how to implement heap sorting in Python. We will share this with you for your reference. The details are as follows:
Some basic definitions of heap ordering can be found in another blog post I reproduced. http://blog.csdn.net/u010275850/article/details/45311661In fact, in the learning heap when the careful classmate can be found, as long as the deletion of the data, you can get an orderly sequence. Heap sequencing is also taking advantage of such ideas.Algorithm implementation:
Transmission DoorDescriptiongive you n integers, please output the number of the first m large in order from large to small. Inputeach set of test data has two lines, the first row has two number n,m (0Outputoutputs the number of previous m large for each set of test data in order from large to small. Sample Input3-35 92 213-644Sample Output213 92 31. Use the built-in sort function#include 2. Use the built-in priority queue#include 3. Using internal
ObjectiveThe interview was asked a question (http://www.voidcn.com/blog/u010943214/article/p-3808842.html), and then relive the heap sort.Problem:Give you a doubly linked list, ordered output,Limit:Spatial complexity O1,Time complexity Nlogn, the worst can not degenerate N2Ideas:1 building a binary tree based on a doubly linked list2 heap sorting of two-fork treesDefinition of a heapProperties of the heapHo
This is a creation in
Article, where the information may have evolved or changed.
The previous article used the Quicksort method to sort, but if the rapid sorting method for high repetition rate of slice, time complexity will soar, the speed is quite slow, so try to heap sorting, experimental results, feel very good. Here is the code, you can refer to, This is the building of the big top pile. Two fork tree
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.