1. A. Not Valid, the Array Does Not Have a Sort Method

Total Page:16

File Type:pdf, Size:1020Kb

1. A. Not Valid, the Array Does Not Have a Sort Method

Chapter 10

Section 10.1

1. a. Not valid, the array does not have a sort method b. Valid. c. Not valid, Arrays.sort only accepts arrays as its argument. d. Not valid, Collections.sort does not accept arrays. e. Valid f. Not valid, Collections.sort does not accept a range.

Section 10.2

1. 40 35 80 75 60 90 70 75 compare 7 times, exchange 35 and 40

35 40 80 75 60 90 70 75 compare 6 times, exchange 40 and 40

35 40 80 75 60 90 70 75 compare 5 times, exchange 80 and 60

35 40 60 75 80 90 70 75 compare 4 times, exchange 75 and 70

35 40 60 70 80 90 75 75 compare 3 times, exchange 75 and 80

35 40 60 70 75 90 80 75 compare 2 times, exchange 90 and 75

35 40 60 70 75 75 80 90 compare 1 time, switch 80 and 80 final array: 35 40 60 70 75 75 80 90

Total of 28 comparisons, total of 7 switches after 7 passes.

3. modified algorithm: 1. for fill = 0 to n-2 2.1 Initialize posMin to fill 2.2 for next = fill+1 to n-1 2.3 if the item at next is less than the item at posMin 2.4 reset posMin to next 3.1 if posMin is not equal to fill 3.2 exchange the item at posMin with the one at fill. This does not affect big O for exchanges. The if statement is a bit less expensive than the swap. Therefore, if more than half of the exchanges would be eliminated by the if statement then there would be a net saving; otherwise the addition of the if statement adds to the execution time.

Section 10.3

1. 40 35 80 75 60 90 70 75 7 comparisons, 5 exchanges (40 and 35, then 80 and 75, then 60 and 80, then 90 and 70, then 90 and 75)

35 40 75 60 80 70 75 90 7 comparisons, 3 exchanges (75 and 60, 80 and 70, 80 and 75)

35 40 60 75 70 75 80 90 7 comparisons, 1 exchange (75 and 70) final array: 35 40 60 70 75 75 80 90

Total of 21 comparisons (28 if you count the final pass to verify order), total of 8 exchanges after 3 passes (4 passes if you count the final pass to verify order.)

Section 10.4

1. 40 35 80 75 60 90 70 75 1 comparison, 1 exchange (40 moves up one spot)

35 40 80 75 60 90 70 75 1 comparison, 0 exchanges (80 goes back into it’s original spot.)

35 40 80 75 60 90 70 75 2 comparisons (<80, >40), 1 exchange (80 moves up one spot)

35 40 75 80 60 90 70 75 3 comparisons (<80, <75, >40), 2 exchanges (75 and 80 move up one spot)

35 40 60 75 80 90 70 75 1 comparison (>80), 0 exchanges (90 goes back to original spot)

35 40 60 75 80 90 70 75 4 comparisons (<90, <80, <75, >60), 3 exchanges (75, 80, 90 move up one)

35 40 60 70 75 80 90 75 3 comparisons (<90, <80, <=75), 2 exchanges (80, 90 move up one) Final array: 35 40 60 70 75 75 80 90

Total of 14 comparisons, total 9 exchanges in a total of 7 passes.

Section 10.5

1. n = 1024, n² = 1,048,576, n*log↓2(n) = 10,240 n = 2048, n² = 4,194,304, n*log↓2(n) = 22,528

Section 10.6

1. 40 35 80 75 60 90 70 75 Initial gap set to 7/2 (or 3). First sub-array to insertion sort is: 40 75 70 1 comparison (>40), 0 exchanges. 40 75 70 2 comparisons (<75, >40), 1 exchange (75 and 70) 40 70 75

Array with sorted sub-array 1: 40 35 80 70 60 90 75 75

40 35 80 70 60 90 75 75 Second sub-array to insertion sort is: 35 60 75 1 comparison (>35), 0 exchanges. 35 60 75 1 comparison (>60), 0 exchanges. 35 60 75

Array with sorted sub-array 2: 40 35 80 70 60 90 75 75

40 35 80 70 60 90 75 75 Third sub-array to insertion sort is: 80 90 1 comparison (>80), 0 exchanges. 80 90 With gap of 3, 6 comparisons and 1 exchange total.

Gap set to 1. 40 35 80 70 60 90 75 75 1 comparison (<40), 1 exchange (40 and 35)

35 40 80 70 60 90 75 75 1 comparison (>40), 0 exchanges

35 40 80 70 60 90 75 75 2 comparisons (<80, >40), 1 exchange (80 and 70)

35 40 70 80 60 90 75 75 3 comparisons (<80, <70, >40), 2 exchanges (70 and 80 move up one spot)

35 40 60 70 80 90 75 75 1 comparison (>80), 0 exchanges.

35 40 60 70 80 90 75 75 3 comparisons (<90, <80, >70), 2 exchanges (80 and 90 move up one spot)

35 40 60 70 75 80 90 75 3 comparisons (<90, <80, >=75), 2 exchanges (80 and 90 move up one spot)

Final array: 35 40 60 70 75 75 80 90

With gap of 1, 14 comparisons total and 8 total exchanges. Total for entire sort: 20 comparisons and 9 exchanges.

For the normal insertion sort: Total of 14 comparisons, total 9 exchanges in a total of 7 passes. Section 10.7

1.

Section 10.8

1. Section 10.9 1. 3. Because that condition is already caught by (down > up) (up is incremented from the pivot, which is logically of an index > first.)

Section 10.10 1. Because an array will always be sorted by the recursive rule that element at position n+1 is >= in value to the element at position n. The verify method works in the case of an array that contains duplicate values because the method uses the condition that t[i].compareTo(t[i+1]) <= 0, therefore the two values can be equal.

Recommended publications