跳转到内容

算法/查找最大值

来自维基教科书,开放世界中的开放书籍

顶部,章节:123456789A

这是一个教别人不同编程语言的资料来源。查找最大值是主题。


橙色方法

[编辑 | 编辑源代码]
 if the tb length is 1 then
    output the zero is index of array tb;
 else if the tb length is 0 then
    output 0;
 define variable call o_intMax ( integer type);
 o_intMax become tb[indexInd];
 looping from 1 to (tb's length - 1) save the index into variable indexInt
    if tb[indexInt] > maxInt then
       maxInt become tb[indexInt];
    end if
 end loop
Java Vbscript Java 脚本 JScript Visual Basic Pascal Oracle SQL Perl PHP C Python

苹果方法

[编辑 | 编辑源代码]
  if input_array size is 0 then
     return 0
  end if
  if input_array size is 1 then
     return input_array[0]
  end if
  if input_array size is 2 then
     if element zero is bigger than element one then
         return element zero
     else 
         return element one 
     end if
  end if
  create a array call aryA and the size is (input_array size divide 2)
  create a array call aryB and the size is (input_array size - aryA size)
  looping from zero to (input_array size / 2), 
     aryA[loopIndex] = input_array[loopindex]
     loopindex = loopindex + 1;
  end looping
  define integer variable call intK, initial it the zero
  looping from (aryA size) to input_array size
     aryB[intK] = input_array[loopIndex]
     loopindex  = loopindex + 1
     intK       = intK + 1;
  end loop
  create a variable call intM
  create a variable call intN
  intM = run findMax(aryA);
  intN = run findMax(aryB);
  if intM > intN then
    return intM
  else
     return intN
  end if
Java Vbscript Java 脚本 JScript Visual Basic Pascal Oracle SQL Perl PHP Python

梨方法

[编辑 | 编辑源代码]



方法 findMax(i_aryTab,int i_start,int i_end)

 if ( i_end - start equal one then
   if ( i_aryTab[i_end] > i_aryTab[i_start] )
         return i_aryTab[i_end]
   else
         return i_aryTab[i_start]  
 end if
 define variable call intJ
 intJ :=( (i_end - i_start ) + 1 ) / 2  - 1
 define variable call oMaxN
 define variable call oMaxM
 oMaxN = findMax(i_aryTab,i_start,i_start+intJ)
 oMaxM = findMax(i_aryTab,i_start + intJ + 1,i_end)
 if ( oMaxN > oMax M )
    return oMaxN
 else
    return oMaxM

方法 findMaxHandler(i_aryTab)

 return findMax(i_aryTab,0,i_aryTab.length - 1)
Java Vbscript Java 脚本 JScript Visual Basic Pascal Oracle SQL Perl PHP Python

方法:findMax

  make all the element of input_array = mulitple itself -1;
  run findMin(input_array) put result into o_Max;
  return o_Max * -1;

方法:findMin

 put to element[0] into min
 for index = 1 to arraysize -1 
    if ( element[index] < min ) then
       min = elemnt[index];
     end if
  end loop
  return min
Java Vbscript Java 脚本 JScript Visual Basic Pascal Oracle SQL Perl PHP Python

方法:排序

 define p_blnFound As boolean type;
 p_blnFound := true;
 while ( p_blnFound is true ) do
 begin
    p_blnFound := false;
    for loop from zero to( array size - 2 ) do
        if ( inputArray[index] bigger than inputArray[index+1] ) then
            swap inputArray[index] and inputArray[index + 1];
            p_blnFound := true;
        end if
    end for loop
 end while loop
 return input_array;

方法:findMax

 inputArray = sorting(inputArray);
 return the last element of inputArray;
Java Vbscript Java 脚本 JScript Visual Basic Pascal Oracle SQL Perl PHP Python

方法:findMaxFourEle ( intA,intB,intC,intD)

  int max = intA;
  if intB > max then
  begin
     max = intB;
  end if
  if intC > max then
  begin
     max = intC;
  end if
  if intD > max then
  begin
     max = intD;
  end if
  return max;

方法:findMaxThreeEle(intA,intB,intC)

  return findMaxFourEle(intA,intB,intC,intC);

注意:此方法可与梨方法一起使用

Java Vbscript Java 脚本 JScript Visual Basic Pascal Oracle SQL Perl PHP Python


更多数据即将到来...



顶部,章节:123456789A

华夏公益教科书