Question

JAVA What is the complexity of this algorithm? Assign each student in the class a number...

JAVA

  1. What is the complexity of this algorithm?

Assign each student in the class a number from 1 to n, where n is the number of students. Then ask each of the odd-numbered students whether he or she is left-handed.

a. O(1)
b. O(n)
c. O(n ^ 2)
d. O(log n)

  1. What is the complexity of this algorithm?

In a very difficult CS class, half the n students who originally signed up drop the course after the first quiz. After each successive quiz, half the remaining students drop. This continues until there is only one student remaining in the course.

a. O(n)
b. O(n ^ 2)
c. O(log n)
d. O(n log n)

  1. Which statement is true?

a. traversing a linked list and traversing an array list have the same complexity
b. getting an item from an arbitrary index in an array list is generally more expensive than c. getting an item from an arbitrary index in a linked list
d. clearing (dropping all elements) from an array list is O(n)
e. deleting the first item from a linked list is O(n)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Assigning each student in class a number 1 to n will take O(n) time. Since there will be n/2 odd numbered students I.e. O(n) whether s/he is left/right handed will take O(n) time. Hence total time complexity is O(n) + O(n) + O(n) = O(n).

Hence option b is correct .

2. Since after each successive quiz, half of the students drop the course. Hence if there are k quizzes happen, then number of students remain will be n/2k. Since at last only 1 student remains,

=> n/2k = 1

=> 2k = n

=> k = log2 n = O(log n)

Hence option c is correct.

3. Here all statements except statement d are false.

Option a, b, c are false since traversing an element of array is less expensive then linked list . Option e is false since accessing first element at linked list will fake O(1) time.

Option. d is correct since dropping 1 element will take O(1) time and hence dropping n elements one by one will take n*O(1) = O(n).

Please comment for any clarification .

Add a comment
Know the answer?
Add Answer to:
JAVA What is the complexity of this algorithm? Assign each student in the class a number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

  • Show that the time complexity for the number of assignments of records for the Mergesort algorithm...

    Show that the time complexity for the number of assignments of records for the Mergesort algorithm (Algorithms 2.2 and 2.4) is approximated by T (n) = 2nlgn. by Mingfu LI, CGUEE Algorithms Algorithms 2.2 : Mergesort O Problem Sort n keys in nondecreasing sequence. Inputs positive integer n, array of keys S index from 1 to n Output the array 5 containing the keys in nondecreasing sequence. void mergesort (int n, keytype S[]) (1 <u)и } keytype UI..h), ИI.m); copy...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Show that the time complexity for the number of assignments of records for the Mergesort algorithm...

    Show that the time complexity for the number of assignments of records for the Mergesort algorithm (Algorithms 2.2 and 2.4) is approximated by T (n) = 2nlgn. by Mingfu LI, CGUEE Algorithms Algorithms 2.2 : Mergesort O Problem Sort n keys in nondecreasing sequence. Inputs positive integer n, array of keys S index from 1 to n Output the array 5 containing the keys in nondecreasing sequence. void mergesort (int n, keytype S[]) (1 <u)и } keytype UI..h), ИI.m); copy...

  • Determine the output of each algorithm below the number of assignment operations in each (show work)...

    Determine the output of each algorithm below the number of assignment operations in each (show work) the number of print operations in each (show work) the complexity of each algorithm in terms of Big O notation (show work) 2. Let n be a given positive integer, and let myList be a three-dimensional array with capacity n for each dimension. for each index i from 1 to n do { for each index j from 1 to n/2 do { for...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Match each problem to the time complexity class it likely belongs to: 1. O(1): Constant 2....

    Match each problem to the time complexity class it likely belongs to: 1. O(1): Constant 2. O(n): Linear 3. O(n!): Factorial 4. O(logn): Logarithmic 5.O(n2): Quadratic 6. O(n3): Cubic OPTIONS: a. Find an element in an unsorted array b. Shortest path between two nodes in a graph c. Matrix multiplication d. Generate permutations of a string e. Add an element to the front of a linked list f. Find an element in a binary search tree

  • Interfaces 1. What is inside an interface definition? What does a class do to an interface...

    Interfaces 1. What is inside an interface definition? What does a class do to an interface and what keyword is involved? How does a class do this to an interface (what should we find in the class)? Can an interface have a generic parameter? How do you instantiate an interface as an object? What methods can’t you use on an interface type? Abstract Data Types 2. What does an ADT define? Does an ADT specify programming language and/or data structures...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int...

    Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int j) that exchanges the values stored at indices i and j in the array a. You do not need to worry about cases where either i or j is an invalid index. Give the best estimate you can for its time complexity (b) In an ordered array of n items, how can we determine whether or not an item belongs to the list using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT