Question

# Sort An Array using insertion Sort in Ascending Order.

```				```
Enter the size of array
5
Enter the Elements in Array
2
5
10
8
1
Sorted Array:
1 2 5 8 10
```
```

# Share code with your friends

Code

```				```
import java.util.Scanner;
public class InsertionSortAscendingOrder
{

public static void main()
{
Scanner sc=new Scanner(System.in);
int sizeOfArray=0,i=0,j=0,val=0;
int arr[];
System.out.println("Enter the size of array");
sizeOfArray=sc.nextInt();
System.out.println("Enter the Elements in Array");
arr=new int[sizeOfArray];
for(i=0;i< sizeOfArray;i++)
{
arr[i]=sc.nextInt();
}
for (i = 1; i < sizeOfArray; ++i)
{
val = arr[i];

for(j=i-1;j>=0;j--)
{
if(arr[j]>val)
{
arr[j+1]=arr[j];
}
//break out of loop
//if arr[j] is less than variable 'val' which has value of arr[i]
else
{
break;
}
}
arr[j+1]=val;

}
System.out.println("Sorted Array:");
for(i=0;i< sizeOfArray;i++)
{
System.out.print(arr[i]+" ");
}
}
}
```
```

Sale

50
Sale

150
Sale

200
Sale

50
Sale

50
Sale

50
Sale

50
Sale

50
Sale

50