求和數組和大O表示法

[英]Summing an Array and Big O Notation


How to find an algorithm for calculating the sum value in the array??

如何找到一個算法來計算數組中的和值?

Is is Something like this?

這是這樣的嗎?

Algorithm Array Sum
Input: nonnegative integer N, and array A[1],A[2],...,A[N]
Output: sum of the N integers in array A
Algorith Body:
j:=1
sum:=0
while j<N
      sum := sum + a[J]
      j:=j+1
  end while
end Algorithm Array Sum

And how I can relate it with the running time of the algorithm by using O-Notation

以及如何通過使用O-Notation將其與算法的運行時間聯系起來

This is the past year exam and I need to make revision for my exam.

這是過去一年的考試,我需要修改我的考試。

Question
An Array A[] holding n integer value is given
1.Give an algorithm for calculating the sum of all the value in the array
2.Find the simplest and best O-notation for the running time of the algorithm.

問題給出一個包含n整數值的數組A [] 1.給出計算數組中所有值之和的算法2.找到算法運行時間的最簡單和最佳O符號。

4 个解决方案

#1


The question is to find the sum of all the values so iterate through each element in the array and add each element to a temporary sum value.

問題是找到所有值的總和,以便遍歷數組中的每個元素,並將每個元素添加到臨時總和值。

temp_sum = 0
for i in 1 ...array.length
    temp_sum = temp_sum + array[i]

Since you need to go through all the elements in the array, this program depends linearly to the number of elements. If you have 10 elements, iterate through 10 elements, if you have a million you have no choice other than to go through all the million elements and add each of them. Thus the time complexity is Θ(n).

由於您需要遍歷數組中的所有元素,因此該程序線性地取決於元素的數量。如果你有10個元素,那么迭代10個元素,如果你有100個元素,除了遍歷所有百萬個元素並添加它們之外你別無選擇。因此,時間復雜度是Θ(n)。

If you are finding the sum of all the elements and you dont know any thing about the data then you need to look at all the elements at least once. Thus n is the lowerbound. You also need not look at the element more than once. n is also the upper bound. Hence the complexity is Θ(n).

如果您找到所有元素的總和,並且您不知道有關數據的任何事情,那么您需要至少查看一次所有元素。因此n是下限。您也不需要多次查看該元素。 n也是上限。因此,復雜度是Θ(n)。

However if you know something about the elements..say you get a sequency of n natural numbers, you can do it in constant time with n(n+1)/2. If the data you get are random then you have no choice but do the above linear time algorithm.

但是,如果您對元素有所了解,那么您可以獲得n個自然數的序列,您可以在n(n + 1)/ 2的恆定時間內完成。如果您獲得的數據是隨機的,那么您別無選擇,只能執行上述線性時間算法。

#2


Since n is the size of array and all you have to do is iterate from begeinning to end the the Big O notation is O[n]

由於n是數組的大小,所有你需要做的就是從begeinning迭代到結束Big O表示法是O [n]

integer N= Size_array;
array a[N]
j=1 
sum=0
while j<=N 
 sum += a[j]  
 j++
end while

#3


I think that you meant "while j <= N", you need to specify this.

我認為你的意思是“當j <= N”時,你需要指定它。

The running time shall be O(n), I think, as you have only one loop.

我想,運行時間應為O(n),因為你只有一個循環。

#4


To calculate O for this algorithm you need to count the number of times each line of code executes. Later on you will only count the fundamental operations but start by counting all.

要計算此算法的O,您需要計算每行代碼執行的次數。稍后您將只計算基本操作,但首先計算所有操作。

So how many times will the j := 1 line run? How many times will the sum := 0 run? How many times will the while loop's condition execute? The statements inside the while loop?

那么j:= 1行會運行多少次?總和:= 0運行多少次? while循環的條件執行多少次? while循環中的語句?

Sum these all up. You will notice that the value you get will be something like 1 + 1 + n + n + n = 2 + 3n. thus you can conclude that it is a linear function on n.

總結一下這些。你會注意到你得到的值將是1 + 1 + n + n + n = 2 + 3n。因此,您可以得出結論,它是n上的線性函數。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/05/24/87d4072b86736448a6504bcc4a8a0d4.html



 
  © 2014-2022 ITdaan.com 联系我们: