Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 12 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
12
Dung lượng
94,78 KB
Nội dung
TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 1 DATA STRUCTURES & ALGORITHMS Tutorial 6 Solutions SORTING Question 1. Many operations can be performed faster on sorted than on unsorted data. For which of the following operations is this the case? a. checking whether one word is an anagram of another word, e.g., plum and lump b. finding an item with a minimum value c. computing an average of values d. finding the middle value (the median) e. finding the value that appears most frequently in the data Solution: a. true b. true c. false d. true e. false Question 2. In which case, the following sorting algorithm is fastest/slowest and what is the complexity in that case? Explain. a. insertion sort b. selection sort c. bubble sort d. quick sort e. merge sort Solution: a. insertion sort Fastest Slowest The input is an ordered list. At every step, the element at current is only compared to the element at (current-1). Comparisons: C=n-1=O(n) The input is a reverse-ordered list. At every step, the element at current is compared to every elements preceded it. Comparisons: C=n(n-1)/2=O(n 2 ) TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 2 Moves : M=0 or M=2(n-1)=O(n) (If we change the algorithm so that it does not copy out the data in line 1.2.1 and copy in the data in line 1.2.4 in the lecture slide every time, in this fastest case, the number of moves is 0. Otherwise, we need 2 moves at each step. Then the number of moves is 2(n-1)) Moves : M=(3+4+5+ (n-1+2)) = (n-1)(n+4)/2 = O(n 2 ) (At each i, we have to copy the data out to the variable tmp, and move (i-1) elements forward, and copy the tmp to the first position) b. selection sort This algorithm performs the same in all cases. C=n(n-1)/2=O(n 2 ) M=3(n-1)=O(n) At each of iterations, we have to compare all the remained elements to select the next smallest elements, follows by an exchange (3 moves) of the current position and the smallest position. For an ordered list input, if we change the algorithm so that it does not exchange the elements if the smallest position and the current position are the same, the number of move is M=0. c. bubble sort Fastest Slowest The input is an ordered list. The algorithm stops after the first iteration. C=n-1=O(n) M=0 The input is a reverse-ordered list. C=n(n-1)/2=O(n 2 ) M=n(n-1)/2 = O(n 2 ). At each step, we have to compare and move the next smallest element up to the beginning. d. quick sort Fastest Slowest After each partition, we have two equal halves. C(n) = 2*C(n/2)+n Computing, C(n) = O(nlog 2 n) The pivot needs n comparisons to recognize that it must be first element. So, C = n+(n-1)+(n-2)+ +1=n(n+1)/2 = O(n 2 ). M = 3*n = O(n). e. merge sort This algorithm performs the same in all cases. C=O(nlog 2 n) M=0 We only have to change pointers, not move elements. TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 3 Question 3. a. Insertion sort goes sequentially through the array when making comparisons to find a proper place for an element currently processed. Consider using binary search instead and give a complexity of the resulting insertion sort. b. For a given “nearly” sorted list, which sorting algorithm (insertion, selection, merge sort) should be used? Why? Solution: a. By using binary search, we can find a proper place for an element currently processed in O(log 2 k) with the k elements is sorted in all cases. So the complexity of comparison operation is O(nlog 2 n) in all cases. The complexity of move operation is the same with straight insertion sort. b. According to question 2, the “insertion sort” is the best. The number of comparisons is around O(n) and the number of moves is around O(1) (when we prevent the 2 copy actions in line 1.2.1 and 1.2.4 in the algorithm in the lecture slide). Note that, in this case, the numbers of comparisons for selection sort and merge sort are O(n 2 ) and O(nlog 2 n), respectively. The numbers of moves are O(n) and O(1). Further note that, if the merge sort is for an array, the number of moves should be O(nlog 2 n). That is because we have to copy data into another array in partitioning and copy it back to the original array in merging. Using the same idea of calculation the number of comparisons, it is easy to show that the number of moves in this case is O(nlog 2 n). Question 4. Given a list = {13, 27, 8, 3, 21, 17, 28, 32, 91, 72}, show the sorting process step-by-step of the following algorithm. What are the number of comparisons and number of moving elements (an exchange of 2 elements is considered as 3 moves). Which is the best algorithm in this case? a. insertion sort b. selection sort c. heap sort d. bubble sort e. merge sort f. quick sort 1 1 The pivot is chosen as described in the slides TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 4 Solution: a. Insertion sort C=0, M=0 C=1, M=0 C=2, M=4 C=3, M=5 C=2, M=3 C=3, M=4 C=1, M=0 C=1, M=0 C=1, M=0 C=2, M=3 Number of comparisons: 16 Number of moving: 19 b. Selection sort C=9, M=3 C=8, M=3 C=7, M=3 C=6, M=3 C=5, M=0 C=4, M=0 C=3, M=3 C=2, M=3 C=1, M=3 13 27 8 3 21 17 28 32 91 72 13 27 8 3 21 17 28 32 91 72 8 13 27 3 21 17 28 32 91 72 3 8 13 27 21 17 28 32 91 72 3 8 13 21 27 17 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 72 91 3 27 8 13 21 17 28 32 91 72 3 8 27 13 21 17 28 32 91 72 3 8 13 27 21 17 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 23 27 28 72 91 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 5 Number of comparisons: 45 Number of moving: 21 c. Heap sort Build Heap C=1, M=3 C=2, M=3 C=2, M=3 C=4, M=6 C=6, M=9 Sort C=0, M=3 C=4, M=6 C=0, M=3 C=4, M=6 C=0, M=3 C=4, M=6 C=0, M=3 C=4, M=6 C=0, M=3 C=2, M=3 C=0, M=3 C=2, M=3 C=0, M=3 13 27 8 3 72 17 28 32 91 21 13 27 8 91 72 17 28 32 3 21 13 27 28 91 72 17 8 32 3 21 13 91 28 72 27 17 8 32 3 21 91 72 28 32 27 17 8 13 3 21 21 72 28 32 27 17 8 13 3 91 72 32 28 21 27 17 8 13 3 91 3 32 28 21 27 17 8 13 72 91 32 27 28 21 3 17 8 13 72 91 13 27 28 21 3 17 8 32 72 91 28 27 17 21 3 13 8 32 72 91 8 27 17 21 3 13 28 32 72 91 27 21 17 8 3 13 28 32 72 91 13 21 17 8 3 27 28 32 72 91 21 13 17 8 3 27 28 32 72 91 3 13 17 8 21 27 28 32 72 91 17 13 3 8 21 27 28 32 72 91 8 13 3 17 21 27 28 32 72 91 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 6 C=2, M=3 C=0, M=3 C=2, M=3 C=0, M=3 C=0, M=0 Number of comparisons: 39 Number of moving: 87 d. Bubble sort C=9, M=15 C=8, M=9 C=7, M=3 C=6, M=0 Number of comparisons: 30 Number of moving: 27 e. Merge sort 13, 27, 8, 3, 21, 17, 28, 32, 91, 72 13, 27, 8, 3, 21 17, 28, 32, 91, 72 13, 27, 8 3, 21 17, 28, 32 91, 72 13, 27 8 3 21 17, 28 32 91 72 13 27 8 3 21 17 28 32 91 72 13, 27 8 3, 21 17, 28 32 72, 91 8, 13, 27 3, 21 17, 28, 32 72, 91 3, 8, 13, 21, 27 17, 28, 32, 72, 91 3, 8, 13, 17, 21, 27, 28, 32, 72, 91 13 8 3 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 8 3 13 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 13 8 3 21 17 27 28 32 72 91 8 3 13 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 7 f. Quick sort (The pivot is chosen as described in the slides) C=0, M=3 C=1, M=3 C=1, M=3 C=1, M=3 C=1, M=3 C=4, M=0 C=0, M=3 C=0, M=3 C=1, M=3 C=1, M=0 C=0, M=3 C=0, M=3 C=0, M=3 C=0, M=3 C=0, M=0 C=0, M=3 C=1, M=0 C=0, M=3 C=0, M=3 21 27 8 3 13 17 28 32 91 72 21 8 27 3 13 17 28 32 91 72 21 8 3 27 13 17 28 32 91 72 21 8 3 13 27 17 28 32 91 72 21 8 3 13 17 27 28 32 91 72 21 8 3 13 17 27 28 32 91 72 17 8 3 13 21 27 28 32 91 72 8 17 3 13 21 27 28 32 91 72 8 3 17 13 21 27 28 32 91 72 8 3 17 13 21 27 28 32 91 72 3 8 17 13 21 27 28 32 91 72 8 3 17 13 21 27 28 32 91 72 3 8 17 13 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 32 28 27 91 72 3 8 13 17 21 32 28 27 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 28 27 32 91 72 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 8 C=0, M=3 C=0, M=3 C=0, M=3 Number of comparisons: 11 Number of moving: 45 Question 5. Repeat question 4 for the following list: {13, 3, 8, 21, 27, 17, 28, 32, 91, 72}. Solution: a. Insertion sort C=0, M=0 C=1, M=3 C=2, M=3 C=1, M=0 C=1, M=0 C=3, M=4 C=1, M=0 C=1, M=0 C=1, M=0 C=2, M=3 Number of comparisons: 13 Number of moving: 13 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 13 3 8 21 27 17 28 32 91 72 3 13 8 21 27 17 28 32 91 72 3 8 13 21 27 17 28 32 91 72 3 8 13 21 27 17 28 32 91 72 3 8 13 21 27 17 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 72 91 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 9 b. Selection sort C=9, M=3 C=8, M=3 C=7, M=0 C=6, M=3 C=5, M=3 C=4, M=0 C=3, M=0 C=2, M=0 C=1, M=3 Number of comparisons: 45 Number of moving: 15 c. Heap sort Build Heap C=1, M=3 C=2, M=3 C=2, M=3 C=4, M=9 C=6, M=15 C=0, M=3 C=6, M=9 C=0, M=3 3 13 8 21 27 17 28 32 91 72 3 8 13 21 27 17 28 32 91 72 3 8 13 21 27 17 28 32 91 72 3 8 13 17 27 21 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 91 72 3 8 13 17 21 27 28 32 72 91 13 3 8 21 72 17 28 32 91 27 13 3 8 91 72 17 28 32 21 27 13 3 28 21 72 17 8 32 91 27 13 91 28 72 27 17 8 32 21 3 91 72 17 32 27 13 8 28 21 3 3 72 17 32 27 13 8 28 21 91 72 32 17 28 27 13 8 3 21 91 21 32 17 28 27 13 8 3 72 91 TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 10 C=4, M=6 C=0, M=3 C=4, M=6 C=0, M=3 C=4, M=6 C=0, M=3 C=2, M=3 C=0, M=3 C=2, M=3 C=0, M=3 C=2, M=3 C=0, M=3 C=2, M=3 C=0, M=3 C=0, M=0 Number of comparisons: 36 Number of moving: 99 d. Bubble sort C=9, M=12 C=8, M=3 C=7, M=0 Number of comparisons: 24 Number of moving: 15 32 28 17 21 27 13 8 3 72 91 3 28 17 21 27 13 8 32 72 91 28 27 17 21 3 13 8 32 72 91 8 27 17 21 3 13 28 32 72 91 27 21 17 8 3 13 28 32 72 91 13 21 17 8 3 27 28 32 72 91 21 13 17 8 3 27 28 32 72 91 3 13 17 8 21 27 28 32 72 91 17 13 3 8 21 27 28 32 72 91 8 13 3 17 21 27 28 32 72 91 13 8 3 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 8 3 13 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 3 8 13 21 17 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 3 8 13 17 21 27 28 32 72 91 [...]... TRƯỜNG ĐẠI HỌC BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 3 8 13 17 21 27 28 32 3 8 13 17 21 27 28 32 C=0, M=3 72 91 72 91 C=0, M=0 Number of comparisons: 15 Number of moving: 30 Question 6 Implement and analyze the complexity of the following nonrecursive version of merge sort First, merge subarrays of length 1 into n/2 two-cell subarrays, possibly one of them being a one-cell array The... one, two, or three cells, and so on, until the entire array is ordered Note that this is a bottom-up approach to the merge sort implementation, as opposed to the top-down approach discussed in the slides Solution: void MergeSort(Array list){ for(step=2 ; step . M=3 C=2, M=3 C=2, M=3 C=4, M =6 C =6, M=9 Sort C=0, M=3 C=4, M =6 C=0, M=3 C=4, M =6 C=0, M=3 C=4, M =6 C=0, M=3 C=4, M =6 C=0, M=3 C=2, M=3 C=0, M=3. KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 1 DATA STRUCTURES & ALGORITHMS Tutorial 6 Solutions SORTING Question 1. Many operations can be performed faster on sorted. BÁCH KHOA TP.HCM Khoa Khoa học & Kỹ thuật Máy tính 10 C=4, M =6 C=0, M=3 C=4, M =6 C=0, M=3 C=4, M =6 C=0, M=3 C=2, M=3 C=0, M=3 C=2, M=3 C=0, M=3 C=2, M=3