File tree Expand file tree Collapse file tree 2 files changed +119
-0
lines changed Expand file tree Collapse file tree 2 files changed +119
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * PROGRAM : To find the 3rd largest and 2nd smallest element in an array. Also print the input array.
3
+ * FILE : : LarSmall.java
4
+ * CREATED BY : Santosh Hembram
5
+ * DATE : 12-10-20
6
+ */
7
+ import java .util .*;
8
+ class LarSmall {
9
+
10
+ public static int [] largeSmall (int arr []){
11
+
12
+ int temp ;
13
+ for (int i =0 ; i <arr .length ; i ++ ) {
14
+
15
+ for (int j =i +1 ; j <arr .length ; j ++ ) {
16
+
17
+ if (arr [i ] > arr [j ]) {
18
+
19
+ temp = arr [j ];
20
+ arr [j ] = arr [i ];
21
+ arr [i ] = temp ;
22
+ }
23
+ }
24
+ }
25
+ return arr ;
26
+ }
27
+
28
+ public static void main (String [] args ) {
29
+ Scanner sc = new Scanner (System .in );
30
+ System .out .print ("Enter the size of the array: " );
31
+ int s = sc .nextInt ();
32
+ if (s <3 ) {
33
+
34
+ while (true ) {
35
+
36
+ System .out .println ("------Please enter the size of array more than 2--------" );
37
+ System .out .print ("Enter the size of the array: " );
38
+ s = sc .nextInt ();
39
+ }
40
+ }
41
+ int arr [] = new int [s ];
42
+ System .out .println ("Enter the elements of the array: " );
43
+ for (int i =0 ; i <s ; i ++)
44
+ {
45
+ arr [i ]=sc .nextInt ();
46
+ }
47
+ int lar [] = largeSmall (arr );
48
+ int thirdLarge = lar [lar .length - 3 ];
49
+ int secondSmall = lar [1 ];
50
+
51
+ System .out .println ("The third largest element of the array = " +thirdLarge );
52
+ System .out .println ("The second smallest element of the array = " +secondSmall );
53
+ }
54
+
55
+ }
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * PROGRAM : To sort an array of integer values in default order and print the array before and after sorting operation.
3
+ * FILE : SortArray.java
4
+ * CREATED BY : Santosh Hembram
5
+ * DATE : 12-10-20
6
+ */
7
+ import java .util .*;
8
+ class SortArray {
9
+
10
+ public static int [] sortingOperation (int arr []) {
11
+
12
+ int temp ;
13
+ for (int i =0 ; i <arr .length ; i ++) {
14
+ for (int j =i +1 ; j <arr .length ; j ++) {
15
+ if (arr [i ] > arr [j ]) {
16
+
17
+ temp = arr [j ];
18
+ arr [j ] = arr [i ];
19
+ arr [i ] = temp ;
20
+
21
+ }
22
+ }
23
+ }
24
+ return arr ;
25
+ }
26
+
27
+ public static void main (String [] args ) {
28
+
29
+ Scanner sc = new Scanner (System .in );
30
+ System .out .print ("Enter the size of the array: " );
31
+ int s = sc .nextInt ();
32
+
33
+ int arr [] = new int [s ];
34
+ System .out .println ("Enter the elements of the array: " );
35
+ for (int i =0 ; i <s ; i ++)
36
+ {
37
+ arr [i ]=sc .nextInt ();
38
+ }
39
+ System .out .println ("Elements of the array before Sorting : " );
40
+ for (int i : arr ) {
41
+ System .out .print (i +" " );
42
+
43
+ }
44
+ int sort [] = sortingOperation (arr );
45
+ System .out .println ();
46
+ System .out .println ("Elements of the array after Sorting: " );
47
+ for (int i : sort ) {
48
+ System .out .print (i +" " );
49
+
50
+ }
51
+
52
+ }
53
+
54
+ }
You can’t perform that action at this time.
0 commit comments