File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ package demos ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ public class SelectionSort {
6
+
7
+ public static void main (String [] args ) {
8
+ // TODO Auto-generated method stub
9
+ Scanner sc =new Scanner (System .in );//user input using scanner
10
+ System .out .println ("Enter the size of the array" );//taking size of the array from user
11
+ int size =sc .nextInt ();
12
+
13
+ int array []=new int [size ];//declaring array of n elements to be sorted
14
+ System .out .println ("Enter" + size + "elements to sort" );
15
+ for (int i =0 ;i <size ;i ++)//taking array elements from user and storing them in array
16
+ {
17
+ array [i ]=sc .nextInt ();
18
+ }
19
+
20
+ System .out .println ("Array before sort" );
21
+ for (int i =0 ;i <size ;i ++)//taking array elements from user and storing them in array
22
+ {
23
+ System .out .print (array [i ]+ " " );//Printing elements of array before sorting in a single line
24
+ }
25
+
26
+ SelectionSorting (array );
27
+ }
28
+
29
+ public static void SelectionSorting (int array []){
30
+
31
+ for (int i =0 ;i <array .length ;i ++)
32
+ {
33
+ for (int j =i +1 ;j <array .length ;j ++)
34
+ {
35
+ if (array [i ]>array [j ])//if i'th element of array is greater than j'th element swap the elements
36
+ {
37
+ int temp =array [i ];
38
+ array [i ]=array [j ];
39
+ array [j ]=temp ;
40
+ }
41
+ }
42
+ }
43
+
44
+ System .out .println ("Array after sorting" );
45
+ for (int i =0 ;i <array .length ;i ++)
46
+ {
47
+ System .out .print (array [i ]+ " " );//Printing elements of array after sorting in a single line
48
+ }
49
+
50
+ }
51
+ }
You can’t perform that action at this time.
0 commit comments