Return to Snippet

Revision: 34907
at November 19, 2010 07:27 by adkatrit


Updated Code
public static void main(String[] args) {
    	String[] arr = {"0","1","2","3"};      
    	  int len = arr.length;
    	    	for(int p= 0;p<len;p++){
    	    		for(int i=len;i-->p+1;){
    	    			System.out.print(arr[p]+arr[i]+" ");
    	    		}
    	    	}
    }

Revision: 34906
at October 30, 2010 02:21 by adkatrit


Initial Code
public static void main(String[] args) {
    	String[] arr = {"0","1","2","3"};      
    	  int len = arr.length;
    	    	int thresh = 1;
    	    	for(int p= 0;p<len;p++,thresh++){
    	    		//System.out.println(arr[p]);
    	    		for(int i=len;i-->thresh;){
    	    			System.out.print(arr[p]+arr[i]+" ");
    	    		}
    	    		System.out.println(p);
    	    		
    	    	}
    	
    }

Initial URL

                                

Initial Description
compare individual array elements with one another, without doing the same comparison twice (3vs2 then 2vs3).  this reduces the number of iterations by roughly half.  it's still O(n^2), an 1000 member array won't iterate a 1000X1000 times, it will be (1000X999)/2.  efficient!!
an array of 0,1,2,3  will print out the following:
01,02,03,,12,13,2,3
ENJOY MY MATHS

Initial Title
Matching elements in an array

Initial Tags

                                

Initial Language
Java