tailieunhanh - Data Structures and Program Design in C++ phần 6

nơi mà nó đã được chứng minh rằng bất kỳ phương pháp phân loại sử dụng so sánh của các phím phải làm ít nhất lg n! ≈ n lg n - + O (log n) so sánh của các phím. Khi n lớn, thuật ngữ đầu tiên của biểu thức này trở nên quan trọng hơn những gì còn lại. Chúng tôi đã tìm thấy, trong Mergesort, một thuật toán mà đến trong tầm tay này thấp hơn bị ràng buộc. | Section Mergesort for Linked Lists 349 that insertion sort will do is more than 250 000. A problem requiring a minute of computer time using insertion sort will probably require only a second or two using mergesort. n lg n The appearance of the expression n lg n in the preceding calculation is by no means accidental but relates closely to the lower bounds established in Section where it was proved that any sorting method that uses comparisons of keys must do at least lg n 3 n lg n - O log n comparisons of keys. When n is large the first term of this expression becomes more important than what remains. We have now found in mergesort an algorithm that comes within reach of this lower bound. 3. Improving the Count By being somewhat more careful we can in fact obtain a more accurate count of comparisons made by mergesort which will show that its actual performance comes even closer to the best possible number of comparisons of keys allowed by the lower bound. First let us observe that merging two lists of combined size k never requires k comparisons but instead at most k - 1 since after the second largest key has been put out there is nothing left to which to compare the largest key so it goes out without another comparison. Hence we should reduce our total count of comparisons by 1 for each merge that is performed. The total number of merges is essentially nnn 2 4 8 1 n - 1- This calculation is exact when n is a power of 2 and is a good approximation otherwise. The total number of key comparisons done by mergesort is therefore less than n lg n - n 1. Second we should note that it is possible for one of the two lists being merged to be finished before the other and then all entries in the second list will go out with no further comparisons so that the number of comparisons may well be less than we have calculated. Every element of one list for example might precede every element of the second list so that all elements of the second list would come out .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.