首页 > 代码库 > diary 12 -20 github上的找最大子程序和的项目

diary 12 -20 github上的找最大子程序和的项目

我想看github上那人是对于一个c程序是如何测试的,文件的安排。

昨天开始的,后来弄弄这个,弄弄那个就乱了,番茄时候会帮助自己跳出死循环,把目标和任务记下了以提醒自己。

就是利用库函数中的计时函数,测量用不同方法所学的计算时间。这倒是挺好的方法,我以前也试图比较某些算法,如各种排序算法,所用的时间,忘了当时用什么方法了。

在该项目中,那人用了10*100的数组作为实验数据,赋予初始值,很大一片。还真挺不容易的。

 

利用一个数组,在初始值中,存储每次需要测定的数据个数,可以应用循环,简化程序。数组和结构一样,是非常基本,却又非常有用的数据结构。

除了测试程序,测试结果文件,项目中还有报告,觉得挺好玩的,拷贝如下:

Maximum Subarray Project report

1. Mathematical analysis

1.1 Psuedocode

1.1.1 Enumeration pseudocode

enumeration(a: array of integers)
max = -infinity
n = length(a)
for i = 0 to n
sum = 0
for j = i to n
for k = i to k = j
sum += a[n]
if sum > max
max = sum
return max

1.1.2 Better enumeration pseudocode

betterEnumeration(a: array of integers)
max = -infinity
n = length(a)
for i = 0 to n
sum = 0
for j = i to j < n
sum += a[j]
if sum > max
max = sum
return max


1.1.3 Divide and Conquer pseudocode

divideAndConquer(a: array of integers, lo, hi)

//base case
if lo == hi
return a[hi]

//recursive case
midpoint = (lo+hi)/2

//left side
leftMax = divideAndConquer(a, lo, midpoint)

//right side
rightMax = divideAndConquer(a, midpoint+1, hi)

//crossing both sides
//left side
leftBothMax = -infinity
for i = midpoint to i >= 0
leftBothSum += a[i]
if leftBothSum > leftBothMax
leftBothMax = leftBothSum
//right side
rightBothMax = -infinity
for i = midpoint to i >= 0
rightBothSum += a[i]
if rightBothSum > rightBothMax
rightBothMax = rightBothSum

bothMax = leftBothMax + rightBothMax

return max(bothMax, leftMax, rightMax)

1.2 Asymptomatic Analysis

1.2.1 Enumeration

Let the array size be n.
There are three nested loops.
For the first loop, the amount of work is n.
For the second loop, there is n-i work.
For the third loop, there is n-i - j
Total work is n*(n-i)*(n-j)
= n^3 + An^2 + Bn +C
Therefore, Enumeration is O(n^3)

1.2.2 Better enumeration

Let the array size be n
There are two nested loops
For the first loop, the amount of work is n
For the second loop, the amount of work is n-i
Therefore the total work is n*n-i = n^2 +ni
Thus, Better Enumeration is O(n^2)

1.2.3 Divide and Conquer

-Recursive part
let n be the array size
For each level of recursion, the array size is halved.
Therefore, this portion of the algorithm is O(logn)

-Iterative part
there are two sequential loops.
both loops are n/2, therefore the combined work is n.
Therefore, the iterative part is O(n)

-Complete algorithm
Combined, the complete algorithm is: O(nlogn)

2. Theoretical correctness

Claim: DivideandConquer correctly returns the sum of the subarray with the largest sum.

Proof: For an array A, let P(A) be the statement that DivdeandConquer(A, lo , hi)
correctly finds the greatest subarray. We will prove that P(A) is true using induction.

As a base case, consder when |A| is 1. The only element is the greatest subarray.

As an induction hypothesis, suppose that P(A) is true for all lists of length < n where length >= 1; that is,
suppose that for any array A of length < n, DivedeandConquer will correctly return the sum of the greatest subarray.

Now, consider an array A of length < n.

3. Testing

4. Experimental analysis

5. Extrapolation and interpretation

 

diary 12 -20 github上的找最大子程序和的项目