import java.util.Random;
import java.util.*;
class ss
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
Random randomGenerator = new Random();
System.out.println("Enter value of n");
int n = sc.nextInt();
int [] a = new int[n] ;
for (int i = 0; i < n; i++)
{
a[i] = randomGenerator.nextInt(100);
System.out.println("Number :"+a[i]);
}
long startTime=System.nanoTime();
int i,j,temp;
// selection sort
for(i=0;i<=n-2;i++)
{
int min=i;
for(j=i+1;j<=n-1;j++)
{
if (a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
long endTime=System.nanoTime();
long elapseTime=(endTime-startTime);
System.out.println("Sorted Numbers :");
for (i = 0; i < n; i++)
{
System.out.println("Number :"+a[i]);
}
System.out.println("Time taken to sort array elements is:"+elapseTime+" nano seconds");
System.out.println("Time taken to sort array elements is:"+(float)elapseTime/1000000000+"seconds");
}
}
No comments:
Post a Comment