aistrate / algorithmssedgewick Goto Github PK
View Code? Open in Web Editor NEWCode from the book "Algorithms" (4th ed.) by Robert Sedgewick and Kevin Wayne (original, and my solutions to exercises).
Code from the book "Algorithms" (4th ed.) by Robert Sedgewick and Kevin Wayne (original, and my solutions to exercises).
Hello:
In the Ex_1_1_16 Java code, I don't understand why the result is 311361142246??The question is to examine what?
Can you explain it for me ? :)
In QueueIterator
Item item = q[i % q.length];
Why not q[(first + i) % q.length];
?
Thanks for your work on this. I'm currently going over the book and I have been a little off by the section on Heapsort
. One of the things that I was confused about was dealing with the index 1 to N in the sorting method. I noticed from your implementation that you use -1
in the swap and in the less but have you tested?
class Heap
# Trickledown
def sink(a, k, n)
while 2 * k <= n
j = 2 * k # child
if !a[j + 1].nil? # check if there is a right child
j += 1 if j > 1 && less(a, j, j + 1) # if left child less than right child
end
break if !less(a, k, j) # If parent greater than child break
swap(a, k, j)
k = j
end
end
def sort(a)
n = a.length
k = n / 2
while k >= 1
sink(a, k, n)
k -= 1
end
while n > 1
swap(a, 1, n)
n -= 1
sink(a, 1, n)
end
a
end
def less(pq, i, j)
pq[i - 1] < pq[j - 1]
end
def swap(a, i, j)
temp = a[i - 1]
a[i - 1] = a[j - 1]
a[j - 1] = temp
end
end
When I do your implementation in Ruby I get the error for as the answer ["A", "M", "E", "L", "E", "O", "P", "R", "S", "T", "X"]
and I haven't been able to figure out why.
I created the following post on Stackoverflow
https://stackoverflow.com/questions/63634906/ruby-heapsort-sink-undefined-method-for-nilnilclass
trying to figure out where am I off.
I think s[a[i]][i]++; is right not s[i][a[i]]++; in 22 line num. Am I wrong?
In the Ex_1_1_27a.java binomial() function, I test the code,but there is a error :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method incremen() is undefined for the type Counter
In your program, the incremen() is undefined, so, how can the this program run successfully?
I know the Counter c is used to cumulative the number of recursive calls, but can't understand why use the function c.incremen().
I think now that c is used to cumulative the number of recursive calls, I use this program:
public static double binomial( int n, int k, double p, int c) {
if ( n == 0 && k == 0) {
return 1.0;
}
if ( n < 0 || k < 0) {
return 0.0;
}
c++;
return (1.0 - p) * binomial( n - 1, k, p, c) + p * binomial( n - 1, k - 1, p, c);
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]),
k = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
int c = 0;
double b = binomial(n, k, p, c);
StdOut.println(b);
StdOut.println(c);
}
I test 10 5 0.5, but I get the result: 0.24609375 0.
I hope to help me Solve this doubt,thanks a lot !
de
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.