forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort.java
58 lines (49 loc) · 1.3 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
53
54
55
56
57
58
// Java Code to implement Selection Sorting Technique
import java.util.*;
public class SelectionSort{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Input:\n");
System.out.println("Enter the size of an array : \n");
int size=sc.nextInt();
sc.nextLine();
int arr[]=new int[size];
System.out.println("Enter the elements of an array :\n");
for(int index = 0; index < size; index++) {
arr[index]=sc.nextInt();
}
//Algorithms for Selection Sorting
for(int k=0;k<size-1;k++)
{
int indx=k;
for(int j=k+1;j<size;j++)
{
if(arr[indx]>arr[j])
{
indx=j;
}
}
//Swapping of values when found minimum
int temp=arr[indx];
arr[indx]=arr[k];
arr[k]=temp;
}
//Displaying Output
System.out.println("Output:\n");
for(int i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
}
}
/*
Input:
Enter the size of an array : 5
Enter the elements of an array :
64 22 12 25 11
Output:
11 12 22 25 64
Time Complexity: O(n^2)
Space Complexity: O(1)
*/