-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSelectionSortADT.java
52 lines (42 loc) · 1018 Bytes
/
SelectionSortADT.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.Scanner;
public class SelectionSortADT
{
public static void main(String args[])
{
int a[]; // array reference
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size");
int n = sc.nextInt();
a = new int[n]; // dynamic memory allocation in java for array
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
System.out.println("\n Before sorting...");
display(a,n);
selectionSort(a,n); // calling sorting function
System.out.println("\n After sorting...");
display(a,n);
}
public static void selectionSort(int a[], int n)
{
int i,j,temp,min;
for(i=0;i<n;i++)
{
min = i;
for(j=i+1;j<n;j++) // inner loop for finding minimum element
{
if(a[min] > a[j])
min = j;
}
if(min!=i) // to perform swapping
{
temp = a[min]; a[min] = a[i]; a[i] = temp;
}
}
}
public static void display(int a[],int n)
{
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}