c17f01t2 - CSc 17 Test 2 Wednesday 28 November 2001...

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

View Full Document Right Arrow Icon
CSc 17 Test 2 Wednesday 28 November 2001 >>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<< 1. (10 pts) Using the algorithms discussed in class for building a balanced binary tree for sorting integers, draw a picture of the tree after each of the numbers listed in (a) are added to the tree in the order given. Repeat the exercise for the numbers in (b). (a) 16 -4 28 35 20 40 16 --> 16 --> 16 --> 16 --> 16 --> 16 / / \ / \ / \ / \ -4 -4 28 -4 28 -4 28 -4 28 \ / \ / \ 35 20 35 20 35 \ 40 ==> 28 / \ 16 35 / \ \ -4 20 40 (b) 16 -4 28 -2 8 12 16 --> 16 --> 16 --> 16 --> 16 --> -2 / / \ / \ / \ / \ -4 -4 28 -4 28 -4 28 -4 16 \ \ / \ -2 -2 8 28 \ 8 ==> -2 --> -2 ==> 8 / \ / \ / \ -4 16 -4 16 -2 16 / \ / \ / / \ 8 28 8 28 -4 12 28 \ 12 2. (25 pts) Write a function inList(). If we have the declarations int n,y,x[20]; then the call inList(y,x,n) should return the index of the first entry among the first n entries in x[] which equals y. If none of the first n entries in x[] equals y, then inList(y,x,n) should return -1. int inList(int y, int x[],int n) {int loc; loc=0; while(loc<n && x[loc]!=y) loc++; if(loc<n) return loc; return -1; } 3. (25 pts) The selection sort uses the following algorithm to sort a list of doubles. The largest entry is placed in the first location, the second largest entry is placed in the second location, etc.
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
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