Home Week-2 Week-4

PDSA - Week 3

Quick Sort

Quick sort is a sorting algorithm that uses the divide-and-conquer strategy to sort the list of elements. The basic idea of quick sort is to partition the input list into two sub-list, one containing elements that are smaller than a chosen pivot element, and the other containing elements that are greater than or equal to the pivot. This process is repeated recursively on each sub-list until the entire list is sorted.

Algorithm:

  1. Choose a pivot element from the list. This can be any element, but typically the first or last element is chosen.

  2. Partition the list into two sub-list, one containing elements smaller than the pivot, and the other containing elements greater than or equal to the pivot. Steps for Partition:-

    1. Set pivot to the value of the element at the lower index of the list L.

    2. Initialize two indices i and j to lower and lower+1 respectively.

    3. Loop over the range from lower+1 to upper (inclusive) using the variable j.

    4. If the value at index j is less than or equal to the pivot value, increment i and swap the values at indices i and j in the list L.

    5. After the loop, swap the pivot value at index lower with the value at index i in the list L.

    6. Return the index i as the position of the pivot value.

  3. Recursively apply steps 1 and 2 to each sub-list until the entire list is sorted.

 

Visualization

Select the Quick Sort(QUI) option on top bar then run the visualization.

Source - https://visualgo.net/en/sorting

 

Implementation

 

Code Execution Flow

 

Lecture Implementation

 

Code Execution Flow

 

Analysis

Best Case - In each recursive call, If the selected pivot element comes at the middle position and divide the list into two equal halves.

Recurrence relation:- T(n)=2T(n/2)+O(n)

Complexity:- n+n+n...logn times=nlogn=O(nlogn)

 

Worst Case - If the list is already sorted in ascending or descending order (if the selected pivot element comes at the first or last position of the list in each recursive call).

Recurrence relation:- T(n)=T(n1)+O(n)

Complexity:- n+(n1)+(n2)...1=n(n+1)/2=O(n2)

 

Average Case

Complexity:- n+n+n...logn times=nlogn=O(nlogn)

 

Stable - No

Sort in Place - Yes

 

Comparison of sorting algorithm

 

 

Linked List

A linked list is a data structure consisting of a sequence of nodes, where each node contains a piece of data and a reference (or pointer) to the next node in the sequence. The first node is called the head, and the last node is called the tail, and the tail node points to null. Linked lists are useful for storing and manipulating collections of data, especially when the size of the collection is not known in advance, as they can dynamically adjust in size.

There are two types of linked lists:

Singly linked list

 

Representation

Doubly linked list

 

Representation

 

Visualization of Linked List

Select the Linked List(LL) or Doubly Lined List(DLL) option on top bar then run the visualization.

Source - https://visualgo.net/en/list

 

 

Implementation of singly linked list in Python

Using one class- Recursively

 

Code Execution Flow

 

 

Using two classes

 

Code Execution Flow

 

Advantage

Disadvantage

Application

 

Stack

A Stack is a non-primitive linear data structure. It is an ordered list in which the addition of a new data item and deletion of an already existing data item can be done from only one end, known as top of the stack.

The last added element will be the first to be removed from the Stack. That is the reason why stack is also called Last In First Out (LIFO) type of data structure.

 

Basic operations on Stack

Push

The process of adding a new element to the top of the Stack is called the Push operation.

Pop

The process of deleting an existing element from the top of the Stack is called the Pop operation. It returns the deleted value.

Traverse/Display

The process of accessing or reading each element from top to bottom in Stack is called the Traverse operation.

Applications of Stack

Implementation of the Stack in Python

Stack implementation using a python list

 

Code Execution Flow

 

Stack implementation using a Linked list

 

Code Execution Flow

 

 

Queue

The Queue is a non-primitive linear data structure. It is an ordered collection of elements in which new elements are added at one end called the Back end, and the existing element is deleted from the other end called the Front end.

A Queue is logically called a First In First Out (FIFO) type of data structure.

 

Basic operations on Queue

Enqueue

The process of adding a new element at the Back end of Queue is called the Enqueue operation.

Dequeue

The process of deleting an existing element from the Front of the Queue is called the Dequeue operation. It returns the deleted value.

Traverse/Display

The process of accessing or reading each element from Front to Back of the Queue is called the Traverse operation.

Applications of Queue

Implementation of the Queue in python

Queue implementation using a python list

 

Code Execution Flow

 

 

Queue implementation using a Linked list

 

Code Execution Flow

 

 

Hash mapping

Hash mapping, also known as hash table or dictionary, is a data structure that allows for efficient insertion, deletion, and retrieval of key-value pairs.

The basic idea behind hash mapping is to use a hash function to map the key to a bucket in an array. The hash function takes the key as input and returns an index of the array where the value corresponding to the key can be stored. When we want to retrieve a value, we simply use the hash function to calculate the index and then access the value stored at that index.

One of the advantages of hash mapping is its constant-time complexity for basic operations such as insertion, deletion, and retrieval, on average, making it efficient for large datasets. However, the performance can be affected by factors such as the quality of the hash function, the number of collisions (i.e., when multiple keys map to the same index), and the size of the array.

 

For storing element

For searching element

Collision

The situation where a newly inserted key maps to an already occupied slot in the hash table is called collision.

Collision resolving technique

Open addressing(Close hashing)

Closed addressing ( Open hashing)

 

 

Visualization of Hashing

Source - https://visualgo.net/en/hashtable