Definition of Merge Sorting
The merge sorting algorithm adopts the grouping algorithm. That is, two (or more) ordered tables are merged into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each sub-sequence is ordered, and then the ordered sub-sequence is merged into an overall ordered sequence. This process is also called a 2-path merge. note: a stable sort of Merge Sorting, that is, the order of equal elements will not change.
Principles of Merge S
//你知道为什么快速排序的时间复杂度是nlgn吗? We look at the array as a complete binary number and put it in a small heap,The ordering of each node requires O (h), which is H, H is the height of the two fork tree of the fully corresponding node,n is the total number of nodes, the row of a node needs O (h) times, then row n nodes?and 2^h=n-1 (full binary tree nature);->H=LOG2 (n); when there are N nodes that need to be sorted is N*LG (n),The time
analysis of time complexity of recursive function
(1) Recursive execution processExample: Seek n!.This is a simple "tired multiply" problem, with recursive algorithm can also be solved.N!= n * (n-1)! n > 10!= 1, 1! = 1 N = 0,1Therefore, the recursive algorithm is as follows:Java codeFact (int n) {if (n = = 0 | | n = = 1)return 1;Elsereturn n * fact (n-1);}Take n=3 as an example, look at the running process
Introduction
List sorting is a common algorithm problem for linked lists, often we will use bubble sort or choose the sort of two algorithms to solve this problem. But their time complexity is O (n²), the efficiency is not high. So today we are going to implement the time Complexity O (NLOGN) algorithm and we can no l
Note: The interview book has related exercises, but the ideas are relatively unclear and the layout is incorrect. The author has rewritten the related books and opinions for your reference.
11. Data Structure interview 12-sorting 3 (Summary of Sorting Algorithm classification, sorting time, spatial complexity, and stability)
Sorting Algorithm Classification
Insert sorting class
Select sorting c
-3) + O (1) + O (1) + O (1)= ......= O (1) + ... + O (1) + O (1) + O (1)= n * O (1)= O (n)The time complexity of this example is linear.: The following recursive equation:T (n) = 2T (N/2) + 2, and assumes the K-square of the n=2.T (n) = 2T (N/2) + 2= 2 (2T (n/2*2) + 2) + 2= 4T (n/2*2) + 4 + 2= 4 (2T (n/2*2*2) + 2) + 4 + 2= 2*2*2T (n/2*2*2) + 8 + 4 + 2= ...= 2 (k-1) sub-square * T (N/2 (i-1)) + $ (i:1~ (k-1)
RecursionAlgorithmThe time complexity calculation equation is a recursive equation:
Before introducing a recursive tree, consider the following example:
T (n) = 2 T (n/2) + N2
Iterations can be:
T (n) = n2 + 2 (2 T (N/4) + (n/2) 2)
You can continue the iteration and expand it completely:
T (n) = n2 + 2 (n/2) 2 + 2 (n/22) 2 + 2 (n/23) 2 + 2 (n/24) 2 +... + 2 (n/2I) 2 + 2 T (N/2I + 1 )))...))))
This paper describes the scheduling algorithm with time complexity of N*logn: merge sort, quick sort, heap sort, and hill sort principle, Java implementation and transform application.First, merge sort
Principle: Combine two ordered series into an ordered series. Recursive implementation is required.
Java implementations:
1 Public int[] MergeSort (int[] A,intN)2 {3 returnD
high, high priority is the same low-priority high. Base sorting is based on sorting separately and is collected separately, so it is a stable sorting algorithm.(7) Hill Sort (shell)Hill sort is the insertion of elements according to different steps, when the first element is very disordered, the step is the largest, so the number of elements inserted in the order is very small, fast; when the elements are basically ordered, the step size is very low, and the insertion sort is very efficient for
Package ms100;/** * A step will always have an n level, assuming you can jump 1 levels at a time. can also jump 2 levels, to find the total number of total common jump method. And analyze the time complexity of the algorithm * Note: This problem is often seen in the near future. including MicroStrategy and other more attention to the algorithm of the company has
Eight Sorting Algorithm JAVA Implementation (time complexity O (n * n), eight java
This article mainly describes three sorting algorithms with a time complexity of n2: Bubble sorting, select sorting, and insert sorting.
1. Bubble Sorting:Starting from the array header, two elements are compared at a
// N indicates the length of the array ArrayInt find (INT [] array, int N, int X ){Int I = 0;Int Pos =-1;For (; I If (array [I] = x) Pos = I;}Return Pos;}The time complexity is O (n)// N indicates the length of the array ArrayInt find (INT [] array, int N, int X ){Int I = 0;Int Pos =-1;For (; I If (array [I] = x ){Pos = I;Break;}}Return Pos;}Preferably O (1), and worst is O (n)Average
For a common sorting algorithm, it is difficult to sort in O (n) time, and the space complexity is O (1), here is a way to achieve the requirements.You can use the idea of hash sorting, which is to hash all the numbers into a hash table and implement sorting. The specific algorithm idea is to find out the maximum and minimum value of this set of data, in three kinds of situations to discuss:1, if the minimu
Title: Enter the age of a group of employees, which is O (n) time complexity to sort themIdeas:1. Using the idea of space efficiency for time efficiency, create a new array of length 100 countage to save the age of the 0-100-year-old employee2. Traverse Countage The Age assignment ages array, the age appears several times to assign the value several times, for ex
Enter a one-way list to output the last K nodes in the linked list. The bottom No. 0 node of the list is the tail pointer of the linked list. My idea is 2 pointers to the back, you think Ah, if it is a pointer, it must traverse 2 times, the first traversal total number of nodes, the second time to traverse the final result//Such a practice is obviously not good enough, time
AA bwsw F DNew string: # W # a # a # B # w # s # w # F # d #Auxiliary array p:1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1Here is a good property,p[id]-1 is the length of the palindrome string in the original string (including ' # '). If this is not particularly clear, you can take out the paper to draw a picture, you experience. Of course, everyone here may be different, but I think the general idea should be the same.The key question now is how to find the P array within the O (n)
C language: Two applets with time complexity of O (1)Both examples use the same programming philosophy: For character operations, the char type can contain a maximum of 256 characters, but can meet the needs of upper and lower case, number, and other basic characters. Use the ascii code value of the character as the subscript of the array with the size of 256, and fill the content/condition to be searched i
Select the problem (linear time complexity) and choose linear
Use the sub-Governance Policy to find the small K element! Requires that the time complexity of the program be a linear function.
# Include
TopicGiven a table header in a single-linked list and a node waiting to be deleted (not a table header or footer). Please delete the linked list node in the O (1) time complexity. and returns the header after the node is deleted.IdeasSince O (1) is deleted, it is not possible to traverse from the head node to find his predecessor.Because the node is being deleted, if the next node is deleted, it can be comp
These two examples all use the same programming idea: for character manipulation, the char type can have a maximum of 256 characters, but it can satisfy the use of case, number, and other basic characters. Using the ASCII value of the character as the subscript of an array of size 256, the content/condition to be found is populated into the array so that the corresponding action can be achieved by looking at the contents of the corresponding subscript.This is 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.