Data structure portion 3

Chapter 3
A. Interface and Implementation
1. The following description of our definition of the interface, the error is?
If the visible list a portion of the length of the list is n, the head, the first or last, respectively, the rank tail node -1, 0, n, n + 1
Analysis: end, end node should be rank n--. 1, n-
2 the following description of the interface that we defined, the error is?
If the visible list a portion of the length of the list is n, the head, the first or last, respectively, the rank tail node -1, 0, n, n + 1

. b unordered list
1. If insertAsPred () to the following function, the result is:
Here Insert Picture Description
Can not insert node, a list of the original structure is destroyed
analysis: setting a linked list node insertion algorithm can not change the order, according to the present embodiment, the new succ node will point to their own
2. we can consider speeding up the cycle speed of access by rank as follows: if r> n2, then we can begin to constantly access pred () from the tail Sentinel, finally found from back to front rank of r node.
About this optimization is wrong?
Through such optimization, we can rank access cycle than time complexity O (n).
Analysis: half O (n) is still O (n)

. c ordered list
Procedure 1. ordered list of unique algorithm is:
the first element to retain only a section of each element is equal to
2. The binary search can such that the time complexity is reduced Θ (log2n ordered list with )?
Can not, because the rank list can not efficiently access cycle
analysis: each requires Θ (n) of time to locate the midpoint mi

. d selection sort
1.V = {11, 5, 7 , 13, 2, 3}, V is selected to sort unsorted was chosen the largest subvector element were: 13, 11, 7, 5, 3
Resolution: that is the order of the entire V
2. in order to ensure selectSort () stability of the algorithm, the measures we have taken are:
Max () for a more equal in the largest element, select one of the closest to the latter position
3. for scale n is a vector or list, and select the sort bubble sort worst time complexity is:
[Theta] (N2), [Theta] (N2)

. e insertion sort
1.insertionSort () average, the worst time complexity are: Θ (n2), Θ ( n2)
sequence 2.n elements are contained in the reverse order of the maximum number: (n (n -1)) / 2
Analysis: apparently reverse sequence number ≤ n elements (n2). Consider {n, n-1 ... 2,1 }, it can reach the upper bound. It is the maximum number of reverse (N2) n-= (. 1-n-) 2
3. For the sorted sequence into the sorting process (set a length of k):
wherein the element is positioned in front of the original sequence of the k element
4 to give the sub-step sequence after insertion of a sort of V = {2,7,13,5,3}, this time sorting portion has three elements. After a further result of the iteration is:
{2,5,7,13,3}
Resolution: 5 is inserted into the appropriate position

Chapter Test:
1. The following statement on the error vector and a list is:
a vector merge sort time complexity is O (nlog2 (n)), and the list is [Omega] (N2)
2. To insert a new node in the list node as a direct precursor p, there are four relevant statements
①p-> pred-> succ = node

②node->pred = p->pred

③node->succ = p

④ p-> pred = node
correct execution order is the statement:
③②①④
analysis: ① and ② needs correct execution of the original can be positioned directly p precursor p-> pred, p and ④ will destroy its links, so ① and ② must be executed before ④. It will be appreciated by way of the drawing process
3. For bidirectional, unidirectional list of n nodes, a node p positioning worst direct precursor are time complexity: O (1), O ( n)
Analysis: For two-way list, direct access p-> pred positioned to direct the precursor, only time O (1) is. For unidirectional list, locating its direct predecessor node needs to start one by one from the head of each access node, in the worst case needs to traverse the entire list, it takes O (n) time
4. a lookup element in the ordered list of time complexity They are: Ω (n)
Analysis: even if the lists are ordered, search efficiency is not higher than a sequential search
5. list {11, 5, 7, 13, 2, 3} for selection sort, every selectMax ( ) was chosen unsorted sublist greatest element in the order of:
13, 11, 7, 5, 3
Resolution: i.e., in the order of the entire list (not necessarily the smallest element removal process)
6.selectSort () algorithm which implementation is stable:
each pass the smallest element to move forward, a plurality of elements equal to the minimum select the most forward position in which the person.
Analysis: stable to maintain the same relative position that is equal to the original ordered sequence of elements
7. For the sorted sequence (whose length is set k) during the insertion sort:
wherein the element is positioned in front of the original sequence of k elements
8. After the insertion sort to give a first inserted sequence {2, 7, 13, 5, 3, 19, 17}, with a sorting portion 3 at this time elements. After another 2 times the iteration result is: {2, 3, 5, 7, 13, 19, 17}
analysis: 5 and 3 are inserted into the appropriate position
9. inverse number τ of a sequence is defined as the reverse sequence comparing the total number of the total number, n, of the size of the list of elements for insertion sort: O (n + τ)
Analysis: wherein each attributed to reverse the position of the element can be obtained by O (τ), taking into account each times may have to be inserted one additional comparison (the last time), the net result is O (n + [tau])
10. the length of the list of n, is equally divided into n / k segments each of length k, the different segments elements between the reverse does not exist. Insertion Sort the list of the worst time complexity is: O (nk)
Analysis: for each segment of the sort can actually be regarded separately. In certain library sorting algorithm (such as the sort STL) will first reach by other sorting algorithms effect more than two segments, and then call the entire sequence insertion sort, in order to obtain a higher actual efficiency.

Released four original articles · won praise 7 · views 675

Guess you like

Origin blog.csdn.net/qq_38101996/article/details/104554044