Monday, July 14, 2014

Array Problem2: Selection sort

public class Sort {

public static void main(String[] args) {
// TODO Auto-generated method stub
int A[]={2,4,6,8,2,4,1,10,6,12,14,10,6,8,2,2};
//The below code will sort the array into ascending order
for(int i=0;i<A.length;i++)
for(int j=i+1;j<A.length;j++)
if(A[i]>A[j])
{
int temp=A[i];
A[i]=A[j];
A[j]=temp;
}
for(int i=0;i<A.length;i++)
System.out.println(A[i]);
}

}

Output- 1 2 2 2 2 4 4 6 6 6 8 8 10 10 12 14

Complexity- O(n^2)

No comments:

Post a Comment