File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* In the following Java program, we ask user to enter the array elements or number,
2
+ then we compare the array's element and start swapping with the variable temp.
3
+ Put the first element in the temp and the second element in the first,
4
+ and then temp in the second number and continue for the next match to sort the whole
5
+ array in ascending order. */
6
+
7
+ import java .util .Scanner ;
8
+
9
+ public class SelectionSortExample2
10
+ {
11
+ public static void main (String args [])
12
+ {
13
+ int size , i , j , temp ;
14
+ int arr [] = new int [50 ];
15
+ Scanner scan = new Scanner (System .in );
16
+
17
+ System .out .print ("Enter Array Size : " ); // size of array taken from user.
18
+ size = scan .nextInt ();
19
+
20
+ System .out .print ("Enter Array Elements : " );
21
+ for (i =0 ; i <size ; i ++)
22
+ {
23
+ arr [i ] = scan .nextInt (); // enter elements in array.
24
+ }
25
+
26
+ System .out .print ("Sorting Array using Selection Sort Technique..\n " );
27
+ for (i =0 ; i <size ; i ++)
28
+ {
29
+ for (j =i +1 ; j <size ; j ++)
30
+ {
31
+ if (arr [i ] > arr [j ])
32
+ {
33
+ temp = arr [i ];
34
+ arr [i ] = arr [j ];
35
+ arr [j ] = temp ;
36
+ }
37
+ }
38
+ }
39
+
40
+ System .out .print ("Now the Array after Sorting is :\n " );
41
+ for (i =0 ; i <size ; i ++)
42
+ {
43
+ System .out .print (arr [i ]+ " " ); // printing the sorted array.
44
+ }
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments