Internal Sort-10th chapter-"Data structure problem set" problem analysis-Min 聯繫 version

Source: Internet
Author: User

* * Stay pit to fill **//

First, the basic knowledge problem

10.1? Take the key code sequence (503,087,512,061,908,170,897,275,653,426) as an example, manually execute the following sorting algorithm, write out the key code status at the end of each trip:

(1) Direct insertion sort; (2) Hill sort (increment d[1]=5);

(3) Quick sorting; (4) heap sorting;

(5) Merge sort; (6) Base order.

10.2. If the following keyword sequences are sorted quickly and sorted by the algorithms listed in sections 10.3 and 10.5 of the textbook, the results of the three call process partition and the process merge are written separately.

(Tim,kay,eva,roy,dot,jon,kim,ann,tom,jim,guy,amy)

10.3. Which of the various sequencing methods listed in the 10.1 question is stable? What is unstable? And for each kind of unstable sorting method to cite an unstable instance.

10.4. Ask: How many times do you need to make a direct insert sort of each sequence with the initial state (length n), which requires the sorted sequence to be ordered by keyword from the beginning to the very large order of the keywords?

(1) Keywords (from childhood to large) orderly order; (Key1<key2<...<keyn)

(2) Keywords (arrogant to small) reverse order; (Key1>key2>...>keyn)

(3) Ordinal is an odd number of key word order, ordinal number is even the key word order;

(key1<key3< ...., Key2<key4 ...)

(4) The first half of the sequence of the key word order, the following half of the sequence of keywords in reverse order:

(Key1<key2<...<key└n/2┘, Key└n/2┘+1>...keyn)

10.5. Let's say that the element AK that satisfies the condition Ak<max{at} (1≤t<k) in the sequence {a1,a2,..., an} of n elements is called an "inverse element". If there is a pair of element Ai>aj (I<J) in an unordered sequence, ask when AI and AJ are exchanged (that is, the sequence is set by {... ai...aj ...} Change to {...} aj...ai ...} ), will the number of reverse elements in this sequence increase? Why?

10.6. The parity exchange sort is as follows: The first trip compares all odd I, a[i] and a[i+1], and the second trip compares all the even I, a[i] and a[i+1], and if A[I]>A[I+1], the two are exchanged; the third trip is odd I; ..., and so on until the entire sequence is ordered.

(1) What is the end condition of this sort method?

(2) Analyze the number of keyword comparisons that need to be made during parity-exchange sequencing when the initial sequence is in either positive or reverse order.

10.7. It is not difficult to see that the number of comparisons required to make a quick sort of a record sequence of length n depends on the initial arrangement of the n elements.

(1) How many comparisons should be made in the best case when n=7? Please explain why.

(2) Give an initial permutation instance of the best case for n=7.

10.8. Test proof: When the input sequence is already rendered in an ordered state, the time complexity of the quick Sort is O (N2).

10.9. If you rewrite the one-time division of the quick sort to the following form, rewrite the fast-sorting algorithm, and discuss the number of times (including three) of the key words that need to be compared in the best case for a quick sort of a record sequence of length n.

int partition (sqlist &l, int low, int. high, BOOL CI, BOOL CJ)

{

int I, j, M;

KeyType x;

i = s;

j = t;

m = (s+t)%2;

x = Midkey (S, M, T); To take the median value of three

CI = CJ = FALSE;

while (I<J)

{

while ((I<J) && (r[i].key<=x))

{

i = i + 1;

if (R[i].key<r[i-1].key)

R[I]↔R[I-1];

CI = TRUE;

}//while

while ((J>i) && (r[j].key>x))

{

j = j-1;

if (R[j].key>r[j+1].key)

{

R[J]↔R[J+1];

CJ = TRUE;

}//if

if (I<J)

{

R[I]↔R[J];

if ((i>s) && (R[i].key<r[i-1].key))

CI = TRUE;

if ((j<t) && (R[j].key>r[j+l].key))

CJ = TRUE;

}//if

}//while

}//while

return i;

}//partition

10.10. Read the following sorting algorithms and compare the number of executions of the basic operations in the algorithm compared to the algorithms that have been learned.

void sort (sqlist &r, int n)

{

I=1;

while (i<n-i+1)

{

Min = max = 1;

for (j=i+1; j<=n-i+1; ++j)

{

if (R[j].key<r[min].key)

min = j;

else if (R[j].key>r[max].key)

max = J;

}//for

if (min!=i)

R[min]↔r[i];

if (max!=n-i+1)

{

if (max==i)

R[MIN]↔R[N-I+1];

Else

R[MAX]↔R[N-I+1];

}//if

i++;

}//while

}//sort

10.11. Ask: In order to decide the rank of eight athletes according to the idea of tournament, at least how many matches should be arranged (should consider the worst case)?

10.12. Determine if the following sequence is a heap (small top heap or large top heap). If it is not, then adjust it to a heap (requiring the least number of records to be exchanged).

(1) (100, 86, 48, 73, 35, 39, 42, 57, 66, 21);

(2) (12, 70, 33, 65, 24, 56, 48, 92, 86, 33);

(3) (103, 97, 56, 38, 66, 23, 42, 12, 30, 52, 06, 20);

(4) (05, 56, 20, 23, 40, 38, 29, 61, 35, 76, 28, 100).

10.13. A sequence of length n, if one of the few K (k<<n) records is removed, the sequence is ordered by the keyword, which is called an approximate ordered sequence. The time complexity of various simple sorting methods is discussed in this series.

10.14. Assuming that the sequence consists of a record of n different keywords, which requires the first K (K<<n) records of the keyword to be selected from a large to a small order without ordering, how can we minimize the number of comparisons made between the keywords?

10.15. For a sequence of records that are different from n keywords, can you choose a record of the maximum and minimum number of keywords in these n records with less than 2n-3? If so, how do you make it work? How many comparisons are performed at least in the worst case scenario?

10.16? A sequence of N records is known, and the keywords are integers between 0 and N2. The time complexity is O (NLOGN) If sorting is done using methods such as heap sorting. If each keyword ki is recognized as Ki =ki1n+ki2, where both Ki1 and Ki2 are integers in the range [0, N), then only the time of the O (n) is used to sort the radix. promotion, if the integer keyword range is [0, NK), you can get a time-only O (KN) sequencing method, try to discuss how to achieve it.

10.17. It is known that a single-linked list consists of 3,000 elements, each of which is an integer whose value is between 1~1000000. What methods can be used to solve the sorting problem of the list in the 10th chapter? Which can't? Brief reasons.

10.18. In the two methods of multi-keyword sequencing, what are the conditions under which the MSD method is more efficient than the LSD method?

10.19. Suppose that there are 5,000 beds in a large hotel, each day according to the documents of the guests to make a roster, the roster is required by the order of the Province (city), each province (city) ranked by County (district), but also the same county (district) of passengers by surname. Please design a method for the management of the hotel to make this roster.

10.20. The probabilities of the six permutations that are known to be sorted by the three integers, a, B, and C (A≠b≠c≠a), are different, as shown in the following table:

A<b<c

B<a<c

A<c<b

C<a<b

B<c<a

C<b<a

0.13

0.24

0.08

0.19

0.20

0.16

Try to design an optimal sorting scheme for the sequence to minimize the expected number of comparisons between the keywords that need to be performed during the sorting process.

10.21. The sequence of 4 records is sorted by using the binary Insert Sort method and the 2-way merge sort method, and the decision tree describing the sorting process is drawn, and the maximum value of the number of comparisons between the keywords that they need to make is compared.

10.22. Merge Insert sort is an internal sort method that minimizes the number of times a keyword is compared, which can be done as follows (assuming that the element to be sorted is stored in the array X[1..N]):

(1) Another two arrays of size ┏n/2┓ small and large are opened. From I=1 to n-1, for each odd I, compare x[i] and x[i+1], the smaller and the larger ones are sequentially deposited into the array small and large (when n is odd, small[┏n/2┓]=x[n]);

(2) The elements in the array Large[1..┗n/2┛] are merged into the sort, and the elements in the small arrays are adjusted accordingly, so that at the end of this step the Large[i]<large[i+l],i=1,2,...,┗n/2┛-1,small[i is reached] <large[i],i=1,2,...,┗n/2┛;

(3) Transfer small[1] to x[1], large[1] to Large[┗n/2┛] in turn to x[2] to x[┗n/2┛+1];

(4) Define a set of integers int[i]= (2i+1+ ( -1) i)/3,i = 1,2,...,t-1, until int[t]>┗n/2┛+1, using binary insert in turn small[int[i+1]] to small[int[i]+1] Insert into the x array. For example, if n=21, you get a set of integer int[1]=1,int[2]=3,int[3]=5,int[4]=11, so the elements in the small array should be in the following order: Small[3],small[2],small[5],small[4], SMALL[11],SMALL[10],...,SMALL[6], insert into the x array.

Try the merge insert sort manually with N=5 and n=11, and calculate the number of keywords that were compared during the sorting process.

Second, the algorithm design problem

10.23. Try using l.r[k+1] as a sentinel to rewrite the direct insertion sorting algorithm given in the textbook 10.2.1 section. Where L.R[1..K] is the record to be sorted and k<maxsize.

10.24. Try to compile the algorithm for the 2-way insertion sort described in the textbook 10.2.2 section.

10.25. Try to write the algorithm of the linked list insertion sorting in the textbook 10.2.2 section.

10.26? As described below, rewrite the bubble sorting algorithm described in section 10.3 of the textbook: the 1.4.3 section of the algorithm used to control the Boolean variable change to an integer variable, indicating the position of the last record to be exchanged in each pass, and use it as the control value for the next bubbling sort loop termination.

10.27. Write a two-way bubble sorting algorithm, that is, adjacent two times in the opposite direction bubbles.

10.28. To modify the algorithm required in the 10.27 question, consider how to avoid writing the algorithm into two similar one-way blistering algorithm segments.

10.29. Write the algorithm of parity exchange sorting as described in 10.6.

10.30. Write a non-recursive algorithm for fast rows as follows:

(1) After the sequencing, if the sub-sequence is ordered (no Exchange), then do not participate in the sorting, otherwise the shorter length of the sub-sequence is sorted, and the other sub-sequence of the upper and lower bound into the stack to save;

(2) If the number of records to be ≤3, it is no longer split, but directly to compare the sort.

(1) Ordered sequences are no longer involved in sequencing.

(2) Sort by bubbling when the sequence record ≤3.

10.31. Write an algorithm that organizes the sequence of records with integer values for n keywords, so that records with negative values for all keywords are preceded by records with a non-negative keyword, requiring:

(1) Using sequential storage structure, up to a record of secondary storage space;

(2) The time complexity of the algorithm is O (n);

(3) Discuss the maximum number of moves recorded in the algorithm.

(1) ~ (2) The algorithm satisfies the condition as follows:

(3) When a record with a positive keyword is all ranked before a record with a negative keyword, the maximum number of moves is required: N/2.

10.32. Question of the flag of the Netherlands: there is a series of bars consisting only of three colors, red, white, and blue. Write an algorithm with a time complexity of O (n) so that the bars are lined up in red, white, and blue, which is the Dutch flag pattern.

10.33. Try to use single-linked list as the storage structure to implement simple selection sorting algorithm.

10.34. Known (K1,K2,..., KP) is a heap, you can write an algorithm with a time complexity of O (Logn) to adjust (k1,k2,..., kp,kp+1) to the heap. This paper tries to write the algorithm of "inserting and constructing the heap from P=1," and discusses the time complexity of building the heap by this method.

10.35. Suppose that the definition heap is a complete three-pronged tree that satisfies the following properties: (1) The empty tree is a heap, and (2) the value of the root node is not less than the value of all sub-roots, and all subtrees are heaps. The algorithm used to sort the heap using the defined above is written, and the time complexity of the derivation algorithm is analyzed.

10.36. The merge sort can be implemented as follows: Suppose there is an ordered subsequence with K length ≤l in the sequence. Using the process merge (see section 10.5 of the textbook), they are 22-merged to get an ordered subsequence of ┏k/2┓ length ≤2l, called a merge sort. Repeated calls to merge the ordering process, so that the length of ordered sub-series from l=1 began to multiply exponentially, until the entire sequence into an ordered sequence. This paper tries to implement the recursive algorithm of the above merge sort, and analyzes the time complexity of the algorithm.

10.37. Use linked list storage structure, redo 10.36 questions.

10.38? 2-Way Merge sort another strategy is to scan the sort sequence first, find and divide into several of the largest ordered child columns, and use these as the initial merge segments. Try to write an algorithm to implement this strategy on the linked list structure.

10.39. Two ordered sequences (A1,A2,..., am) and (am+1,am+2,..., an) are known, and the number of records in one of the sequences is less than s and the S=┗ radical N┛. Try to write an algorithm that uses O (n) time and O (1) additional space to complete the merging of these two ordered sequences.

10.40. Assuming that all two sequences have at least s records, redo 10.39 questions.

10.41 fake a record sequence with 1000 keywords of less than 10000 integers, design a sorting method that requires sorting with as few comparisons and moves as possible and compiling the algorithm according to your design.

10.42. The "median record" of a sequence refers to the ┌n/2┐ record if this sequence is sorted. Try to write an algorithm to find the median record.

10.43. The keywords in the known record sequence A[1..N] are different and can be sorted as follows: An array of C[1..N], a[i for each record, the number of records in a statistical sequence that is smaller than it is stored in c[i], then the c[i]=0 record must be the smallest record of the keyword, Then according to the size of the c[i] value of the records in a re-arrangement, try to write the algorithm to achieve the above sorting method.

10.44? Suppose that in a sequence with N records, all of its keywords are integers with values between V and W, and many of the keywords are the same. You can sort by the following method: Set the array number[v. W] and the number[i] statistic keyword takes the number of records of the integer I, and then the sequence is sorted in order to achieve order. Try to write the algorithm to achieve the above ranking method, and discuss the advantages and disadvantages of this method.

10.45. Try to write the algorithm, using "count" to achieve the cardinal sort.

10.46. Each element of sequence B is a record, with each record occupying a much larger amount of storage than its keywords, so the recorded movement is extremely time-consuming. Try to write an algorithm that puts the ordered result of sequence B into sequence A, with each record being copied only once without any other movement. Your algorithm can invoke any sort algorithm given in the 10th chapter. Thinking: When the record in the linked list, if you want to use the fast sorting algorithm to sort the keywords, so that the final list of the list, how to imitate the above method implementation?

Internal Sort-10th chapter-"Data structure problem set" problem analysis-Min 聯繫 version

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.