Arrays  and Hashing

What is an Array?

What is an Array?

An Array is a collection of items of the same data types with each item indexed starting with 0.

WHY ARE ARRAYS IMPORTANT ?

WHY ARE ARRAYS IMPORTANT ?

  • It’s one of the most popular and simple data structures and is often used to implement other data structures

  • Implementing data structures such as stacks and queues

  • Representing data in table and matrices

  • Creating dynamic data structures like linked list and trees

Accessing Array Index

Accessing Array Index

We access array index using square brackets notation 

[...]

Example

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

Types of Arrays

There are different types of Arrays but we will be focusing on just two

Static Arrays

Dynamic Arrays

An array with a fixed size, meaning the number of elements is set when the array is created and cannot be changed later on.

Above we have an array of integers of fixed length 5

Only traversal can be performed. 

int arr[5] = {1, 2, 3, 4, 5}

STATIC ARRAYS

DYNAMIC ARRAYS

An array that can change in size during runtime

Above we have a dynamic array of integers

Traversal, Insertion and Deletion operations can be performed. 

arr = [1, 2, 3, 4, 5]

.append()

INSERTION OPERATOR

Example

cars = ["Ford", "Volvo", "BMW"]

cars.append("Honda")

print(cars)

#Result

["Ford", "Volvo", "BMW", "Honda"]

Items are usually added to the ending of the arry

Time Complexity: O(1)

.pop()

DELETION OPERATOR

Example

cars = ["Ford", "Volvo", "BMW"]

cars.pop()

print(cars)


#Result

["Ford", "Volvo"]

Items are usually deleted from the end of the array

Time Complexity: O(1)

But that's not all for Deletion operator

We can also delete an Item at a particular index by passing the index at an argument to the operation

cars = ["Ford", "Volvo", "BMW"]

cars.pop(1)

print(cars)


#Result

["Ford", "BMW"]

Time Complexity: O(n)

where n is the index passed as argument

ARRAY ALGORITHMS

ARRAY ALGORITHMS

Remember what an Algorithm is ?

A step by step procedure to perform a specific task

ARRAY ALGORITHMS

We would be considering 2 types of task

SEARCHING and SORTING

Searching Algorithms

We have various types of searching algorithms in the real world

Some common ones include:

  • Binary search
  • Linear search

We would be looking more into them as we go on in this challenge

Sorting Algorithms

Same as searching, we have various types of sorting algorithms 

Some common ones include:

  • Bubble sort
  • Quick sort
  • Merge sort

We would be looking more into them as we go on in this challenge

WHAT IS HASHING?

WHAT IS HASHING?

From geeksforgeeks

???

WHAT IS HASHING?

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

WHAT ARE HASH FUNCTIONS?

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

TYPES OF HASH FUNCTIONS?

Hash functions are programmed based of requirements 

Here we have some (3) popular hashing functions 

Mid Square Method

Folding Method

Division Method

Structure of Hash Map or Table

Hash value Value

Address

Value

Using the division method hash funtion

L = k mod x

Hash value (key)

any division number

item

Using the division method hash funtion

L = k mod x

Given an array [2, 3, 4]

Using 2 as divisor

1st iteration

2 mod 2 = 0

Hash map

Hash value Value
0 2

2nd iteration

3 mod 2 = 1

Hash map

Hash value Value
0 2
1 3

3rd iteration

4 mod 2 = 0

Hash map

Hash value Value
0 2
1 3

The resulting hash value is 0, however there is already a value assigned to hash value 0

We have a problem

This is called Collision

Collision 

A collision occurs when two different inputs produce the same hash value.

Collision 

Collisions naturally happen in hashing because there are often more possible inputs (values) than there are available hash values.

More Examples Collision (2)

Two different keys, 'apple' and 'banana,' both being mapped to the same index in a hash table.

In a group of 13 people, at least 2 people would have the same birth month 

Months of the year 12 (hash values) < group 13 (inputs)

Collision Resolution

Here we have some (3) popular collision resolution methods 

Process of resolving collisions

Linear Probing

Quadratic Probing

Double Hashing

Using double Hashing to solve our division method problem

Double Hashing refers to running another hash function within the field with collision

Double Hashing

4 mod 2 = 0

Hash value Value
0 2, 4
1 3

3rd iteration

Double Hashing

index mod 3 = hash value

Hash value Value
1 4
2 2

[2, 4] 

Hash map

Using 3 as divisor

Double Hashing

Finally we have our complete hash table as

Hash value Value
1 4
2 2

Hash map

Hash value Value
0
1 3

Further Reading and Relevant Links

MIT 6.006 Introduction to Algorithms: Hashing

MIT 6.006 Introduction to Algorithms: Arrays

Tutorial Point Introduction to DSA: Hashing

Ready to solve Leetcode challenges on Arrays and Hashing ?

Thank you !!!!

S.I.T: DSA - Arrays and Hashing

By Chineletam Ugwuadu (Letam)

S.I.T: DSA - Arrays and Hashing

  • 263