Back

Algorithm

What is an Algorithm?

An algorithm is a step-by-step procedure used to solve a problem or perform a task.

πŸ”§ In computer science, every task that a computer needs to perform is treated as a problem that must be solved using a sequence of steps.

Example

For example, when you right-click with a mouse, the computer must decide what actions should occur (such as opening a context menu).

To make this happen, programmers design an algorithm that defines the steps the computer follows.

Complex Example

Consider creating an AI robot that can perform heart surgery autonomously.

Researchers might design several possible systems:

  • AI Model 1
  • AI Model 2
  • AI Model 3

Each system is tested and evaluated. The system that produces results closest to the desired outcome is selected.

πŸ”§ Clarification:

The step-by-step process that the chosen system follows to produce the correct result is considered the algorithm.


Characteristics of an Algorithm

An algorithm has several important properties:

1. πŸ”§ Definiteness

Each step must be clear, precise, and unambiguous.

2. Finiteness

The algorithm must terminate after a finite number of steps.

3. Effectiveness

Each step must be simple enough to be performed by a computer in a reasonable amount of time.

4. Input

An algorithm can take zero or more inputs.

πŸ”§ Correction:

Input is not just a cause for the CPU to start working.

Instead, it is data that the algorithm receives for processing.

Examples:

  • numbers
  • text
  • user actions (mouse click, keyboard input)

5. Output

An algorithm must produce at least one result.

Output can be:

  • data
  • a displayed result
  • a performed action
  • a change in the system

Processing

Processing refers to the operations performed on data by the computer.

Examples include:

  • calculations
  • comparisons
  • data analysis
  • decision making
  • logical operations

πŸ”§ Clarification:

A computer does not β€œthink” like humans, but it performs logical operations that simulate decision making.


Data Structure

What is a Data Structure?

A data structure is a way of organizing and storing data so that it can be used efficiently.

πŸ”§ Improved Definition:

A data structure defines how data is arranged in memory and how it can be accessed or modified.

Purposes of Data Structures

Data structures help with:

  • storing data efficiently
  • organizing data
  • searching data
  • sorting data
  • modifying or manipulating data
  • performing computations on data

Examples of Data Structures

πŸ”§ Added important examples:

  • Arrays
  • Linked Lists
  • Stacks
  • Queues
  • Trees
  • Graphs
  • Hash Tables

Each structure is designed to optimize certain types of operations.


Relationship Between Algorithms and Data Structures

πŸ”§ Important concept added:

Algorithms + Data Structures = Efficient Programs

  • Data structures organize the data.
  • Algorithms define the steps used to process that data.

Example:

  • A search algorithm works on a data structure like an array or tree.