首页>Program>source

count的value 每次循环都会不断增加(32-64-70等).但是我需要在完成每个循环后将该值设为零。

公共类BinarySearchLecture {

int count = 0;
int binarySearchLecture(int arr[], int l, int r, int x) {
   if (r >= l) { 
      int mid = l + (r - l) / 2; 
      count++;
      if (arr[mid] == x) 
      {
         count++;
         return mid; 
      }
      if (arr[mid] > x) 
      {  
         count++; 
         return binarySearchLecture(arr, l, mid - 1, x);
      }
      else  
      {  
          count++;
         return binarySearchLecture(arr, mid + 1, r, x); 
      }
   } else {     
      return -1;
   }
} 
   
}

import java.util.Random; 公共类FirstAssignment {

// Driver method to test  
public static void main(String args[]) 
{ 
    BinarySearchLecture bs = new BinarySearchLecture(); 
    int arr[] = new int[65536];
    int n = arr.length; 
    
     
    for (int i = 0; i<n; i++) 
    {
         arr[i] = new Random().nextInt(10000000);
    }
    
    
    for (int i = 1; i <=3; i++)
    {
         int x = new Random().nextInt(10000000);
        System.out.println( i + "." + "Random search key: " + x );
    
    int result = bs.binarySearchLecture(arr, 0, n - 1, x); 
    if (result == -1) 
        System.out.println("Element not present"); 
    else 
        System.out.println("Element found at index " + result); 
    
         int count = bs.count; // count is an class variable
          
          System.out.println("Number of comparisons are " + count); 
    
     
        //  System.out.format("%32s%10d%16s", x , count , "presnt");
          

} }}

最新回答
  • 10天前
    1 #

    您的binarySearchLecture方法正在增加计数值。

    您还可以使用bs.resetCount()/ bs.setCount(0)之类的方法或类似方法手动重置它.您可以将其添加到您定义的bs类中。

    public class BinarySearchLecture {
    int count = 0;
     int binarySearchLecture(int arr[], int l, int r, int x) {
       if (r >= l) { 
          int mid = l + (r - l) / 2; 
          count++;
          if (arr[mid] == x) 
          {
             count++;
             return mid; 
          }
          if (arr[mid] > x) 
          {  
             count++; 
             return binarySearchLecture(arr, l, mid - 1, x);
          }
          else  
          {  
              count++;
             return binarySearchLecture(arr, mid + 1, r, x); 
          }
       } else {     
          return -1;
       }
      }
    void resetCount(){
      count = 0;
    }
    }
    

    另一种性能不佳解决方案-在每个循环中创建bs类的新实例。

    for (int i = 1; i <= 3; i++) {
      int x = new Random().nextInt(10000000);
      System.out.println(i + "." + "Random search key: " + x);
      bs = new <bs class>();
      int result = bs.binarySearchLecture(arr, 0, n - 1, x);
      if (result == -1){
        System.out.println("Element not present");
      }
      else {
        System.out.println("Element found at index " + result);
      }
        int count = bs.count; // count is an class variable
        System.out.println("Number of comparisons are " + count);
    }
    

  • c++:如何在C中使用int指向struct
  • javascript:将数据从NPM模块传递到应用程序