-
Notifications
You must be signed in to change notification settings - Fork 4
/
SelectionSort.java
52 lines (50 loc) · 1.11 KB
/
SelectionSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
class SelectionSort
{
void sort(int arr[])
{
int n=arr.length;
for(int i=0;i<n-1;i++)
{
int min=i;
for(int j=i+1;j<n;j++)
if(arr[j]<arr[min])
min=j;
int temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
}
void print(int arr[])
{
int n=arr.length;
for(int i=0;i<n;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String ar[])
{
SelectionSort ob=new SelectionSort();
Scanner scan=new Scanner(System.in);
System.out.println("Enter array size");
int n=scan.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=(int) (Math.random()*100);
}
//System.out.println("Before Sorting =>");
for(int c=0;c<n;c++)
{
//System.out.print(arr[c]+" ");
}
System.out.println();
System.out.println("After Sorting=>");
long startTime = System.nanoTime();
ob.sort(arr);
long endTime = System.nanoTime();
long totalTime = (endTime - startTime)/1000;
//ob.print(arr);
System.out.println("Total Execution Time "+totalTime);
}
}