File tree 1 file changed +58
-0
lines changed
1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Java Code to implement Selection Sorting Technique
2
+
3
+ import java .util .*;
4
+ public class SelectionSort {
5
+ public static void main (String args [])
6
+ {
7
+ Scanner sc =new Scanner (System .in );
8
+ System .out .println ("Input:\n " );
9
+ System .out .println ("Enter the size of an array : \n " );
10
+ int size =sc .nextInt ();
11
+ sc .nextLine ();
12
+ int arr []=new int [size ];
13
+ System .out .println ("Enter the elements of an array :\n " );
14
+ for (int index = 0 ; index < size ; index ++) {
15
+ arr [index ]=sc .nextInt ();
16
+ }
17
+ //Algorithms for Selection Sorting
18
+ for (int k =0 ;k <size -1 ;k ++)
19
+ {
20
+ int indx =k ;
21
+ for (int j =k +1 ;j <size ;j ++)
22
+ {
23
+ if (arr [indx ]>arr [j ])
24
+ {
25
+ indx =j ;
26
+ }
27
+ }
28
+ //Swapping of values when found minimum
29
+ int temp =arr [indx ];
30
+ arr [indx ]=arr [k ];
31
+ arr [k ]=temp ;
32
+ }
33
+ //Displaying Output
34
+ System .out .println ("Output:\n " );
35
+ for (int i =0 ;i <size ;i ++)
36
+ {
37
+ System .out .print (arr [i ]+" " );
38
+ }
39
+
40
+ }
41
+ }
42
+
43
+ /*
44
+ Input:
45
+
46
+ Enter the size of an array : 5
47
+
48
+ Enter the elements of an array :
49
+ 64 22 12 25 11
50
+
51
+ Output:
52
+
53
+ 11 12 22 25 64
54
+
55
+ Time Complexity: O(n^2)
56
+ Space Complexity: O(1)
57
+
58
+ */
You can’t perform that action at this time.
0 commit comments