ch08 - 1.1 Indicate whether each of the following method...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1.1 Indicate whether each of the following method calls is valid. Describe why it isn’t valid or, if it is valid, describe what it does. Assume people is an array of Person objects and peopleList is a List of Person objects. a. people.sort(); b. Arrays.sort(people, 0, people.length - 3); c. Arrays.sort(peopleList, 0, peopleList.length - 3); d. Collections.sort(people); e. Collections.sort(peopleList, new ComparePerson()); f. Collections.sort(peopleList, 0, peopleList.size() 3); 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. 2.1 Show the progress of each pass of the selection sort for the following array. How many passes are needed? How many comparisons are performed? How many exchanges? Show the array after each pass. 40 35 80 75 60 90 70 75 50 22 Pass Array After Pass Comparisons Exchanges 0 40 35 80 75 60 90 70 75 50 22 0 0 1 22 35 80 75 60 90 70 75 50 40 9 1 2 22 35 80 75 60 90 70 75 50 40 8 1 3 22 35 40 75 60 90 70 75 50 80 7 1 4 22 35 40 50 60 90 70 75 75 80 6 1 5 22 35 40 50 60 90 70 75 75 80 5 1 6 22 35 40 50 60 70 90 75 75 80 4 1 7 22 35 40 50 60 70 75 90 75 80 3 1 8 22 35 40 50 60 70 75 75 90 80 2 1 9 22 35 40 50 60 70 75 75 80 90 1 1 TOTAL 45 9 1.2 How would you modify selection sort to arrange an array of values in decreasing sequence? By changing posMin to posMax and the if statement to read: if(table[next].compareTo(table[posMax]) > 0)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2.1 How many passes of bubble sort are needed to sort the following array of integers? How many comparisons are performed? How many exchanges? Show the array after each pass. 40 35 80 75 60 90 70 75 50 22 Pass Array After Pass Comparisons Exchanges 0 40 35 80 75 60 90 70 75 50 22 0 0 1 35 40 75 60 80 70 75 50 22 90 9 7 2 35 40 60 75 70 75 50 22 80 90 8 5 3 35 40 60 70 75 50 22 75 80 90 7 3 4 35 40 60 70 50 22 75 75 80 90 6 2 5 35 40 60 50 22 70 75 75 80 90 5 2 6 35 40 50 22 60 70 75 75 80 90 4 2 7 35 40 22 50 60 70 75 75 80 90 3 1 8 35 22 40 50 60 70 75 75 80 90 2 1 9 22 35 40 50 60 70 75 75 80 90 1 1 TOTAL 45 24 3.2 How would you modify bubble sort to arrange an array of values in decreasing sequence? Change the if statement to read: if (table[i].compareTo(table[i+1]) < 0) { 4.1 Sort the following array using insertion sort. How many passes are needed? How many comparisons are performed? How many exchanges? Show the array after each pass. 40 35 80 75 60 90 70 75 50 22 Pass Table After Pass Compares Exchanges 0 40 35 80 75 60 90 70 75 50 22 0 0 1 35 40 80 75 60 90 70 75 50 22 1 1 2 35 40 80 75 60 90 70 75 50 22 1 0 3 35 40 75 80 60 90 70 75 50 22 2 1 4 35 40 60 75 80 90 70 75 50 22 3 2 5 35 40 60 75 80 90 70 75 50 22 1 0 6 35 40 60 70 75 80 90 75 50 22 4 3 7 35 40 60 70 75 75 80 90 50 22 3 2 8 35 40 50 60 70 75 75 80 90 22 7 6 9
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 11/01/2011 for the course COMPSCI 225 at Boise State.

Page1 / 11

ch08 - 1.1 Indicate whether each of the following method...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online