Ext.: http://www.cppblog.com/converse/archive/2008/09/28/63008.html
The merge sort algorithm (mergesort) divides a sequence into two sub-sequences of the same size, then sorts the two sub-sequences, and finally merges the two subsequence sequences into an ordered sequence. In the process of synthesis, the general realization all need to open up a space with the same size as the original sequence, For merging operations, an example of a merge
million, but if you remove the duplicates, no more than 3 million. The higher the repetition of a query string, the more users are queried for it, the more popular it is, please count the hottest 10 query strings, which requires no more than 1G of memory.Solution: Although there are 10 million query, but because of the high repetition, so in fact only 3 million of the query, each query255byte, (300w*255b
Hash statistics: This batch of massive data preprocessing (maintain a key for the
A set of randomly sorted alphanumeric numbers. Write an algorithm with a time complexity of O (n) so that the letters are sorted from small to large. Description: The letters are case-sensitive, the same letters, and the sort guard lowercase rows are in front. For example: r,b,b,b,w,w,b,r,b,w sort to:b,b,b,b,b,r,r,w,w,w#includeToday is too late, first put out the
spacewhile (Iif (r[i].key{R1[k]=r[i];i++;k++;}else//elements in the 2nd paragraph are put into R1{R1[K]=R[J];j++;k++;}while (I{R1[k]=r[i];i++;k++;}while (J{R1[K]=R[J];j++;k++;}for (k=0,i=low;iR[I]=R1[K];Free (R1);}An algorithm for merging a tripvoid Merpass (RecType r[],int length,int N)//The entire sort sequence is merged{int i;for (i=0;i+2*length-1Merge (r,i,i+length-1,i+2*length-1);if (i+length-1Merge (r,i,i+length-1,n-1); Merge these two sub-tabl
heap) #include 4, the time complexity of the heap and the complexity of spacethe best, worst, and average time complexity of the heap is O (nlogn).the space complexity of the
//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
The implementation of the sort heap and the use of heaps to sort table of Contents
A few basic definitions
Heapy: Adjusting the heap
Build: Build a heap
Insert: Inserting a new element
Removetop: Remove the top of the
For example , array a:[0:5,1:2,2:4,3:3,4:1]The results are as follows: [1, 2, 3, 4, 5], but sometimes there is a need to keep the pre-order position in a single array, as in the preceding example: [4, 1, 3, 2, 0], so a separate array ordering process is implemented using heap sorting.The code is as follows:functionArraykeys (arr) {vari = 0, Len=arr.length, Keys= []; while(I Len) {Keys.push (i++); } returnkeys; } //Determine if the vari
/** (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
Code that was previously written Public void bubblesort (int arr[]) { for (int i = 0, len = arr.length; i -1; i++) { for (int j = 0; J ) {if(arr[j + 1] Arr[j]) + 1); }}}have never considered the best case of time complexity why is O (n), a sudden look, it is not scientific ah?Until I read the following code, it dawned on me. Public voidBubblesort (intarr[]) { BooleanDidswap; for(inti = 0, len = arr.lengt
("----------++++++++++++---------------------/n");
}
Sorting Core Algorithm Description
One, treat the first record as an ordered subsequence
Second, sequentially, from the second record, insert the ordered subsequence by comparison.
Client Test Code
int main (int argc, char* argv[]){int i=0;Calogrithm demo;Demo.setlength (5);
Demo.setmemkey (1, 6);Demo.setmemkey (2, 20);Demo.setmemkey (3, 15);Demo.setmemkey (4, 7);Demo.setmemkey (5, 3);Demo.cout ();Demo. Insertsort ();Demo.cout ();return 0;}
T
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
back, and the corresponding nextpos ++
You can write it in one sentence:
for (int c = A[i]; c
When I = 0, nextpos is changed to {1, 1, 1}, but a [0] is changed to 2. So we need to reverse the for loop so that a [0] is still 0
for (int c = 2; c >= A[i]; c--) A[nextPos[c]++] = c;
However, this loop does not run correctly, because when C = 2, a [0] is changed to 2, c --
void sortColors(int A[], int n) { int nextPos[3] = {0}; for (int i = 0; i
code for this step is simple:/* Heap Sorting, Idea: * First maxheapbuild, make the root node the maximum value * then the root node and the last node's value is exchanged * then the last node is disconnected (maxsize-=1) * and then maxheapify, Because this time the root node of the left and right sub-tree is still a large heap, so only need maxheapify and do not need maxheapbuild * So from n-1 start cycle
First, the topic understandingTitle: Array al[0,mid-1] and al[mid,num-1] are each ordered, two sub-ordered segments of the array al[0,num-1] to merge, get al[0,num-1] overall order. Requires a space complexity of O (1). Note: The al[i] element is supported by the ' The first chapter of the data structure tells the order table merging, but then it is merged into the new table, the condition is while (iSecond, the implementation of the algorithmSet two
only need to maxheapify and not need maxheapbuild
* This will start from n-1 to 2.
*/
void Maxheapsort (int a[], int maxSize, int tmp[]) {
Maxheapbuild (A, maxSize); //n
For (int i = maxSize; i > 0;) { //cycle n-1 times
Swap (A, a + i);
Tmp[i] = A[i];
i--;
if (i = = 0) {
Tmp[0] = a[0];
} Else {
Maxheapindex (A, 0, i); //logn executed n-2 times.
}
}
For (int i = 0; I
A[i] = Tmp[i];
}
}
iv. Complexity
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
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.