Java 查找数组中第二小的整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30427857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the second smallest integer in array
提问by Majd Khoury
We are required in our assignment to find the second smallest integer in one array recursively. However, for the sake of understanding the subject more, I want to do it iteratively first (with the help of this website) and recursively on my own.
我们需要在我们的任务中递归地找到一个数组中第二小的整数。但是,为了更深入地理解这个主题,我想先迭代(在本网站的帮助下),然后自己递归进行。
Unfortunately, doing it iteratively is quite confusing. I understand that the solution is simple but i can't wrap my head around it.
不幸的是,以迭代方式执行此操作非常令人困惑。我知道解决方案很简单,但我无法理解它。
Below is my code, so far:
以下是我的代码,到目前为止:
public static void main(String[] args)
{
int[] elements = {0 , 2 , 10 , 3, -3 };
int smallest = 0;
int secondSmallest = 0;
for (int i = 0; i < elements.length; i++)
{
for (int j = 0; j < elements.length; j++)
{
if (elements[i] < smallest)
{
smallest = elements[i];
if (elements[j] < secondSmallest)
{
secondSmallest = elements[j];
}
}
}
}
System.out.println("The smallest element is: " + smallest + "\n"+ "The second smallest element is: " + secondSmallest);
}
This works for a few numbers, but not all. The numbers change around because the inner if condition isn't as efficient as the outer if condition.
这适用于一些数字,但不是全部。数字会发生变化,因为内部 if 条件不如外部 if 条件有效。
Array rearrangements are forbidden.
禁止阵列重排。
采纳答案by nesteant
Try this one. Second condition is used to catch an event when the smallest number is the first
试试这个。第二个条件用于在最小数字为第一个时捕获事件
int[] elements = {-5, -4, 0, 2, 10, 3, -3};
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;
for (int i = 0; i < elements.length; i++) {
if(elements[i]==smallest){
secondSmallest=smallest;
} else if (elements[i] < smallest) {
secondSmallest = smallest;
smallest = elements[i];
} else if (elements[i] < secondSmallest) {
secondSmallest = elements[i];
}
}
UPDby @Axel
@Axel的UPD
int[] elements = {-5, -4, 0, 2, 10, 3, -3};
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;
for (int i = 0; i < elements.length; i++) {
if (elements[i] < smallest) {
secondSmallest = smallest;
smallest = elements[i];
} else if (elements[i] < secondSmallest) {
secondSmallest = elements[i];
}
}
回答by Adam Stelmaszczyk
public static int findSecondSmallest(int[] elements) {
if (elements == null || elements.length < 2) {
throw new IllegalArgumentException();
}
int smallest = elements[0];
int secondSmallest = elements[0];
for (int i = 1; i < elements.length; i++) {
if (elements[i] < smallest) {
secondSmallest = smallest;
smallest = elements[i];
}
else if (elements[i] < secondSmallest) {
secondSmallest = elements[i];
}
}
return secondSmallest;
}
回答by Amit.rk3
public static void main(String[] args)
{
int[] elements = {-4 , 2 , 10 , -2, -3 };
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;
for (int i = 0; i < elements.length; i++)
{
if (smallest>elements[i])
smallest=elements[i];
}
for (int i = 0; i < elements.length; i++)
{
if (secondSmallest>elements[i] && elements[i]>smallest)
secondSmallest=elements[i];
}
System.out.println("The smallest element is: " + smallest + "\n"+ "The second smallest element is: " + secondSmallest);
}
回答by akmal saeed
Try this ...
First condition checks if both values are less than value in array.
Second condition if value is less than small than smallest=element[i]
else secondSmallest=elements[i]
..
试试这个……第一个条件检查两个值是否都小于数组中的值。第二个条件如果 value 小于smallest=element[i]
else secondSmallest=elements[i]
..
public static void main(String[] args)
{
int[] elements = {0 , 2 , 10 , 3, -3 };
int smallest = elements[0];
int secondSmallest = 0;
for (int i = 0; i < elements.Length; i++)
{
if (elements[i]<smallest || elements[i]<secondSmallest )
{
if (elements[i] < smallest )
{
secondSmallest = smallest ;
smallest = elements[i];
}
else
{
secondSmallest = elements[i];
}
}
}
System.out.println("The smallest element is: " + smallest + "\n"+ "The second smallest element is: " + secondSmallest);
}
回答by Feeroz Alam
int[] arr = { 4, 1, 2, 0, 6, 1, 2, 0 };
int smallest = Integer.MAX_VALUE;
int smaller = Integer.MAX_VALUE;
int i = 0;
if (arr.length > 2) {
for (i = 0; i < arr.length; i++) {
if (arr[i] < smallest) {
smaller = smallest;
smallest = arr[i];
} else if (arr[i] < smaller && arr[i] > smallest) {
smaller = arr[i];
}
}
System.out.println("Smallest number is " + smallest);
System.out.println("Smaller number is " + smaller);
} else {
System.out.println("Invalid array !");
}
}
回答by Ankit
Simply, you can do this
很简单,你可以这样做
int[] arr = new int[]{34, 45, 21, 12, 54, 67, 15};
Arrays.sort(arr);
System.out.println(arr[1]);
回答by Jay Parikh
You can do it in O(n) time. Below is the python code
你可以在 O(n) 时间内完成。下面是python代码
def second_small(A):
if len(A)<2:
print 'Invalid Array...'
return
small = A[0]
second_small = [1]
if small > A[1]:
second_small,small = A[0],A[1]
for i in range(2,len(A)):
if A[i] < second_small and A[i]!=small:
if A[i] < small:
second_small = small
small = A[i]
else:
second_small = A[i]
print small, second_small
A = [12, 13, 1, 10, 34, 1]
second_small(A)
回答by girish
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter array size = ");
int size=in.nextInt();
int[] n = new int[size];
System.out.println("Enter "+ size +" values ");
for(int i=0;i<n.length;i++)
n[i] = in.nextInt();
int small=n[0],ssmall=n[0];
// finding small and second small
for(int i=0;i<n.length;i++){
if(small>n[i]){
ssmall=small;
small=n[i];
}else if(ssmall>n[i])
ssmall=n[i];
}
// finding second small if first element itself small
if(small==n[0]){
ssmall=n[1];
for(int i=1;i<n.length;i++){
if(ssmall>n[i]){
ssmall=n[i];
}
}
}
System.out.println("Small "+ small+" sSmall "+ ssmall);
in.close();
}
回答by Rounak
public static void main(String[] args) {
int arr[] = {6,1,37,-4,12,46,5,64,21,2,-4,-3};
int lowest =arr[0];
int sec_lowest =arr[0];
for(int n : arr){
if (lowest > n)
{
sec_lowest = lowest;
lowest = n;
}
else if (sec_lowest > n && lowest != n)
sec_lowest = n;
}
System.out.println(lowest+" "+sec_lowest);
}
回答by Keshav Gera
public class SecondSmallestNumberInArray
{
public static void main(String[] args)
{
int arr[] = { 99, 76, 47, 85, 929, 52, 48, 36, 66, 81, 9 };
int smallest = arr[0];
int secondSmallest = arr[0];
System.out.println("The given array is:");
boolean find = false;
boolean flag = true;
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println("");
while (flag)
{
for (int i = 0; i < arr.length; i++)
{
if (arr[i] < smallest)
{
find = true;
secondSmallest = smallest;
smallest = arr[i];
} else if (arr[i] < secondSmallest) {
find = true;
secondSmallest = arr[i];
}
}
if (find) {
System.out.println("\nSecond Smallest number is Array : -> " + secondSmallest);
flag = false;
} else {
smallest = arr[1];
secondSmallest = arr[1];
}
}
}
}
**Output is**
D:\Java>java SecondSmallestNumberInArray
The given array is:
99 76 47 85 929 52 48 36 66 81 9
Second Smallest number is Array : -> 36
D:\Java>