An algorithm is a finite sequence of well-defined steps that takes some input and produces an output, solving a particular problem. It is independent of any one programming language; the same algorithm can be written in Python, C or plain English.
Why it matters
The choice of algorithm often matters more than how fast the hardware is. Efficiency is described with Big O notation, which shows how running time grows with input size. Searching a sorted list of one million items by binary search takes about 20 comparisons, while a linear scan may need up to one million.
Example
Binary search on a sorted list:
def find(sorted_list, target):
lo, hi = 0, len(sorted_list) - 1
while lo <= hi:
mid = (lo + hi) // 2
if sorted_list[mid] == target:
return mid
if sorted_list[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1
Each step halves the remaining range, so the work grows logarithmically.
Common mistakes
- Optimizing before measuring; a simple algorithm is fine for small inputs.
- Ignoring edge cases such as empty input or duplicates.
- Using binary search on data that is not sorted.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.