Question

In: Computer Science

Correctly follow the described algorithm to complete the method    * Add operation counts -   ...

Correctly follow the described algorithm to complete the method
   * Add operation counts -
   * f(N) formula (show your work) -
   * O(N) reduction -
   */
   public static int[] algorithm1(int N)//f(N) = ; O(N) =
   {
       long opCount = 0;
       int[] arr = new int[N];
       /*
       * Use the following method to fill the array
       * For each position in the array, generate a random number between zero and N
       * - If N = 10, random numbers should be 0-9
       * Check if that random number is used in any previous position in the array
       * - If it is used anywhere else, generate a new number and try again
       * - If it is not used anywhere else, place it into the position and move forward
       */
       System.out.println("f(N) = [your answer]");
       System.out.println("O(N) = [your answer]");
       System.out.println("OpCount : "+opCount);
       return arr;
   }
   /*
  
   * Correctly follow the described algorithm to complete the method
   * Add operation counts
   * f(N) formula (show your work)
   * O(N) reduction
   */
   public static int[] algorithm2(int N)//f(N) = ; O(N) =
   {
       long opCount = 0;
       int[] arr = new int[N];
       boolean[] used = new boolean[N];
       /*
       * Use the following method to fill the array
       * For each position in the array, generate a random number between zero and N
       * - If N = 10, random numbers should be 0-9
       * Check if that used[random] is true
       * - If it is, generate a new number and try again
       * - If it is not, place it into the position, set used[random] = true, and move forward
       */
       System.out.println("f(N) = [your answer]");
       System.out.println("O(N) = [your answer]");
       System.out.println("OpCount : "+opCount);
       return arr;
   }
   /*
   * Correctly follow the described algorithm to complete the method
   * Add operation counts
   * f(N) formula (show your work)
   * O(N) reduction
   */
   public static int[] algorithm3(int N)//f(N) = ; O(N) =
   {
       long opCount = 0;
       int[] arr = new int[N];
       /*
       * Use the following method to fill the array
       * Fill the arr with zero to N-1 in order
       * Run a loop through each position
       * - For each position, swap that position and a randomly chosen position
       */
       System.out.println("f(N) = [your answer]");
       System.out.println("O(N) = [your answer]");
       System.out.println("OpCount : "+opCount);
       return arr;
   }
}

Solutions

Expert Solution

CODE

public static int[] algorithm1(int N)//f(N) = ; O(N) =

{

long opCount = 0;

int[] arr = new int[N];

/*

* Use the following method to fill the array

* For each position in the array, generate a random number between zero and N

* - If N = 10, random numbers should be 0-9

* Check if that random number is used in any previous position in the array

* - If it is used anywhere else, generate a new number and try again

* - If it is not used anywhere else, place it into the position and move forward

*/

for (int i=0; i<N; i++) {

int random = -1;

Random rand = new Random();

while (true) {

if (N == 10) {

random = rand.nextInt(10);

} else {

random = rand.nextInt(N);

}

opCount ++;

boolean found = false;

for (int j = 0; j<= i; j++) {

if (arr[j] == random) {

opCount ++;

found = true;

break;

}

}

if (!found) {

break;

}

}

}

System.out.println("f(N) = N^2 + C");

System.out.println("O(N) = O(N^2)");

System.out.println("OpCount : "+opCount);

return arr;

}

/*

* Correctly follow the described algorithm to complete the method

* Add operation counts

* f(N) formula (show your work)

* O(N) reduction

*/

public static int[] algorithm2(int N)//f(N) = ; O(N) =

{

long opCount = 0;

int[] arr = new int[N];

boolean[] used = new boolean[N];

/*

* Use the following method to fill the array

* For each position in the array, generate a random number between zero and N

* - If N = 10, random numbers should be 0-9

* Check if that used[random] is true

* - If it is, generate a new number and try again

* - If it is not, place it into the position, set used[random] = true, and move forward

*/

for (int i=0; i<N; i++) {

int random = -1;

Random rand = new Random();

while (true) {

if (N == 10) {

random = rand.nextInt(10);

} else {

random = rand.nextInt(N);

}

opCount ++;

if (!used[random]) {

break;

}

}

used[random] = true;

}

System.out.println("f(N) = N + c");

System.out.println("O(N) = O(N)");

System.out.println("OpCount : "+opCount);

return arr;

}

/*

* Correctly follow the described algorithm to complete the method

* Add operation counts

* f(N) formula (show your work)

* O(N) reduction

*/

public static int[] algorithm3(int N)//f(N) = ; O(N) =

{

long opCount = 0;

int[] arr = new int[N];

/*

* Use the following method to fill the array

* Fill the arr with zero to N-1 in order

* Run a loop through each position

* - For each position, swap that position and a randomly chosen position

*/

for (int i=0; i<N; i++) {

arr[i] = i;

opCount ++;

}

Random rand = new Random();

for (int i=0; i<N; i++) {

int random = rand.nextInt(N);

arr[i] = arr[random];

opCount ++;

}

System.out.println("f(N) = 2N + c");

System.out.println("O(N) = O(N)");

System.out.println("OpCount : "+opCount);

return arr;

}


Related Solutions

* Add operation counts -    * f(N) formula (show your work) -    * O(N)...
* Add operation counts -    * f(N) formula (show your work) -    * O(N) reduction -    */    public static long sum4(int N)//f(N) = ; O(N) =    {        long opCount = 0;        long sum = 0; // 1        for(int i = 0; i < N; i++)        {            for(int j = 0; j < i; j++)            {                sum++;   ...
   * Add operation counts    * f(N) formula (show your work)    * O(N) reduction...
   * Add operation counts    * f(N) formula (show your work)    * O(N) reduction    */    public static long sum3(int N)//f(N) = ; O(N) =    {        long opCount = 0;        long sum = 0;        for(int i = 0; i < N; i++)        {            for(int j = 0; j < N*N; j++)            {                sum++;            }   ...
Does this look correct? Add operation counts - 0.5pt    * f(N) formula (show your work)...
Does this look correct? Add operation counts - 0.5pt    * f(N) formula (show your work) - 0.5pt    * O(N) reduction - 0.5pt public static long sum1(int N)//f(N) = ; O(N) =    {        long opCount = 0;        long sum = 0; //1        opCount++;        opCount++;        opCount++;        for(int i = 0; i < N; i++) //2 + 2N        {            sum++; //N       ...
Add a new function that takes a phrase as an argument and counts each unique word...
Add a new function that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
Complete the following Customer class below. Also, add the equals method that would compare each field....
Complete the following Customer class below. Also, add the equals method that would compare each field. Please pay attention to the format of the equals method as it needs to have the same format as shown below. public class Customer {       private int id; //customer id       private String name; //customer's name       private int discount; //discount rate in percent             //construct a new customer       public Customer(int id, String name, int discount) {                    }      ...
Design a linear-time algorithm that verifies that the height information in an AVL tree is correctly...
Design a linear-time algorithm that verifies that the height information in an AVL tree is correctly maintained and that the balance property is in order. (C++)
Determine the element that is not correctly described A. Ti is a d-block element that has...
Determine the element that is not correctly described A. Ti is a d-block element that has two 3d electrons B. Ar is a second period monoatomic gas C. S is a p-block element with four 3p electrons D. The first ionization energy of Na is lower than the first ionization energy of Mg E. I is a group-7 element, and exists as a solid at room temperature
Q1. Write an algorithm to do the following operation on an array passed as parameter: If...
Q1. Write an algorithm to do the following operation on an array passed as parameter: If an element of the array is having more than one occurrence, then keep the first occurrence and remove all other occurrences of the element.
Complete the attached program by adding the following: a) add the Java codes to complete the...
Complete the attached program by adding the following: a) add the Java codes to complete the constructors for Student class b) add the Java code to complete the findClassification() method c) create an object of Student and print out its info in main() method of StudentInfo class. * * @author * @CS206 HM#2 * @Description: * */ public class StudentInfo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application...
Which of the following sentence correctly follow the rules of punctuation? A. The peach’s are ready...
Which of the following sentence correctly follow the rules of punctuation? A. The peach’s are ready for harvest. B. The peaches’ are ready for harvest. C. My son’s peaches are ready for harvest. D. My sons’s peaches's are ready for harvest Extending her arms over the left-field wall, the baseball plummeted toward the fan, and she caught it cleanly on the fly before displaying the home run ball for all to see. Which of the following phrases is misplaced in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT