Algorithms/Find approximate maximum

Method 1

edit

Code Java

edit
  public int findApproimateMax(int[] i_aryTb) {
     final int factorA = 100;
     int o_intMax = i_aryTb[0];
     int p_countMax = 1;
     for (int p_intI = 1; p_intI < i_aryTb.length; p_intI++) {
         if ( i_aryTb[p_intI] > o_intMax ) {
            o_intMax = i_aryTb[p_intI];
            p_countMax = 0;
         } else {
            p_countMax = p_countMax + 1;
            if ( p_countMax >= factorA )
              return o_intMax;       
         } // end if
     } // end loop
     return o_intMax;
   } //end method

Algorithm

edit

Method : findApproimateMax(i_aryTb)

  define a constant call factorA and assign 100 
  define o_intMax is integer and assign i_aryTb[0];
  define p_countMax is integer and assign 1 
  for loop = 1 to (i_aryTb.length - 1) do, index
      if ( i_aryTb[index] > o_intMax ) then
         o_intMax   := i_aryTb[index];
         p_countMax := 0;
      else
         p_countMax = p_countMax + 1;
         if ( p_countMax bigger or equal factorA ) then
            return o_intMax and terminate the method;
         end if
      end if
  end loop
  return o_intMax;

Method 2

edit

Code java

edit
   public int findMax(int[] i_aryTB,int numberOfSecond) {
       if ( numberOfSecond <= 0 ) {
          numberOfSecond = -1;
       } // end if
       final int factorA = 100;
       long p_logStart = System.currentTimeMillis;
       long p_logFinish = p_logStart + (numberOfSecond * 1000);
       int o_intMax = i_aryTb[0];
       for ( int p_intI= 1; p_intI < i_aryTb.length; p_intI++) {
           if ( i_aryTb[p_intI] > o_intMax ) {
               o_intMax = i_aryTb[intI];
           } // end if
           if ( p_intI % factorA == 0 ) {
              if ( p_logFinish <= System.currentTimeMillis && numberOfSecond != -1) {
                  return o_intMax;
              } // end if
           } // end if
       } // end loop
       o_intMax;
   } // end method

Algorithm

edit

Method : findMax(i_aryTB, numberOfSecond)

  if numberOfSecond <= 0 then
     numberOfSecond := -1;
  end if
   define a constant factorA := 100;
   define p_logStart become := current_time;
   define p_logFinish := p_logStart + ( numberOfSecond );
   define o_intMax := i_aryTb[0];
   looping from 1 to (aryTb.length - 1), p_intI
   begin 
       if ( aryTb[p_intI] > o_intMax ) then
            o_intMax := aryTb[p_intI];
       end if;
       if ( the remainder of p_intI / factorA is zero ) then
       begin
          if ( p_logFinish <= current_time and numberofSecond not equal -1 ) then
                return o_intMax;
                terminate the method;
          end if
       end if
   end looping
   return o_intMax;

Method 3

edit

Code java

edit
  public int findApproximateMax(int[] i_intaryTb,final int i_oneStepLength) {
     int o_max = i_intaryTb[0];
     for (int intI = 0; intI < i_intaryTb; intI += i_oneStepLength)
        if ( i_intaryTb[intI] > o_max ) 
            o_max = i_intaryTb[intI];
      return o_max;
  } // end method

Algorithm

edit

Method : findApproximateMax(i_intaryTb,i_one_stepLength)

  define o_max and assign i_intaryTb[0];
  looping from 0 to i_intaryTb.length -1 ,index += i_one_stepLength
      if ( index >= i_intaryTb.length ) then
          break the looping;
      if ( i_intaryTb[index] > o_max ) then
          o_max = i_intaryTb[index];
      end if
  end looping
  return o_max;

Method 4

edit

Code java

edit
import java.util.Random();

   //Assume noOfRandom < 1_intaryTb.length;
   public int findApproximateMax(int[] i_intaryTb,int noOfRanomd) {
      int[] p_intaryRes = generateTheRadnomIndex(noOfRandom);
      int o_intMax      = i_intaryTb[p_intaryRes[0]];
      for ( int intJ = 1; intJ < p_intaryRes.length; intJ++) {
          int p_intK = p_intaryRes[intJ];
          if ( p_intK >= 0 && p_intK < i_intaryTb.length ) {
              if ( i_intaryTb[p_intK] > o_intMax) {
                 o_intMax = p_intaryRes[p_intK];
              } // end if
          } // end if
      } // end loop
      return o_intMax;
   } // end method          
   private int[] generateTheRandomIndex(int nRandom) {
      int[] o_intaryRes = new int[nRandom];
      for (int i = 0; i < nRandom; i++) 
          o_intaryRes[i] = -1;
      for ( int j = 0; j < nRandom; j++) {
          int p_intK =findRandomIndex(nRandom);
          while ( haveIndex(o_intaryRes,p_intK) ) {
              p_intK =findRandomIndex(nRandom);
          } // end while loop
          o_intaryRes[j] = p_intK;
       } // end loop
       return o_intaryRes;
    } // end method
    private boolean haveIndex(int[] i_aryRes,int index) {
       for (int i = 0; i < i_aryRes.length; i++)
          if ( i_aryRes[i] == index) 
             return true;
       return false;
    } // end method
    static Random gen = new Random();
    private int findRandomIndex(int n) {
        return gen.nextInt(n);
    } // end method