
Arrays and Hash Tables
1. What Do We Mean by Data Organization?
In computer science, data organization means the way we arrange and store data so that it can be used efficiently.
For example:
- A list of student roll numbers can be stored in order (like 1, 2, 3, 4).
- A dictionary can store word–meaning pairs, where each word is a key and the meaning is the value.
2. Concept of Sequential Organization
Sequential organization means storing data one after another in order, just like books on a shelf.
Example: Marks of 5 students 45, 67, 89, 76, 55
are stored one after another:
Index | Value |
---|---|
0 | 45 |
1 | 67 |
2 | 89 |
3 | 76 |
4 | 55 |
3. Linear and Non-Linear Data Structures
Linear Data Structures: Elements are stored one after another. Examples: Arrays, Linked Lists, Stacks, Queues.
Non-Linear Data Structures: Elements are connected in a non-sequential way. Examples: Trees, Graphs.
4. Storage Representation
- Sequential Representation: Data is stored in continuous memory. (Example: Arrays)
- Linked Representation: Data is stored randomly with pointers. (Example: Linked Lists)
5. Arrays – The Basics
An array stores elements of the same type in continuous memory.
int marks[5] = {45, 67, 89, 76, 55};
- Fixed size
- Same data type
- Random access using index
6. Operations on Arrays
- Traversal: Visit each element.
- Insertion: Add a new element (needs shifting).
- Deletion: Remove an element (needs shifting).
- Searching: Linear search, Binary search.
- Sorting: Bubble sort, Quick sort, etc.
7. Hash Tables – The Basics
A hash table stores data as key-value pairs. It uses a hash function to find the storage location.
Example:
Roll No | Name |
---|---|
101 | Amit |
102 | Riya |
103 | Neha |
8. How Does a Hash Table Work?
A hash function converts the key into an index. For example: index = key % table_size
.
9. Collision in Hash Tables
When two keys map to the same index, a collision happens.
Collision Resolution:
- Open Addressing (linear probing, quadratic probing, double hashing)
- Chaining (linked list at each index)
10. Advantages and Disadvantages of Hash Tables
- Advantages: Very fast, stores key-value pairs, average O(1) search.
- Disadvantages: Collisions reduce speed, more memory, hash function design is tricky.
11. Arrays vs Hash Tables
Feature | Arrays | Hash Tables |
---|---|---|
Storage | Sequential | Hash-based |
Access | By index | By key |
Speed | Fast (index known) | Faster (average O(1)) |
Flexibility | Fixed size | Dynamic |
Use Case | Ordered data | Key-value pairs |
12. Real-Life Examples
- Arrays: Student marks, days of the week, temperature readings.
- Hash Tables: Dictionary (word → meaning), phone book (name → number), student database (roll → details).
13. Conclusion
Arrays and hash tables are fundamental building blocks in computer science. Arrays are simple and ordered, while hash tables give very fast searching using keys. Understanding them is essential for learning advanced data structures and algorithms.
Back to Top