Question

Java For each of the algorithms unique1 and unique2, which solve the element uniqueness problem, perform...

Java

For each of the algorithms unique1 and unique2, which solve the element uniqueness problem, perform an experimental analysis to determine the largest value of n such that the given algorithm runs in one minute or less. Hint: Do a type of “binary search” to determine the maximum effective value of n for each algorithm.

public static boolean unique1(int[] data) {
int n = data.length;
for (int j=0; j < n-1; j++)
for (int k=j+1; k < n; k++)
if (data[j] == data[k])
return false; // found duplicate pair
return true; // if we reach this, elements are unique
}

/** Returns true if there are no duplicate elements in the array. */
public static boolean unique2(int[] data) {
int n = data.length;
int[] temp = Arrays.copyOf(data, n); // make copy of data
Arrays.sort(temp); // and sort the copy
for (int j=0; j < n-1; j++)
if (temp[j] == temp[j+1]) // check neighboring entries
return false; // found duplicate pair
return true; // if we reach this, elements are unique
}

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

Java code:

import java.util.concurrent.TimeUnit;
import java.lang.Math;
import java.util.Arrays;

public class timeSearch
{
public static boolean unique1(int [] data,int n)
{
   //int n=data.length;
   for(int j=0;j<n-1;j++)
   {
       for(int k=j+1;k<n;k++)
       {
           if(data[j]==data[k])
               return(false);
       }
   }
   return(true);
}

public static boolean unique2(int [] data,int n)
{
   int []temp=Arrays.copyOf(data,n);
   Arrays.sort(temp);
   for(int j=0;j<n-1;j++)
   {
       if(temp[j]==temp[j+1])
           return(false);
   }
   return(true);
}

public static void main(String args[])
{
   int sz=(int)Math.pow(2,20);
   int data[]= new int[sz];
   int i;

   for(i=0;i<sz;i++)   //fill array
   {
       data[i]=sz-i+1;
   }

   long startTime,endTime,totalTime;

   i=1;
   totalTime=0;
   while(totalTime/Math.pow(10,6)<1)
   {
       startTime = System.nanoTime();
       unique1(data,i);
       endTime = System.nanoTime();
       totalTime = endTime - startTime;
       i++;
   }

   System.out.println("Value of n for which unique1 takes 10^(-6)second(1 microsecond):"+i);

   totalTime=0;
   i=1;
   while(totalTime/Math.pow(10,6)<1)
   {
       startTime = System.nanoTime();
       unique2(data,i);
       endTime = System.nanoTime();
       totalTime = endTime - startTime;
   i++;
   }
   System.out.println("Value of n for which unique1 takes 10^(-6)second(1 microsecond):"+i);
}
}

output:

Add a comment
Know the answer?
Add Answer to:
Java For each of the algorithms unique1 and unique2, which solve the element uniqueness problem, perform...
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
  • Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of...

    Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of N-squared. I.E. No nested for loops in rowIsLatin and colIsLatin. Only nested loop allowed in goodSubsquare. public class Sudoku {               public String[][] makeSudoku(String s) {              int SIZE = 9;              int k = 0;              String[][] x = new String[SIZE][SIZE];              for (int i = 0; i < SIZE; i++) {                     for (int j = 0; j < SIZE; j++)...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

  • // I need help with the following questions. Please use java programming ECLIPSE language to solve...

    // I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT. I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED" I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE. import java.util.ArrayList; public class CustomArrayList { //instance variables public int[] data; //data.length gives the capacity public int nItems; //nItems gives items currently in the...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • In the class GraphAlgorithm, insert java code for the method prim public class MyGraph { public...

    In the class GraphAlgorithm, insert java code for the method prim public class MyGraph { public static final int MAXSIZE = 100; public static final double BIGM = 10000000; public MyGraph(int size) { n = size; nodeStart = new Edge[MAXSIZE]; for (int i=0; i<n; i++) { nodeStart[i] = null; } } public int getSize() { return n; } public double getCost(int i, int j) { double value = BIGM; Edge e = nodeStart[i]; while (e !=null) { if (e.dest ==...

  • Can you help with the merge method? This method should create and return an ArrayBagcontaining one...

    Can you help with the merge method? This method should create and return an ArrayBagcontaining one occurrence of any item that is found in either the called object or the parameter other. For full credit, the resulting bag should not include any duplicates. Give the new ArrayBag a maximum size that is the sum of the two bag’s maximum sizes. import java.util.*; /** * An implementation of a bag data structure using an array. */ public class ArrayBag { /**...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

  • Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform...

    Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform a linear search of the array. The method should receive the search key and starting index as arguments. If the search key is found, return its index in the array; otherwise, return –1. Each call to the recursive method should check one index in the array. Figure 19.2 import java.security.SecureRandom; import java.util.Arrays; import java.util.Scanner; public class LinearSearchTest{ public static int linearSearch(int data[], int searchKey)...

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