pseudocode

Time complexity analysis. choosing operator for counting number of times a line of code runs

旧街凉风 提交于 2020-01-11 11:47:43
问题 Analysing time complexity of this pseudocode. On the right my take on the number of times each line runs. I'm not sure whether to use log n , n log n , or simply n for the while-loop..please help times 1 sum = 0 1 2 i = 1 1 3 while i ≤ n log n + 1 4 for j = 1 to n n log n 5 sum = sum + j n log n 6 i = 2i log n 7 return sum 1 this results in: 2 n log + 2log n + 4 and thereby : O(n log n) is this correct ? 回答1: If your while loop is: 3 while i < n log n + 1 4 for j = 1 to n n log n 5 sum = sum

Sorting: how to sort an array that contains 3 kind of numbers

左心房为你撑大大i 提交于 2020-01-09 19:58:53
问题 For example: int A[] = {3,2,1,2,3,2,1,3,1,2,3}; How to sort this array efficiently? This is for a job interview, I need just a pseudo-code. 回答1: Problem description: You have n buckets, each bucket contain one coin , the value of the coin can be 5 or 10 or 20. you have to sort the buckets under this limitation: 1. you can use this 2 functions only: SwitchBaskets (Basket1, Basket2) – switch 2 baskets GetCoinValue (Basket1) – return Coin Value in selected basket 2. you cant define array of size

A Route Assignment Program Algorithm

…衆ロ難τιáo~ 提交于 2019-12-30 07:27:13
问题 What im trying to do, is create a program that will assign a route for a driving test. there will be three diffrent routes, linked together at certain points. Never should there be more than one student at a point of intersection. Best way to solve this is to schedule these interection points by time. This isnt my only problem, i will need routes to be equally distributed to examiners. So route 1 will be given to examiner 1 route 2 - examiner 2 route 3- examiner 3... The Real Baumann

Static (Lexical) Scoping vs Dynamic Scoping (Pseudocode)

巧了我就是萌 提交于 2019-12-28 02:22:19
问题 Program A() { x, y, z: integer; procedure B() { y: integer; y=0; x=z+1; z=y+2; } procedure C() { z: integer; procedure D() { x: integer; x = z + 1; y = x + 1; call B(); } z = 5; call D(); } x = 10; y = 11; z = 12; call C(); print x, y, z; } From my understanding, the result of this program when run using static scoping is: x=13, y=7, and z=2. However, when it is run using dynamic scoping , the result is: x=10, y=7, and z=12. These results are the ones that our professor gave us. However, I

Prove how the algorithm works

孤者浪人 提交于 2019-12-25 20:49:11
问题 Given the pseudocode MUL(a,b) x=a y=0 WHILE x>=b DO x=x-b y=y+1 IF x=0 THEN RETURN(true) ELSE RETURN(false) I have to prove how the algorithm works. So far I have only explained how it works, but I am not sure how you are supposed to prove how it works. Edit: Just to clarify. I did ask this question on another thread. But the two questions are separate. The assignment I am working on consists of 3 questions. The 1st question is where I explained how the algorithm works. The 2nd question is on

Recursive algorithm for the sum of odd number positive integers

China☆狼群 提交于 2019-12-24 23:34:25
问题 I am expressing the algorithms in pseudo code. I'm just wondering if my design works just as well as the original one displayed below. The algorithm is supposed to compute the sum of n odd positive integers. This is how the algorithm should look: procedure sumofodds(n:positive integer) if n = 1 return 1 else return sumofodds(n-1) + (2n-1) This is how i designed my algorithm: procedure odd(n: positive integer) if n = 1 return 1 if n % 2 > 0 return n + odd(n-1) // this means n is odd if n % 2 =

Break A Large File Into Many Smaller Files With PHP

本秂侑毒 提交于 2019-12-24 18:12:48
问题 I have a 209MB .txt file with about 95,000 lines that is automatically pushed to my server once a week to update some content on my website. The problem is I cannot allocate enough memory to process such a large file, so I want to break the large file into smaller files with 5,000 lines each. I cannot use file() at all until the file is broken into smaller pieces, so I have been working with SplFileObject. But I have gotten nowhere with it. Here's some pseudocode of what I want to accomplish:

Determine whether a directed graph has a unique topological ordering?

扶醉桌前 提交于 2019-12-24 11:30:15
问题 I'm trying to create pseudo-code for an algorithm that will be able to determine whether a directed graph has a unique topological ordering. I've already come up with the following pseudo-code for a topological sort, but what would I have to add or edit in order to determine whether a digraph has a unique topological ordering? Input: A graph G Output: A list L that contain the sorted vertices define an empty list L; create a stack Stack; push all vertices with no incoming edges into Stack;

Understanding pseudocode to construct tree from preorder traversal

回眸只為那壹抹淺笑 提交于 2019-12-24 11:04:43
问题 I need to do something similar to the task described in this question: Construct tree with pre-order traversal given There is a really helpful answer here, but I don't understand the pseudocode fully, so I was wondering if someone could help describe to me what is going on. k = 0 // Initialize input = ... get preorder traversal vector from user ... // Get input Reconstruct(T) // Reconstruct method with tree input if input[k] == N // If element of input is N T = new node with label N // Make a

NTRU Pseudo-code for computing Polynomial Inverses

狂风中的少年 提交于 2019-12-24 01:05:39
问题 I was wondering if anyone could tell me how to implement line 45 of the following pseudo-code. Require: the polynomial to invert a(x), N, and q. 1: k = 0 2: b = 1 3: c = 0 4: f = a 5: g = 0 {Steps 5-7 set g(x) = x^N - 1.} 6: g[0] = -1 7: g[N] = 1 8: loop 9: while f[0] = 0 do 10: for i = 1 to N do 11: f[i - 1] = f[i] {f(x) = f(x)/x} 12: c[N + 1 - i] = c[N - i] {c(x) = c(x) * x} 13: end for 14: f[N] = 0 15: c[0] = 0 16: k = k + 1 17: end while 18: if deg(f) = 0 then 19: goto Step 32 20: end if