Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Norm

Thread: how to do write this method with more efficiency ?

  1. #1
    romavolman is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default how to do write this method with more efficiency ?

    The program below accept two array with hole pozitive nums, with same size N, and fill empty array "c" with nums of "a" that do not exist in "b" and return the biggest num of "a" that do not exist in "b".
    qustion: how to do write this method with more efficiency ?

    Java Code:
    public int f(int[]a, int[]b, int[] c)
    	{
    		int N = a.length;
    		int j, max = 0, g = 0, t = 0;
    		
    		for(int i=0; i < N; i++)
    		{
    			for(j=0; j<N; j++)
    				if(b[j] == a[i]) // if find the num in "b" go to the next num in "a"
    				   break;  
    					
    			if(j == N)
    			{
    				c[t] =a[i]   // fill empty array "c" with nums of "a" that do not exist in "b"   
                                                            if(g ==0 || c[t] > max)
    				{
    					max = c[t]; // remmber the max num in "max"
    					g = 1; 
    					
    				}
    				t++;
    					
    			}	
    			
    					
    		}
    		return max; // max of "a" that you do not have in "b"

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default Re: how to do write this method with more efficiency ?

    DarrylBurke likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: how to do write this method with more efficiency ?

    Quote Originally Posted by Norm View Post
    Not very efficient, that ;)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: how to do write this method with more efficiency ?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    rfq
    rfq is offline Member
    Join Date
    Mar 2012
    Location
    Novosibirsk, Russia
    Posts
    15
    Rep Power
    0

    Default Re: how to do write this method with more efficiency ?

    create HashSet<Integer>bs containing all values from the array b[], and check existence with bs.contains(a[i]).

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    6

    Default Re: how to do write this method with more efficiency ?

    First you need to know how you will know when you have answered your question. You need to characterize how your algorithm currently performs in order to know how you might improve it. Here's a good place to start: A Beginner’s Guide to Big O Notation Rob Bell
    Get in the habit of using standard Java naming conventions!

  7. #7
    aljassi is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: how to do write this method with more efficiency ?

    i dont know if better than yours . i m a newbie too

    try this

    Java Code:
    	private double returnBiggestElement( double[]a,double[]b, double[]c){
    		double max=0;
    		for(int i=0;i<a.length;i++){
    			for(int j=0;j<b.length;j++){
    				if(a[i]!=b[j]){
    					c[i]=a[i];
    					if(c[i]>max){
    						max=c[i];
    					}
    				}
    			}
    		}
    		
    		return max;
    	}

  8. #8
    aljassi is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: how to do write this method with more efficiency ?

    and efficiently
    Java Code:
    private double returnBiggestElement( double[]a,double[]b){
    		double max=0;
    		for(int i=0;i<a.length;i++){
    			for(int j=0;j<b.length;j++){
    				if(a[i]!=b[j]&& a[i]>max){
    						max=a[i];
    				}
    			}
    		}
    		
    		return max;
    	}

Similar Threads

  1. BufferedWriter method: write
    By toya in forum New To Java
    Replies: 6
    Last Post: 05-01-2012, 10:22 AM
  2. Writing a method to write a method?
    By balla in forum New To Java
    Replies: 5
    Last Post: 04-01-2011, 12:05 PM
  3. how to write method syntax ?
    By Bader in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 05:58 PM
  4. Trying to write toString method
    By curious in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 02:43 AM
  5. method efficiency
    By TheWave in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 04:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •