{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

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)

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

View Full Document
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 22 35 40 50 60 70 75 75 80 90 9 9 TOTAL 31
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern