Run ❯
Get your
own
website
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Go to Spaces
Ctrl+Alt+P
#include
int main() { int sizes[] = {10, 100, 1000, 1000000}; for (int i = 0; i < 4; i++) { int n = sizes[i]; // Linear search: check every item int linearSteps = n; // Binary search: cut the items in half until none are left int binarySteps = 0; int items = n; while (items > 0) { items = items / 2; binarySteps++; } printf("n = %d: linear = %d steps, binary = %d steps\n", n, linearSteps, binarySteps); } return 0; }
n = 10: linear = 10 steps, binary = 4 steps
n = 100: linear = 100 steps, binary = 7 steps
n = 1000: linear = 1000 steps, binary = 10 steps
n = 1000000: linear = 1000000 steps, binary = 20 steps