知识大全 Weiss的java数据结构与问题解决

Posted 文字

篇首语:努力尽今夕,少年犹可夸。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Weiss的java数据结构与问题解决相关的知识,希望对你有一定的参考价值。

Weiss的java数据结构与问题解决  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  import java util Random;

  public final class MaxSumTest

  

  static private int seqStart = ;

  static private int seqEnd = ;

  /**

  * Cubic maximum contiguous subsequence sum algorithm

  * seqStart and seqEnd represent the actual best sequence

  */

  public static int maxSubSum ( int [ ] a )

  

  int maxSum = ;

  for( int i = ; i < a length; i++ )

  for( int j = i; j < a length; j++ )

  

  int thisSum = ;

  for( int k = i; k <= j; k++ )

  thisSum += a[ k ];

  if( thisSum > maxSum )

  

  maxSum   = thisSum;

  seqStart = i;

  seqEnd   = j;

  

  

  return maxSum;

  

  /**

  * Quadratic maximum contiguous subsequence sum algorithm

  * seqStart and seqEnd represent the actual best sequence

  */

  public static int maxSubSum ( int [ ] a )

  

  int maxSum = ;

  for( int i = ; i < a length; i++ )

  

  int thisSum = ;

  for( int j = i; j < a length; j++ )

  

  thisSum += a[ j ];

  if( thisSum > maxSum )

  

  maxSum = thisSum;

  seqStart = i;

  seqEnd   = j;

  

  

  

  return maxSum;

  

  /**

  * Linear time maximum contiguous subsequence sum algorithm

  * seqStart and seqEnd represent the actual best sequence

  */

  public static int maxSubSum ( int [ ] a )

  

  int maxSum = ;

  int thisSum = ;

  for( int i = j = ; j < a length; j++ )

  

  thisSum += a[ j ];

  if( thisSum > maxSum )

  

  maxSum = thisSum;

  seqStart = i;

  seqEnd   = j;

  

  else if( thisSum < )

  

  i = j + ;

  thisSum = ;

  

  

  return maxSum;

  

  /**

  * Recursive maximum contiguous subsequence sum algorithm

  * Finds maximum sum in subarray spanning a[left right]

  * Does not attempt to maintain actual best sequence

  */

  private static int maxSumRec( int [ ] a int left int right )

  

  int maxLeftBorderSum = maxRightBorderSum = ;

  int leftBorderSum = rightBorderSum = ;

  int center = ( left + right ) / ;

  if( left == right )  // Base case

  return a[ left ] > ? a[ left ] : ;

  int maxLeftSum  = maxSumRec( a left center );

  int maxRightSum = maxSumRec( a center + right );

  for( int i = center; i >= left; i– )

  

  leftBorderSum += a[ i ];

  if( leftBorderSum > maxLeftBorderSum )

  maxLeftBorderSum = leftBorderSum;

  

  for( int i = center + ; i <= right; i++ )

  

  rightBorderSum += a[ i ];

  if( rightBorderSum > maxRightBorderSum )

  maxRightBorderSum = rightBorderSum;

  

  return max ( maxLeftSum maxRightSum

  maxLeftBorderSum + maxRightBorderSum );

  

  /**

  * Return maximum of three integers

  */

  private static int max ( int a int b int c )

  

  return a > b ? a > c ? a : c : b > c ? b : c;

  

  /**

  * Driver for divide and conquer maximum contiguous

  * subsequence sum algorithm

  */

  public static int maxSubSum ( int [ ] a )

  

  return a length > ? maxSumRec( a a length ) : ;

  

  public static void getTimingInfo( int n int alg )

  

  int [] test = new int[ n ];

  long startTime = System currentTimeMillis( );;

  long totalTime = ;

  int i;

  for( i = ; totalTime < ; i++ )

  

  for( int j = ; j < test length; j++ )

  test[ j ] = rand nextInt( ) ;

  switch( alg )

  

  case :

  maxSubSum ( test );

  break;

  case :

  maxSubSum ( test );

  break;

  case :

  maxSubSum ( test );

  break;

  case :

  maxSubSum ( test );

  break;

  

  totalTime = System currentTimeMillis( ) startTime;

  

  System out println( Algorithm # + alg + \\t

  + N = + test length

  + \\ttime = + ( totalTime * / i ) + microsec );

  

  private static Random rand = new Random( );

  /**

  * Simple test program

  */

  public static void main( String [ ] args )

  

  int a[ ] = ;

  int maxSum;

  maxSum = maxSubSum ( a );

  System out println( Max sum is + maxSum + ; it goes

  + from + seqStart + to + seqEnd );

  maxSum = maxSubSum ( a );

  System out println( Max sum is + maxSum + ; it goes

  + from + seqStart + to + seqEnd );

  maxSum = maxSubSum ( a );

  System out println( Max sum is + maxSum + ; it goes

  + from + seqStart + to + seqEnd );

  maxSum = maxSubSum ( a );

  System out println( Max sum is + maxSum );

  // Get some timing info

  for( int n = ; n <= ; n *= )

  for( int alg = ; alg >= ; alg– )

  

  if( alg == && n > )

  continue;

  getTimingInfo( n alg );

  

  

cha138/Article/program/Java/hx/201311/27079

相关参考

知识大全 Java中根据模板导出数据到word的解决方案

Java中根据模板导出数据到word的解决方案  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  J

知识大全 java连接mysql数据库乱码的解决方案

  解决方法一:  mysql安装时候的编码  看下myini有无  [mysql]  defaultcharacterset=utf  [client]defaultcharacterset=utf

知识大全 Java多线程支持如何才能解决接口问题

Java多线程支持如何才能解决接口问题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Java多

知识大全 跟你一起分析JAVA中文比较问题的解决

跟你一起分析JAVA中文比较问题的解决  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Java的

知识大全 Java多线程如何解决关键字封锁问题

Java多线程如何解决关键字封锁问题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Java多线

知识大全 Java编程技术中汉字问题的分析及解决

Java编程技术中汉字问题的分析及解决  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在基于Ja

知识大全 Java SynDemo对象如何解决继承问题

JavaSynDemo对象如何解决继承问题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Jav

知识大全 如何解决Java ME设备碎片问题

如何解决JavaME设备碎片问题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  一次编写随处运行

知识大全 Java web解决常见编码出现的问题

Javaweb解决常见编码出现的问题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n

知识大全 用数组解决JAVA中的大数问题-以求阶乘为例

用数组解决JAVA中的大数问题-以求阶乘为例!  以下文字资料是由(历史新知网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  impor