What is an Array?
WHY ARE ARRAYS IMPORTANT ?
We access array index using square brackets notation
[...]
cars = ["Ford", "BMW", "Honda"]
print(cars[0]) #Result: "Ford"
print(cars[1]) #Result: "BMW"
print(cars[2]) #Result: "Honda"We have an array of length 3
Remember our computers count index from 0
There are different types of Arrays but we will be focusing on just two
Only traversal can be performed.
int arr[5] = {1, 2, 3, 4, 5}Traversal, Insertion and Deletion operations can be performed.
arr = [1, 2, 3, 4, 5]cars = ["Ford", "Volvo", "BMW"]
cars.append("Honda")
print(cars)
#Result
["Ford", "Volvo", "BMW", "Honda"]Time Complexity: O(1)
cars = ["Ford", "Volvo", "BMW"]
cars.pop()
print(cars)
#Result
["Ford", "Volvo"]Time Complexity: O(1)
cars = ["Ford", "Volvo", "BMW"]
cars.pop(1)
print(cars)
#Result
["Ford", "BMW"]Time Complexity: O(n)
where n is the index passed as argument
Remember what an Algorithm is ?
A step by step procedure to perform a specific task
We would be considering 2 types of task
SEARCHING and SORTING
We have various types of searching algorithms in the real world
Some common ones include:
We would be looking more into them as we go on in this challenge
Same as searching, we have various types of sorting algorithms
Some common ones include:
We would be looking more into them as we go on in this challenge
WHAT IS HASHING?
From geeksforgeeks
Hashing can be explained as giving a bunch of items different addresses for better locating and retrieving of these items later on
WHAT IS HASHING?
Hashing can be thought of as a process where each item in a collection is given a unique "index" or "address" based on a specific algorithm, known as a hash function. This index allows you to quickly locate and retrieve the item later on, making searching much faster.
Chat GPT
An Hash function is a function that maps out an address for a given item following a specific rule. This address (Key) mapped to the corresponding item (value) is what forms the
Hash map OR Hash table
Hash functions are programmed based of requirements
Here we have some (3) popular hashing functions
| Hash value | Value |
|---|---|
Address
Value
Hash value (key)
any division number
item
Using 2 as divisor
| Hash value | Value |
|---|---|
| 0 | 2 |
| Hash value | Value |
|---|---|
| 0 | 2 |
| 1 | 3 |
| Hash value | Value |
|---|---|
| 0 | 2 |
| 1 | 3 |
The resulting hash value is 0, however there is already a value assigned to hash value 0
Process of resolving collisions
| Hash value | Value |
|---|---|
| 0 | 2, 4 |
| 1 | 3 |
| Hash value | Value |
|---|---|
| 1 | 4 |
| 2 | 2 |
Using 3 as divisor
| Hash value | Value |
|---|---|
| 1 | 4 |
| 2 | 2 |
| Hash value | Value |
|---|---|
| 0 | |
| 1 | 3 |
MIT 6.006 Introduction to Algorithms: Hashing
MIT 6.006 Introduction to Algorithms: Arrays
Tutorial Point Introduction to DSA: Hashing