complexity is O (n^2).Select the sorting algorithm:Principle:First, the smallest (large) element is found in the unsorted sequence and placed at the first end of the sequence.* * Then find the smallest (large) element from the remaining elements and place it at the end of the sequence.It loops until the sort is complete.Instance:* Examples: 5, 4, 8, 9, 2, 1* 1th Round 1, 4, 8, 9, 2, 5 (1 5 Exchange)* 2nd R
the string length. Linear timeIf the time complexity of an algorithm is O (n), the algorithm is said to have linear time, or O (n) time. Informally, this means that for large enough inputs, the size of the run time increases linearly with the input. For example, a program that computes all the elements of a list and takes the time to be proportional to the length of the list.See details of Alex King's documentsHttp://www.cnblogs.com/alex3714/articles
) + O (H2) ... + O (HLEN/2) where h represents the depth of the node, LEN/2 represents the number of nodes, this is a summation process, the result is a linear O (n).
Tuning the heap: tuning the heap is used during the build heap and will be used during the heap sequencing process. The idea used is to compare node I an
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
Introduction
Note: Because no public editor is enabled, it is convenient: when the following time complexity is involved, its accesskey is replaced by the following symbol:
This article describes three algorithms that can sort n numbers in O (nlgn) Time: Merge Sorting, heap sorting, and quick sorting. Both algorithms are based on comparative sorting. The first
featuresHeapsort is a tree-based sorting method.The characteristic of heap sorting is that during the sorting process, R [L .. n] As a Complete Binary Tree ordered storage structure, using the inherent relationship between the parent and child nodes in the Complete Binary Tree [see the binary tree ordered storage structure ], select the record with the maximum or minimum keyword in the unordered area.
4. Differences between
of the datum point. This will not be the same as the bubble sort in each exchange, the same can only be exchanged between the adjacent number, the distance is much larger. Therefore, the total comparison and the number of exchanges is less, the speed naturally increased. Of course, in the worst case, it is still possible to exchange two consecutive numbers. So the worst time complexity for fast sorting is
the first line of code, which facilitates the uniformity of the insert operation without considering the head node in addition. The next step is to traverse the entire list, find the right location, and insert it!2, the second problem is relatively simple, you can see the aggregation of arrays, the code is as follows:Private ListNode mergetwolists (ListNode head1, ListNode head2) { ListNode head = new ListNode (0); ListNode root = head; while (Head1 = null head2! = null) { if (Head
maximum heap using the process max-heapify. The principle is very simple, that is, from the bottom 2nd level (for illustrative convenience, we call the root node 1th layer, its sub-node is called the 2nd layer, and so on) to start, called Max-heapfy method, until the root node. The algorithm is described as follows: We can prove the correctness of the above algorithm by using the cyclic invariant (see algorithm Introduction (1)) described previously
1 /**2 * Heap sequencing ideas: O (NLOGN)3 * Use the maximum heap, pass in an array, first use the number to build the heap, maintain the nature of the heap4 * Replace the first number with the last number in the heap, because the first number is the largest5 * Reduce the size of the
Analysis of complexity of bubble sort time
Bubble Sorting repeatedly visits the series to be sorted and compares two elements at a time. If their order is wrong, they will be exchanged. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted.The operation of the Bubble Sorting Algorithm is as follows: (from the back to the front) 1. Compare adja
, the heap sort is also a sort of selection, a sort of tree selection. Just select the sort directly, in order to select the maximum record from R[1...N], you need to compare n-1 times, then select the maximum record from r[1...n-2] to compare n-2 times. In fact, many of the
is 10 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 fo
depth direction.(3) Heap sorting: Heap sorting is done using the two processes above. The first is to build the heap from the elements. The root node of the heap is then removed (typically in exchange with the last node), the previous Len-1 node is continued for the heap ad
the above process, the heap sort is also a sort of selection, a sort of tree selection. Just select the sort directly, in order to select the maximum record from R[1...N], you need to compare n-1 times, then select the maximum record from r[1...n-2] to compare n-2 times. In
The basic idea of bubble sorting is to compare two adjacent elements each time and swap them out if they are in the wrong order.The principle of "bubble sort" is that each trip can only be determined by the return of one digit. That is, the first trip can only be determined to the last number (that is, the 5th digit), the second trip can only the number of the last 2nd digit (that is, 4th digit), the third trip can only be the number of the countdown
the heap sorting algorithm, we are using the largest heap. The minimum heap is typically used to construct a priority queue,If you think of a heap as a tree, we define the height of the nodes in a heap as the number of nodes to the top of the longest simple path of the leaf
the I node is2*i+1and the2*i+2. 3, Heap sorting algorithm implementation (maximum heap)#include 4, the time complexity of the heap and the complexity of spacethe best, worst, and average time complexity of the
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 th
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.