Introduction to data structures and algorithms with Python

ngazz

Joseph Ngahu

Posted on February 21, 2022

Introduction to data structures and algorithms with Python

Data structure
is a named location which is used to store data in an organized way.
Algorithm
are sequence of steps to be followed in order to perform a particular task

why data structure and algorithm

Data structure and algorithm are essential for a number of tasks; this includes:
Data search incase where there is a massive or large volume of data and particular record is needed.
To enhance multiple requests in cases where many people want to access a resource.
In enhancing processor speed as data grows the processing speed also reduces too.

Categories of Algorithms

  • Search − Algorithm to search an item in a data structure.
  • Sort − Algorithm to sort items in a certain order.
  • Insert − Algorithm to insert item in a data structure.
  • Update − Algorithm to update an existing item in a data structure.
  • Delete − Algorithm to delete an existing item from a data structure.

Search

def search(arr, n, x):

    for i in range(0, n):
        if (arr[i] == x):
            return i
    return -1

arr = [2, 3, 4, 10, 40]
x = 10
n = len(arr)
result = search(arr, n, x)
if(result == -1):
    print("Element is not present in array")
else:
    print("Element is present at index", result)

Enter fullscreen mode Exit fullscreen mode

Sorting


def bubblesort(list):

# Swap the elements to arrange in order
   for iter_num in range(len(list)-1,0,-1):
      for idx in range(iter_num):
         if list[idx]>list[idx+1]:
            temp = list[idx]
            list[idx] = list[idx+1]
            list[idx+1] = temp
list = [19,2,31,45,6,11,121,27]
bubblesort(list)
print(list)
Enter fullscreen mode Exit fullscreen mode

Insert

def insertionSort(array):

    for step in range(1, len(array)):
        key = array[step]
        j = step - 1

        while j >= 0 and key < array[j]:
            array[j + 1] = array[j]
            j = j - 1

        # Place key at after the element just smaller than it.
        array[j + 1] = key


data = [9, 5, 1, 4, 3]
insertionSort(data)
print('Sorted Array in Ascending Order:')

print(data)
Enter fullscreen mode Exit fullscreen mode

Update

list1 = [1, 2, 3]
list2 = [5, 6, 7]
list3 = [10, 11, 12]

set1 = set(list2)
set2 = set(list1)

set1.update(set2)

print(set1)

set1.update(list3)
print(set1)
Enter fullscreen mode Exit fullscreen mode

Delete

myList = ["Bran",11,22,33,"Stark",22,33,11]

myList.remove(22)

myList
Enter fullscreen mode Exit fullscreen mode

Data structure types

Every programmer should be familiar with the following data structures:

1. Arrays
One of the simplest data structures, an array is a collection of items that are stored sequentially. An array contains values or variables—known as “elements”—of the same data type and is of a fixed size, so you cannot change the size of an array. Each item in an array is indexed starting with 0.

The best way to think about an array is like a weekly medication organizer. It includes small containers lined up in a sequence, and each container has elements inside.

Arrays are commonly used as structures for building other, more complicated data structures. They are also used for sorting algorithms.

2. Linked Lists
A linked list is a sequence of items arranged in a linear order all connected to each other. This means you must access data in order, so random access to data is not possible.

Each element in a linked list is called a “node,” and each node contains a key and a pointer. The pointer directs you to the next node, called a “next.” The sequence starts with a “head,” which directs you to the first element within the list. The last element of this list is known as the “tail.”

You can create a singly linked list, which lets you traverse each item in a forward direction from the head to the tail. Similarly, you can create a doubly-linked list, which can be traversed both forward and backward. And finally, you can create a circular linked list in which the next pointer of the tail points to the head and vice versa, forming a circle.

Linked lists are used for symbol table management in switching between programs using Alt + Tab (On a PC).

3. Stacks
A stack works almost exactly as it sounds. It’s like stacking elements within a tall container.

Stacks are known as LIFO (Last In First Out) structures. This means the element placed last can be accessed first. You can “push” a new element onto the top of the stack, or you can “pop,” deleting the element inserted last which is at the top of the stack.

Stacks are commonly used for parsing and evaluating mathematical expressions and to implement function calls in recursion programming.

4. Queues
A queue functions similarly to a stack, but instead of being a LIFO structure, it is a FIFO (First In First Out) structure. The easiest way to think about a queue is to think of a line of people waiting to enter a building. The person at the beginning of the line will enter the building first, while the person at the end will enter last.

You can enqueue an element in this structure, which means inserting the element to the end of the queue. You can also dequeue an element, which means deleting an element from the beginning of the queue.

Queues are often used to manage threads in multithreading, and they are (not surprisingly) used to implement priority queuing systems.

5. Hash Tables
A hash table structure associates each value with a key and then stores them. This makes it easy to look up values efficiently using a key. It’s an efficient way to insert and search for data regardless of its size, as it makes it easy to identify a specific object from a group of similar objects.

For example, if you go to college, you may be assigned a unique student ID number. This ID number is a key that can be used to retrieve information about you and your student record.

A hash table uses what’s known as a “hash function” to map a data set of any size to one of a fixed size—the hash table. The values that a hash function returns are known as “hash values.”

Hash tables are commonly used to create database indexes, to create associative arrays and to create a “set.”

6. Trees
A tree is a structure similar to a linked list because each item is linked. But in a tree items are linked in a hierarchal fashion, just like you might see in a visual representation of someone’s family tree. There are various types of trees, each suited to different applications.

For example, a binary search tree (BST) stores data in sorted order with every node in the binary comprised of the following attributes:

Key (the value saved in the node)
Left (pointer to the left child node)
Right (pointer to the right child node)
P (pointer to the parent node)
Binary search trees are used in many different types of search applications. Other types of trees are used in wireless networking and to create expression solvers.

7. Heaps
Similarly, a heap is a type of binary tree in which the parent nodes are compared to their children. This allows the values within the nodes to be arranged accordingly. Heaps can be represented as trees, but they can also be represented as binary arrays.

There are two types of heaps. In a min heap, the parent’s key is less than or equal to the keys of its children. In a max heap, the parent’s key is greater than or equal to the keys of its children.

Heaps are often used in algorithms to create priority queues, and to find the smallest or largest value in an array.

8. Graphs
A graph is an abstract, non-linear data structure that is made of a finite set of nodes that are connected by edges. The nodes may be referred to as “vertices” and contain values, whereas the edges are simply lines or arcs that connect two nodes in the graph.

Graphs are often used to represent networks, such as circuit networks or even paths in a city. They're great for solving real-world problems, but they can also be used as representations of digital networks.

For example, on Facebook, each user could be represented with a node (or vertex). Each vertex could then contain information about that user, and each edge could represent their connection with another user.

💖 💪 🙅 🚩
ngazz
Joseph Ngahu

Posted on February 21, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related