What is a Data Structure β and Why is it Important?
After completing this topic:
You will be able to explain what a data structure is and understand why "how you store data" determines a program's performance.
Same Data, Different Performance
Let's consider a phone book with 1,000 names and phone numbers.
Method A: Write them randomly on a piece of paper.
- To find "Kim Hoon"? β Check one by one from the beginning. In the worst case, 1,000 times.
Method B: Sort them alphabetically.
- To find "Kim Hoon"? β Open to the middle and compare, narrowing it down by half. Find it in a maximum of 10 attempts.
The data is the same. Only the way it is stored and organized is different, but the search speed differs by a factor of 100.
This is the essence of a Data Structure β the efficiency of operations depends on how the data is organized.
Basic Types of Data Structures
Linear Data Structures (arranged in a line)
βββ Array β Stored sequentially in contiguous memory locations
βββ Linked List β Each element points to the next
βββ Stack β Last-in, first-out (LIFO)
βββ Queue β First-in, first-out (FIFO)
Non-linear Data Structures (branching structures)
βββ Tree β Hierarchical structure
βββ Graph β Free connections
βββ Hash Table β Direct access by keyWhy Can't We Just Use One?
If arrays were all-powerful, we wouldn't need other data structures. However, each structure has its strengths and weaknesses.
| Operation | Array | Linked List | Hash Table |
|---|---|---|---|
| Access by index | Fast | Slow | β |
| Search | Slow | Slow | Fast |
| Insertion/Deletion | Slow | Fast | Fast |
"This program performs a lot of searches, so use a hash table"; "Order is important, so use an array" β choosing the right data structure for the situation is up to the programmer.
Data Structure + Algorithm = Program
There is a famous formula:
Program = Data Structure + Algorithm β Niklaus Wirth (creator of the Pascal language)
A data structure is the container for data, and an algorithm is the method for processing data in that container. Using a good container with a good recipe results in an efficient program.
Why is it Important in Practice?
# Searching in a list β O(n), slow for large datasetsusers = ["Kim Hoon", "Lee Soo", "Park Jin", ...] # 1 million users"Kim Hoon" in users # Worst case: 1 million comparisons
# Searching in a dictionary (hash table) β O(1), almost instantusers = {"Kim Hoon": "010-1234", "Lee Soo": "010-5678", ...}users["Kim Hoon"] # Found in one stepWith 1 million users, choosing the right data structure can mean the difference between 1 million operations vs. 1 operation. This is why you need to learn data structures.