# Bubble Sort Java? Using -1 after array.length

Page 1 of 1

## 5 Replies - 381 Views - Last Post: 01 February 2013 - 01:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310580&amp;s=4a860361032652d9a819d5cd1347e96b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kevinb4940

Reputation: 3
• 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

• D.I.C Lover

Reputation: 2216
• Posts: 5,241
• 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.

### #3 kevinb4940

Reputation: 3
• 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.

### #4 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• 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

### #5 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• 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

### #6 kevinb4940

Reputation: 3
• 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