題目 1 · Algorithmic & Practical Problems
10.7 分A list of numbers is to be sorted into descending order using a bubble sort. The list is: \(14, 8, 22, 19, 15, 27, 11\). (a) Show the state of the list after each of the first three passes. (b) Find the total number of comparisons and swaps performed during the complete bubble sort. (c) The sorted numbers represent the lengths, in cm, of pipes to be cut from stock pipes of length 45 cm. Use the first-fit decreasing bin-packing algorithm to determine how the pipes should be packed into stock pipes.
查看答案詳解收起答案詳解
解題
(a) Initial list: \(14, 8, 22, 19, 15, 27, 11\). For descending order: Pass 1: Compare 14 and 8 (no swap), 8 and 22 (swap), 8 and 19 (swap), 8 and 15 (swap), 8 and 27 (swap), 8 and 11 (swap). List after Pass 1: \(14, 22, 19, 15, 27, 11, 8\). Pass 2: Compare 14 and 22 (swap), 14 and 19 (swap), 14 and 15 (swap), 14 and 27 (swap), 14 and 11 (no swap). List after Pass 2: \(22, 19, 15, 27, 14, 11, 8\). Pass 3: Compare 22 and 19 (no swap), 19 and 15 (no swap), 15 and 27 (swap), 15 and 14 (no swap). List after Pass 3: \(22, 19, 27, 15, 14, 11, 8\). (b) Continuing the passes: Pass 4: Compare 22 and 19 (no swap), 19 and 27 (swap), 19 and 15 (no swap). List: \(22, 27, 19, 15, 14, 11, 8\). Pass 5: Compare 22 and 27 (swap), 22 and 19 (no swap). List: \(27, 22, 19, 15, 14, 11, 8\). Pass 6: Compare 27 and 22 (no swap). List: \(27, 22, 19, 15, 14, 11, 8\). Comparisons in each pass: 6 + 5 + 4 + 3 + 2 + 1 = 21 comparisons. Swaps in each pass: 5 + 4 + 1 + 1 + 1 + 0 = 12 swaps. (c) First-fit decreasing packing with stock of length 45 cm: Sorted list: \(27, 22, 19, 15, 14, 11, 8\). Bin 1: 27 fits. 22 does not fit (27+22=49). 19 does not fit. 15 fits (27+15=42). Remaining space: 3. No other items fit. Bin 2: 22 fits. 19 fits (22+19=41). Remaining space: 4. No other items fit. Bin 3: 14 fits. 11 fits (14+11=25). 8 fits (25+8=33). Remaining space: 12.
評分準則
(a) M1: First pass completed correctly. A1: Second pass correct. A1: Third pass correct. (b) M1: Clear attempt to count comparisons and swaps for all passes. A1: 21 comparisons. A1: 12 swaps. (c) M1: Sorting the list descending (from part b) and attempting first-fit. A1: Correctly placing 27, 15 in Bin 1 and 22, 19 in Bin 2. A1: Correctly placing 14, 11, 8 in Bin 3.