-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsecond smallest element
More file actions
25 lines (25 loc) · 827 Bytes
/
second smallest element
File metadata and controls
25 lines (25 loc) · 827 Bytes
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
import java.util.*;
public class Secondsmallest{
public static void main(String args []){
Scanner s = new Scanner(System.in);
int n,temp;
System.out.print("enter the number of elements of array :");
n = s.nextInt();
int a[] = new int[n];
for(int i=0; i<n ;i++){
System.out.print("enter the"+(i+1)+"th element of the array : ");
a[i]=s.nextInt();
}
for (int i = 0; i < n - 1; i++){
for (int j = 0; j < n - i - 1; j++){
if (a[j] > a[j + 1]) {
// swap arr[j+1] and arr[j]
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.print("second smallest element is :"+a[1]);
}
}