简体   繁体   English

在while循环中运行几次迭代

[英]Running several iterations in a while loop

I am trying to solve a problem of finding the smallest and second smallest element in an array. 我正在尝试解决在数组中找到最小和第二最小元素的问题。

I am thinking of putting two pointers on the 0th index of the array. 我正在考虑将两个指针放在数组的第0个索引上。 Both the pointers move from left to right traversing the entire array. 两个指针都从左向右移动,遍历整个数组。 First pointer ptr1 determines the min element while the second pointer intends to determine the second min element. 第一指针ptr1确定最小元素,而第二指针打算确定第二最小元素。 The first pointer works ok but the second pointer doesn't traverse. 第一个指针可以正常工作,但第二个指针不能遍历。 While loop exits only after 1 iteration of the second pointer. while循环仅在第二个指针重复1次后退出。

Is it possible to have n pointers in a while loop & make them traverse from left to right turn by turn? 是否可以在while循环中具有n个指针并使它们从左向右依次遍历?

Or I am doing something wrong. 或我做错了什么。

Below is the code 下面是代码

int arr[] = {12,13,1,10,34,1}; 
        int ptr1 = 0;
        int ptr2 =0;
        int min = Integer.MAX_VALUE;
        int minSec = Integer.MAX_VALUE;
        int arrLen=arr.length-1;

        while(ptr1<arrLen && ptr2<arrLen){

            if(arr[ptr1]<min){  // this if works great finds the min element
                min=arr[ptr1];
                ptr1++;
            }else{
                ptr1++;
            }

            //flow enters once & exits the while loop
            if(ptr1==arrLen && arr[ptr2]<minSec && arr[ptr2]>min){
                minSec=arr[ptr2];
                ptr2++;
            }else if(ptr1==arrLen){
                ptr2++;
            }
        }

      System.out.println("min: " + min + " second min: "+ minSec)

output: min: 1 second min: 12 输出: min: 1 second min: 12

the correct output should be min: 1 second min: 10 正确的输出应为min: 1 second min: 10

I am able to solve the problem with another approach, code below. 我可以使用另一种方法(下面的代码)解决问题。 I just need to know about the while loop approach. 我只需要了解while循环方法。

for (int i = 0; i <= arrLen ; i ++)
        {
            /* If current element is smaller than first
              then update both first and second */
            if (arr[i] < min)
            {
                minSec = min;
                min = arr[i];
            }

            /* If arr[i] is in between first and second
               then update second  */
            else if (arr[i] < minSec && arr[i] != min)
                minSec = arr[i];
        }

Because ptr2 value is 0 until loop reach the end 因为ptr2的值为0,直到循环结束

if(ptr1==arrLen && arr[ptr2]<minSec && arr[ptr2]>min){
                minSec=arr[ptr2];
                ptr2++;
}

and enter into the if condition only ptr1==arrLen then you select the minSec value as minSec=arr[ptr2] . 并仅在if条件中输入ptr1==arrLen然后将minSec值选择为minSec=arr[ptr2] No point of putting this condition here. 没有必要把这个条件放在这里。

So second if condition will be like 所以第二个条件是

if(arr[ptr2]<minSec && arr[ptr2]>min){
   minSec=arr[ptr2];
   ptr2++;
}else{
   ptr2++;
}

Your problem is that the first if statement still works even if you found the smallest number. 您的问题是,即使找到了最小的if语句,第一个if语句仍然有效。 So all over traversed elements are either greater as or equal to the smallest number. 因此遍历的所有元素都大于或等于最小数目。 That means pointer 1 is incremented every "while step". 这意味着指针1会在每个“同时步”中递增。 In your second if statement you check if pointer 1 is equal to array length but this case is present in only one "while step". 在第二条if语句中,检查指针1是否等于数组长度,但是这种情况仅在一个“ while步骤”中出现。

PS: Just let java sort your array. PS:只是让Java对数组进行排序。 The work is already done for you ;) 工作已经为您完成;)

You do not need two pointers 您不需要两个指针

int arr[] = {12,13,1,10,34,1}; 
final int arrLen=arr.length;
int min = Integer.MAX_VALUE;
int minSec = Integer.MAX_VALUE;
for (int e=0; e<arrLen; e++) {
    final int v = arr[e];
    if (v<minSec && v>min)
        minSec = v;
    if (v<min && v<minSec)
        min = v;
}
if (min>minSec)
    min = minSec;

The problem is in this while(ptr1<arrLen && ptr2<arrLen) statement coupled with moving the second pointer only when ptr1 is at length. 问题在于, while(ptr1<arrLen && ptr2<arrLen)语句仅在ptr1处于长度时才与移动第二个指针结合在一起。

What happens is ptr1 is at arrLen so the second pointer iterates once, but because you use an && when ptr1<arrLen is evalued to be false the entire loop exits. 发生的情况是ptr1在arrLen因此第二个指针迭代一次,但是因为当ptr1<arrLen值为false时使用了&& ,整个循环退出。 Using an or will not fix this because you have other problems in the code that will cause an IndexOutOfBoundsError. 使用或不能解决此问题,因为您在代码中还有其他问题会导致IndexOutOfBoundsError。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM