Array - bubble sort

import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String args[]) {

	int N = sc.nextInt();
	//array declaration
	int [] arr = new int[N];
	takeInput(arr);
	bubbleSort(arr);
	display(arr);

}
public static void takeInput(int [] arr)
{
	for(int i=0;i<=arr.length-1;i++)
	{
		arr[i] = sc.nextInt();
	}
}
public static void bubbleSort(int [] arr)
{
	int counter = 0;
	while(counter<arr.length-1)
	{
		for(int j=0; j<=arr.length-1-counter; j++)
		{
			if(arr[j]>arr[j+1])
			{
				int temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
		}
		counter++;
	}
}

public static void display(int [] arr)
{
	for(int i=0;i<=arr.length-1;i++)
	{
		System.out.println(arr[i]);
	}
}

}
resolve asap

@Naman_Gupta,
https://ide.codingblocks.com/s/252323 corrected code.

Do:

			for (int j = 0; j < arr.length - 1 - counter; j++) {

Instead of:

			for (int j = 0; j <= arr.length - 1 - counter; j++) {

The ‘<=’ was giving you a out of bounds exception error.

@Naman_Gupta,
Because when counter=0 and j=arr.length()-1, j+1 will be arr.length().

Hence the out of bounds here:

				if (arr[j] > arr[j + 1]) {

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.