{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw3solns - CMPS 10 Homework Assignment 3 Problems Chapter...

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

View Full Document Right Arrow Icon
CMPS 10 - Homework Assignment 3 Problems: Chapter 3 (p.120): 1ab, 6, 8abc, 9, 10, 11, 13, 14, 18, 22, 27, 28ab 1.) a.) Use Gauss’s approach to find the sum 2 + 4 + 6 + … + 100 b.) Use Gauss’s approach to find the formula for the sum of the even numbers from 2 to 2 n . 2 + 4 + 6 + … + 2 n Your formula will be an expression involving n . Solution: a.) Group them into pairs of the form: 100 + 2 = 102, 98 + 4 = 102, etc. There are (50 / 2) = 25 of these pairs, so the sum equals 25 * 102 = 2550. b.) Similar to the last method, calculate 2 n + 2 = 2 n + 2, (2 n – 2) + 4 = 2 n + 2, etc. There are ( n / 2) elements, so the sum will be ( n / 2) * (2 n + 2) = n ( n + 1) 6.) Perform a selection sort on the list 7, 4, 2, 9, 6. Show the list after each exchange that has an effect on the list ordering. Solution: Initial List: 7, 4, 2, 9, 6 Exchange 6 and 9: 7, 4, 2, 6 , 9 Exchange 7 and 6: 6 , 4, 2, 7 , 9 Exchange 6 and 2: 2 , 4, 6 , 7, 9 List sorted: 2, 4, 6, 7, 9 8.) For each of the following lists, perform a bubble sort, and show the list after each exchange. Compare the number of exchanges done here, and in the Practice Problem at the end of Section 3.3.3. a.) 4, 8, 2, 6 b.) 12, 3, 6, 8, 2, 5, 7 c.) D, B, G, F, A, C, E Solution : a.) Initial List: 4, 8, 2, 6
Image of page 1

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

View Full Document Right Arrow Icon
Exchange 8 and 2: 4, 2 , 8 , 6 Exchange 8 and 6: 4, 2, 6 , 8 Exchange 4 and 2: 2 , 4 , 6, 8 List sorted: 2, 4, 6, 8 b.) Initial List: 12, 3, 6, 8, 2, 5, 7 Exchange 12 and 3: 3 , 12 , 6, 8, 2, 5, 7 Exchange 12 and 6: 3, 6 , 12 , 8, 2, 5, 7 Exchange 12 and 8: 3, 6, 8 , 12 , 2, 5, 7 Exchange 12 and 2: 3, 6, 8, 2 , 12 , 5, 7 Exchange 12 and 5: 3, 6, 8, 2, 5 , 12 , 7 Exchange 12 and 7: 3, 6, 8, 2, 5, 7 , 12 Exchange 8 and 2: 3, 6, 2 , 8 , 5, 7, 12 Exchange 8 and 5: 3, 6, 2, 5 , 8 , 7, 12 Exchange 8 and 7: 3, 6, 2, 5, 7 , 8 , 12 Exchange 6 and 2: 3, 2 , 6 , 5, 7, 8, 12 Exchange 6 and 5: 3, 2, 5 , 6 , 7, 8, 12 Exchange 3 and 2: 2 , 3 , 5, 6, 7, 8, 12 List sorted: 2, 3, 5, 6, 7, 8, 12 c.) Initial List: D, B, G, F, A, C, E Exchange D and B: B , D , G, F, A, C, E Exchange G and F: B, D, F , G , A, C, E Exchange G and A: B, D, F, A , G , C, E Exchange G and C: B, D, F, A, C , G , E Exchange G and E: B, D, F, A, C, E , G Exchange F and A: B, D, A , F
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern