- class selection
-
{
-
public static void sort(int arr[])
-
{
-
for(int i= arr.length;i>0;i++)
-
{
-
int m=0;
-
for(int j=1;j<=i;j++)
-
{
-
if(arr[j]>arr[m])
-
{
-
m=j;
-
}
-
}
-
int temp= arr[i];
-
arr[i]= arr[m];
-
arr[m]= temp;
-
}
-
System.out.println("Array After Sort : ");
-
System.out.print("{");
-
for(int i=0;i<arr.length;i++)
-
System.out.println(arr[i]+",");
-
System.out.print("}");
-
}
-
public static void main(String args[])
-
{
-
int arr[] = {11,55,13,5,3};
-
System.out.print("Array before sort : ");
-
System.out.print("{");
-
for(int i=0;i<arr.length;i++)
-
System.out.print(arr[i]+",");
-
System.out.println("}");
-
sort(arr);
-
}
-
}
Output :
Array before sort : {11,55,13,5,3,}
Excepion in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at selection.sort(selection.java:10)
at selection.main(selection.java:33)