From 6d5befacf89445f4955188fc33fcfe4142ca5aac Mon Sep 17 00:00:00 2001 From: Aniruddho Mitra <68218897+aniruddho-mitra@users.noreply.github.com> Date: Wed, 5 Oct 2022 11:33:15 +0530 Subject: [PATCH] Create SecondSmallestElement.java --- Arrays/SecondSmallestElement.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Arrays/SecondSmallestElement.java diff --git a/Arrays/SecondSmallestElement.java b/Arrays/SecondSmallestElement.java new file mode 100644 index 0000000..ab9651e --- /dev/null +++ b/Arrays/SecondSmallestElement.java @@ -0,0 +1,23 @@ +public class SecondSmallestInArrayExample{ +public static int getSecondSmallest(int[] a, int total){ +int temp; +for (int i = 0; i < total; i++) + { + for (int j = i + 1; j < total; j++) + { + if (a[i] > a[j]) + { + temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + } + } + return a[1];//2nd element because index starts from 0 +} +public static void main(String args[]){ +int a[]={1,2,5,6,3,2}; +int b[]={44,66,99,77,33,22,55}; +System.out.println("Second smallest: "+getSecondSmallest(a,6)); +System.out.println("Second smallest: "+getSecondSmallest(b,7)); +}}