1 Packagesorting;2 3 /**4 * Heap Sorting5 * Average O (nlogn), preferably O (nlogn), Worst O (nlogn), Space complexity O (1), unstable, more complex6 * @authorZeng7 *8 */9 Public classDuipaixu {Ten One Public Static voidDuipaixu (int[] a) { A inti, TMP; - for(i = A.LENGTH/2; I >= 1; i--) - Heapadjust (A, I, a.length); the for(i = a.length; i > 1; i--) { -TMP = A[0]; -A[0
1. The concept of heapsThe data structure of a heap is an array object, and the heap can be regarded as a complete binary tree (where each layer of the tree is filled and the last layer may be excepted); Each node in the tree corresponds to the element in the array that holds the value of the node;The heap can be divided into two categories:A) Maximum
Definition of heap:
N-keyword sequences kl,k2,...,kn called heaps, when and only if the sequence satisfies the following properties (referred to as heap properties):
(1) Ki≤k2i and Ki≤k2i+1 or (2) ki≥k2i and ki≥k2i+1 (1≤i≤)
If the vector stored by this sequence is R[1..N] as a storage structure of a complete binary tree, the heap is essentially a complete bina
is a data structure problem, using the heap for weight adjustment and sequencing, each adjustment is O (n) complexity, very efficient.The first view of the problem can be done with a priority queue, it should be very simple.In fact, most priority queues are implemented through heaps.There were some problems with the writing:1, the second Fork root node subscript is obviously not 0;2. If there is a widening
Quick row:
The quick sort uses two points of thought, each time selects a datum element, the element which is compared to the datum element is placed in front of the datum element, is placed behind the datum element than the element of the datum element, and so on, and so on recursively subdivide, completes the sort. C + + code implementation:
void QuickSort (int a[], int l, int r) {
int temp = a[l];
Vi. sequencing of HeapsHeap sorting is a tree-select sort, which is an effective improvement on direct selection sequencing.Heap definition: A sequence with n elements (h1,h2,..., HN), when and only if satisfied (hi>=h2i,hi>=2i+1) or (hi
Basic idea: the sequence of numbers to be sorted is initially treated as a two-fork tree of sequential storage, which adjusts their storage order to become a heap, when
Read a lot of bloggers wrote the principle of heap sorting, are explained quite clear, is the code implementation (mainly the Java language) some people dazzling. I have re-organized the code implementation of the heap sort (Java).What are the problems and irregularities, but also hope that the partners remind me to correct them in time. Thank!!
Package Cn.xf.algorithm.ch09greedy.util;import Java.util.arraylist;import java.util.list;/** * Heap Construction and Sorting * *. function: Heap construction * 1, The heap can be defined as a binary tree, the node of the tree contains the key, and satisfies the condition * 1) The shape requirements of the tree: This binary tree is a basic complete (full binary tr
Copy Code code as follows:
?
Heap Sorting application
Class Heapsort
{
var $a;
function SetArray ($a)//Get array
{
$this->a= $a;
}
function Runvalue ($b, $c)//$a represents an array, $b represents the sort heap, and the $c represents the end point,
{
while ($b {
$h 1=2* $b;
$h 2= (2* $b + 1);
if ($h 1> $c)
Break
ElseIf ($h 1== $c)
{
if ($th
, ""), NewDatawrap (9, "") }; System.out.println ("Before sorting: \ n" +java.util.Arrays.toString (data)); Heapsort (data); System.out.println ("After sorting: \ n" +java.util.Arrays.toString (data)); }}DescriptionThe key to the heap sequencing above is the Buildmaxheap () method. This method is used to build a large top heap on the data array from the 0 to the lastindex indexe
PackageKpp.sort;/*** The heap is defined as follows: sequence of n elements {k0,k1,..., ki,..., k (n-1)} When and only if the next relationship is satisfied, called a heap. "ki@authorKPP **/ Public classHeapsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub intArray[] = {49,38,65,97,176,213,227,49,78,34,12,164,11,18,1}; Heapsort (array); for(intK:array) {
1, the code is as follows PackageBetter.amy.sort;/*** Heap Sort Implementation * *@authorZhujinrong **/ Public classHeapsort {/*** The result of constructing a large heap of large piles is the ascending order * *@paramA * array A *@paramm * Start position *@paramn * End Position*/ Private Static voidHeapadjustmax (intA[],intMintN) {intt =A[m]; for(inti = 2 *
, take biglargest = arr[index]>arr[largest]?index:largest; //between the big and the parent nodes in the left and rightif (Largest==index) {//If the parent node is large, it indicates that the original location is unchanged, and then returns. Return}Swap (Arr,largest,index);index = largest;left = index*2+1; //Layer -by-layer judgment}}//each element is inserted and a new large heap is created for initialization purposes. public static void Heapinsert
for an int array. Please write a heap sorting algorithm. Sorts the array elements.Given an int array a and the size of the array n, return the sorted array.Test examples:[1,2,3,5,2,3],6[1,2,2,3,3,5]Class Heapsort {public:int* heapsort (int* A, int n) { buildmaxheap (A, n);//First build the array A into a large top heap for (int i = n-1; i > = 1; i--) { swap (a[0],a[i]);//Exchange Ma
Massive log data, extract a day to visit Baidu the most times the IP. since it is massive data processing, it is conceivable that the data to us must be huge. How do we get started with this massive amount of data? Yes, it's just a divide-and-conquer/hash map + hash statistics + heap/fast/merge sort, plainly, is the first mapping, then statistics, the last sort:
on the right)*/ if(R 1 A[r] >A[largest]) {Largest= R;//I go, the right child wants to compare with the most soldiers, OK, the right child more than the throne to you. } /** If I'm not the biggest, there's no way, the top throne * to the biggest son*/ if(Largest! =root) {_swap (a[root], a[largest]);//This will, have tochange seats with the son who has the most soldiers.Maxheapify (A, largest, heap_size);//change to the big son's house, mom, still have to see there is no break the bal
first element
//loop is the exchange of the first element and the last element of the current
//Ensure that the current last position of the element is now the largest of this sequence
//constantly reduce the scope of the adjustment heap, each adjustment is completed to ensure that the first
The //element is the largest element of the current sequence
For (i = length-1; i > 0; i.)
{
Arr[i] = Arr[0]^arr[i];
Arr[0] = Arr[0]^arr[i];
Ar
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.