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

java binary heap operation counts /* * Add operation counts to all methods - 4pts *...
java binary heap operation counts /* * Add operation counts to all methods - 4pts * No other methods/variables should be added/modified */ public class A6BH <E extends Comparable<? super E>> {    private int defaultSize = 4;    private int count = 0;    private E[] heap;    private int opCount = 0;       public int getOpCount()    {        return opCount;    }    public void resetOpCount()    {        opCount = 0;    }...
* 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)...
/*    * Add operation counts    * f(N) formula (show your work)    * O(N) reduction -    */    public static long sum2(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; 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       ...
1. The equality operation on naturals is correctly computed by the following pseudo-Java method: boolean equals...
1. The equality operation on naturals is correctly computed by the following pseudo-Java method: boolean equals (natural x, natural y) { if (zero(x)) return zero(y); return equals(pred(x), pred(y)); } 2. Suppose we define a function f from naturals to naturals as follows. We define f(0) to be 0, and for any natural x we define f(Sx) to be 1. Then this function satisfies the equation f(x · y) = f(x) · f(y), for any naturals x and y. 3. Suppose...
Add the operation Insert to the linkedListClass. An Insert operation inserts a new item after a...
Add the operation Insert to the linkedListClass. An Insert operation inserts a new item after a given key in the linked list. The method headline can be void Insert(int item, int key), where the first parameter is the new item, and the second parameter is the key of the item before the new item.
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
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) {                    }      ...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT