5 Replies - 234 Views - Last Post: 01 February 2013 - 01:00 AM Rate Topic: -----

#1 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Bubble Sort Java? Using -1 after array.length

Posted 31 January 2013 - 02:04 PM

Hello

I am learning java and I am practising the bubble sort. I understand everything except.....

Could someone please explain the reason for using the -1 value after // numbers.length-1 in the 2nd for loop?


//more code here

for(int i=1; i<numbers.length; i++){

for(int j = 0; j < numbers.length-1; j++)

//more code here



Is This A Good Question/Topic? 0
  • +

Replies To: Bubble Sort Java? Using -1 after array.length

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Bubble Sort Java? Using -1 after array.length

Posted 31 January 2013 - 02:50 PM

I think the answer will be in the line that swaps values, two or 3 lines after the ones you posted. Limiting j prevents the swap algorithm attempting to swap the LAST element of the array with the NEXT element The NEXT element would be one more than the number of elements in the array.
Was This Post Helpful? 0
  • +
  • -

#3 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Re: Bubble Sort Java? Using -1 after array.length

Posted 31 January 2013 - 02:55 PM

Thanks Greg

So it prevents an out of bounds exception

i get it now.

Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#4 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Bubble Sort Java? Using -1 after array.length

Posted 31 January 2013 - 07:42 PM

You can fully avoid an Exception by try/catch and do nothing on catch

Eg:

try{


//The code that might cause ArrayOutofBounds Exception


]
catch(Exception e){
//blank
}


However, it's not recommended
Was This Post Helpful? 2
  • +
  • -

#5 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Bubble Sort Java? Using -1 after array.length

Posted 31 January 2013 - 07:50 PM

Sample code is provided below, for finding Primes


 public class Erast{
	 public static void main(String args[]){
		 int n=10000002;int pup=1000;
		 int c[]=new int[n];
		 int p[]=new int[n];int k=1;
		 for(int i=1;i<n;i++)
		 {
			p[i]=0; 
		c[i]=i;
		if(i%2==0)c[i]=0;
		 }
		p[1]=2;int m=1;int x=1;
	System.out.println("1 th prime is :  "+p[1]); 
		
	
			
			
	do{		
		for(int j1=m;j1<n;j1++){
			  x=j1;
		


 //BEGINS HERE

   try {
			if (x * p[k] < n)
				c[j1 * p[k]] = 0;
		} catch (Exception e) {
		//	System.err.println(e);
			// TODO: handle exception
		}

// ENDS HERE

	
				 
			}
	 			k++;
			for(int j1=p[k-1]+1;j1<n;j1++){ 
				
			if(c[j1]!=0){
				p[k]=c[j1];  m=j1; System.out.println(k+" th prime is :  "+p[k]);
				break;
					}
		}
	}while(p[k-1]<1000001);
	
	
	//while(m<1000);
		
	int kk = p[k-1];	
		 
//for(int j2=1;j2<kk;j2++)System.out.println(p[j2]);
		 
//	 for(int i=1;i<k+1;i++)	 	 System.out.println(p[i]);
	 
	 
 }}
	 
	 

		 

This post has been edited by burakaltr: 31 January 2013 - 07:52 PM

Was This Post Helpful? 1
  • +
  • -

#6 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Re: Bubble Sort Java? Using -1 after array.length

Posted 01 February 2013 - 01:00 AM

Thank you Burakaltr

I haven't studied try/catch yet but that is really good to know for the future.

Thank you for the prime code also
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1