Before we dive into the fascinating world of algorithms, we need to understand the fundamental building blocks that algorithms operate on: data types. Think of data types as the different kinds of information we can store and manipulate. In programming, we broadly categorize these into primitive and non-primitive data types. Mastering this distinction is crucial for understanding how data is represented and processed, which directly impacts algorithmic efficiency.
Primitive data types are the most basic, indivisible units of data. They are built directly into the programming language and represent single values. Their behavior is well-defined and they are generally very efficient to work with because the computer hardware can directly operate on them.
Common primitive data types include:
- Integers: Whole numbers, both positive and negative (e.g., -10, 0, 42).
- Floating-point numbers (Floats): Numbers with a decimal point (e.g., 3.14, -0.5, 1.0).
- Booleans: Represent truth values, either
trueorfalse. - Characters: Single letters or symbols (e.g., 'a', '$', '7').
let age = 30;
let price = 19.99;
let isComplete = true;
let initial = 'A';Non-primitive data types, also known as composite or reference types, are more complex. They are built using primitive data types and/or other non-primitive data types. Instead of holding a single value directly, they typically hold a reference to a location in memory where the actual data is stored. This allows them to represent collections of data or more abstract structures.
Examples of non-primitive data types include:
- Strings: Sequences of characters (e.g., "Hello, World!"). While a string is composed of characters, it's treated as a single unit.
- Arrays: Ordered collections of elements of the same or different data types.
- Objects/Structs: Collections of named properties, where each property can hold a value (primitive or non-primitive).
- Lists/Linked Lists: Dynamic collections of elements.
- Trees: Hierarchical data structures.
- Graphs: Collections of nodes and edges representing relationships.