השיחה עם chatGPT:
<iframe width="800" height="600" src="https://chat.openai.com/share/2f5a54f1-054a-4f23-bfca-501c3288d16e"></iframe>הקוד שקיבלתי מchatGPT:
public class TimeComplexityExercise {
public static void main(String[] args) {
for (int i = 0; i < N; i += 2) {
System.out.println("Hello world");
}
for (int i = 0; i < Math.sqrt(N); i++) {
for (int j = 0; j < N; j += 3) {
System.out.println("Hello world");
}
}
for (int i = 0; i < Math.log(N); i++) {
for (int j = 0; j < Math.pow(N, 0.25); j++) {
System.out.println("Hello world");
}
}
for (int i = 0; i < N; i *= 3) {
System.out.println("Hello world");
}
for (int i = 0; i < Math.sqrt(N); i++) {
for (int j = 0; j < Math.sqrt(N); j++) {
System.out.println("Hello world");
}
}
}
}
חישוב פונקציית זמן ריצה
loop | inner loop | line | |
---|---|---|---|
N/2 + 1 | for (int i = 0; i < N; i += 2) { | ||
N/2 | System.out.println("Hello world"); | ||
sqrt(N) + 1 | for (int i = 0; i < Math.sqrt(N); i++) { | ||
sqrt(N) * (N/3 + 1) |
N/3+1 | for (int j = 0; j < N; j += 3) { | |
sqrt(N) * N/3 |
N/3 | System.out.println("Hello world"); | |
log(N) + 1 | for (int i = 0; i < Math.log(N); i++) { | ||
log(N) * (pow(N, 0.25) + 1) |
pow(N, 0.25) + 1 | for (int j = 0; j < Math.pow(N, 0.25); j++) { | |
log(N) * pow(N, 0.25) |
pow(N, 0.25) | System.out.println("Hello world"); | |
log_3(N) + 1 | for (int i = 1; i < N; i * = 3) { |
changed i = 1 for valid loop condition | |
log_3(N) | System.out.println("Hello world"); | ||
sqrt(N) + 1 | for (int i = 0; i < Math.sqrt(N); i++) { | ||
sqrt(N) * (sqrt(N) + 1) |
sqrt(N) + 1 | for (int j = 0; j < Math.sqrt(N); j++) { | |
sqrt(N) * sqrt(N) |
sqrt(N) | System.out.println("Hello world"); |
חישוב חסם עליון של פונקציית זמן ריצה:
<iframe width="800" height="600" src="https://chat.openai.com/share/9282e3c8-0746-4946-9219-f65d91937870"></iframe> $$ \text{T(N)} = \sqrt{ N } \cdot\left( \cancel{ 2 }+\frac{2\cdot N}{3} \right)+2\cdot \log N\cdot(\cancel{ 1 }+\sqrt[4]{ N })+2\cdot \log_3{N}+2\cdot \sqrt{ N }+3N+5 $$ $$ \text{T(N)}=\cancel{ \frac{2}{3} }\cdot N^{1.5}+\cancel{ 2\cdot \log N\cdot \sqrt[4]{ N } }+\cancel{ 2\cdot \log_3{N} }+\cancel{ 2\cdot \sqrt{ N } }+\cancel{ 3N }+\cancel{ 5 } $$ $$ O(N^{1.5}) $$<iframe width="800" height="600" src="https://chat.openai.com/share/53132c7e-1b75-4c82-9864-05b07cd73b8a"></iframe>
$$O(e^{0.5x})$$ $$O(e^x)$$ $$O(e^{2x})$$
<iframe width="800" height="600" src="https://chat.openai.com/share/fc15cafc-8b15-4383-a316-09a441924fb3"></iframe>
סיכום: כאשר אלגוריתם עובר כל המערך המקרה הטוב והרע יהיו בדרך כלל שווים.
<iframe width="800" height="600" src="https://chat.openai.com/share/d85f1094-3690-4f57-a045-9909a6dbaca0"></iframe>