"Let all things be done decently and in order." -- 1 Corinthians 14:40
"All go to the same place; all come from dust, and to dust all return." -- Ecclesiastes 3:20
"For whoever has will be given more, and they will have an abundance. Whoever does not have, even what they have will be taken from them." -- Matthew 25:29
"Premature optimization is the root of all evil." -- Donald Ervin Knuth (1938-)
"If you want to master something, teach it.
The more you teach, the better you learn.
Teaching is a powerful tool to learning." -- Richard Feynman (1918-1988)
Goal
This short course is designed for students who plan to learn about common data structures with efficient algorithms, solve LeetCode problems, and understand state-of-the-art information techniques. The achievements of Algorithms Lab are listed below:
You could also use C, C++, C#, Python and JavaScript in this course.
I also plan to create a reference table to compare similarities and differences across multiple mainstream languages. You may take a look at it on this page. You are welcomed to join for this work.
I want to emphasize that with AI tools like ChatGPT and Claude available, differences in programming languages are no longer a valid reason to stop you.
You can find more information on setting up your development environment here.
Syllabus
Array
Concept check
The main memory can be regarded as an array (the biggest one).
An array occupies one continuous space in the memory.
Note that array memory allocation is actually continuous in the virtual memory.
Why is the first element of arrays starting from the array index 0?
public static void main(String[] args) {
int[] A =newint[3];
A[0] =100;
A[1] =200;
A[2] =300;
System.out.println(A[0]); // print 100.
}
/*
* This "rule" is also applicable in C, C++, C#, JavaScript, Python, PHP,
* Go, Rust, etc.
* Note that the first element of arrays starts with 1 in MATLAB, R, and
* Julia.
*/
Make sure that you understand the sketch of memory allocation, especially the stack and the heap of the memory.
Can you describe the program flow in the stack during the execution of the following code snippet?
publicstaticvoidmain(String[] args){
int x =1, y =2;
int z =max(x, y);
}
publicstaticintmax(int a,int b){
return a > b ? a : b;// Halt at this line.
/* equivalent to the following code snippet:
if (a > b)
return a;
else
return b;
*/
}
(Backtracking) is useful for constraint satisfaction problems that involve assigning values to variables according to a set of constraints… Backtracking reduces the number of possible value assignments that we consider, because it never considers invalid assignments…
Assume that there are 64 bottles. Only one of them is poisonous. Ask how many rats, at least, we need to detect which bottle is poisonous. For simplicity, one rat could drink multiple bottles which may or may not include the poisonous one.
See also Hamming code in Error Control from Princeton University.
The algorithm fails with some probability, but we can tell when it fails. In particular, we can run it again until it succeeds, which means that we can eventually succeed with probability 1 (but with a potentially unbounded running time).
The algorithm fails with some probability, but we can't tell when it fails. If the algorithm produces a yes/no answer and the failure probability is significantly less than 1/2, we can reduce the probability of failure by running it many times and taking a majority of the answers.
A young programmer is selected to participate in a ground-breaking experiment in synthetic intelligence by evaluating the human qualities of a highly advanced humanoid A.I.