pseudocode

Selection Sort, For Java

别来无恙 提交于 2019-12-10 23:23:03
问题 I am having trouble understanding this pseudocode, and implementing it into my program. Can anybody explain it better or show me how the code would look? Thanks. A - an array containing the list of numbers numItems - the number of numbers in the list for i = 0 to numItems - 1 for j = i+1 to numItems if A[i] > A[j] // Swap the entries Temp = A[i] A[i] = A[j] A[j] = Temp End If Next j Next i 回答1: Well, let's translate the pseudo-code to pseudo-English. A - an array containing the list of

How to detect if a repeating pattern exists

↘锁芯ラ 提交于 2019-12-10 14:34:53
问题 My question isn't language specific... I would probably implement this in C# or Python unless there is a specific feature of a language that helps me get what I am looking for. Is there some sort of algorithm that anyone knows of that can help me determine if a list of numbers contains a repeating pattern? Let's say I have a several lists of numbers... [12, 4, 5, 7, 1, 2] [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 1, 1, 1, 1, 1] [ 1, 2, 4, 12, 13, 1, 2, 4, 12, 13] I need to detect if there is a

Proof of induction on pseudocode

不问归期 提交于 2019-12-10 12:08:36
问题 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) Let x(n) and y(n) denote the value of x and y after the while loop has run n times. I have to show by the proof of induction that x(n) + b*y(n) = a What I've done: P(n): x(n) + by(n) = a Let a and b be arbitrary numbers then the first loop will give x(1) = a - b and y(1) = 0 + 1 = 1 P(1): x(1) + by(1) = a <=> a = a so P(1) is true. Assume P(n) is true. We want to show that P(n+1) is

Python - Merge Sort Recursive Algorithm

依然范特西╮ 提交于 2019-12-10 11:36:07
问题 I was trying to implement this pseudocode for the recursive merge sort algorithm: **procedure** *mergesort*(L = a1, a2,…,an ) **if** n > 1 **then** m := ⌊n/2⌋ L1 := a1, a2,…,am L2 := am+1, am+2,…,an L := merge(mergesort(L1), mergesort(L2 )) {L is now sorted into elements in increasing order} **procedure** *merge*(L1, L2 :sorted lists) L := empty list **while** L1 and L2 are both nonempty remove smaller of first elements of L1 and L2 from its list; put at the right end of L **if** this removal

Algorithm for Filling bag maximally (this is not the knapsack 0/1)

久未见 提交于 2019-12-10 10:34:29
问题 I am working on some task which requires from me to solve the following algorithmic problem: - You Have collection of items (their weights): [w1, w2, ..., wn] - And You have a bag which weight is: W - It is Needed to fill the bag maximally (fill as much as possible) with the subset of given items. So this is not "Knapsack 0/1" problem, as we deal only with weights (we have only one parameter for item). Therefore I assume that it might have a solution (Knapsack is NP-complete) or some kind of

How can i get the next highest multiple of 5 or 10

大城市里の小女人 提交于 2019-12-10 10:27:31
问题 From a given double I want to get the next highest number according to some rules which, since I have some difficulty describing them, I will illustrate by examples: Input Desired output ------- -------------- 0.08 0.1 0.2 0.5 5 10 7 10 99 100 100 500 2345 5000 The output should be in some sense the 'next highest multiple of 5 or 10'. I hope this is understandable; if not, let me know. The implementation will be in java and input will be positive double s. 回答1: function top5_10 (x) { var ten

Binary matrix multiplication bit twiddling hack

江枫思渺然 提交于 2019-12-09 17:35:08
问题 Abstract Hi, suppose you have two different independent 64-bit binary matrices A and T ( T is a transposed version of itself, using the transposed version of matrix allows during multiplication to operate on T 's rows rather than columns which is super cool for binary arithmetic) and you want to multiply these matrices the only thing is that matrix multiplication result is truncated to 64-bits and if you yield to a value greater that 1 in some specific matrix cell the resulting matrix cell

Code a linear programming exercise by hand

六眼飞鱼酱① 提交于 2019-12-09 05:52:49
问题 I have been doing linear programming problems in my class by graphing them but I would like to know how to write a program for a particular problem to solve it for me. If there are too many variables or constraints I could never do this by graphing. Example problem, maximize 5x + 3y with constraints: 5x - 2y >= 0 x + y <= 7 x <= 5 x >= 0 y >= 0 I graphed this and got a visible region with 3 corners. x=5 y=2 is the optimal point. How do I turn this into code? I know of the simplex method. And

Algorithm 3d orbiting camera control with mouse drag

匆匆过客 提交于 2019-12-08 13:57:44
Please help, I couldn't find detailed explanation about this which is not language specific and not library dependent, because I want to control the math myself for some reason. How to create orbiting camera control with mouse, like middle-click drag in Google SketchUp ? * In Google SketchUp, the camera can move circularly orbiting imaginary object in the middle of the plane ( not always 0,0,0) by dragging the mouse. It can orbit horizontally, vertically, even diagonally, all directions. The simplest solution is to store X/Y direction angles and eventually a zoom level. Then, you modify the

how to reindex a sparse associative array

ⅰ亾dé卋堺 提交于 2019-12-08 13:01:42
问题 first off, this question is not related to a specific language - I use Haxe to target multiple platforms - so a pseudo code will be more than enough. here's my problem : I have a sparse Matrix description in this form: edges = [ 1,1,2,1,3,1,4,1, 2,2,3,2, 3,3,4,3,5,3, 4,4,5,4,6,4, 5,5,6,5,7,5,25,5,27,5,28,5,29,5,30,5 ]; this describes edges associations: point 1 is linked to points 1, 2, 3 & 4 point 2 is linked to points 2 & 3 point 3 is linked to points 3, 4 & 5 point 4 is linked to points 4,