DSA lab manuals
DSA lab manuals
LAB MANUAL
SEMESTER: 3rd
SECTION: A
Table Contents
Lab 1 Queue
Lab 2 Sorting
Lab 10 Tuple.................................................................................................10
Lab 11List.....................................................................................................11
Lab 12Set......................................................................................................12
Lab 13Dictionary.........................................................................................13
Lab 16Array.................................................................................................16
4
Lab 1:
Queue: A queue is a data structure that works like a line of people waiting their
turn. The first person in the line is served first, and new people join at the end. This
behavior is called FIFO (First In, First Out).
Operations in a Queue:
Lab 2:
Sorting:
Sorting means arranging a list (or other data structure) in a specific order, such as ascending
(smallest to largest) or descending (largest to smallest).
For example:
Before sorting: [5, 3, 8, 1]
How to Sort:
1. Using sorted (): Creates a new sorted list without changing the original.
2. Using sort (): Sorts the list in place (modifies the original list).
Program:
Output:
6
Lab 3:
Types of sorting:
Insertion sort:
Insertion Sort is a simple sorting technique that works like organizing playing cards in
your hand. You take one card at a time and place it in its correct position among the cards
you've already sorted.
Program:
Output:
7
Lab 4:
Selection sort:
Selection Sort is a simple sorting algorithm that works by repeatedly finding the smallest
element from the unsorted part of the list and swapping it with the first unsorted element. It
divides the list into two parts:
Program:
Output:
8
Lab 5:
Merge sort:
Merge Sort is a divide-and-conquer sorting algorithm. It works by dividing the list into smaller
parts, sorting each part, and then merging the sorted parts back together to get the final sorted list.
Divide: Split the list into two halves repeatedly until each half has one element (or no elements,
which is already sorted).
Conquer: Merge the smaller sorted lists into larger sorted lists by comparing elements.
Key Points:
Program:
Output:
10
Lab 6:
Bubble sort:
Bubble sort is a simple sorting method where you repeatedly go through a list of items,
compare each pair of neighboring items, and swap them if they are in the wrong order. This
process continues until the entire list is sorted.
Program:
Output:
11
Lab 7:
Shell sort:
Shell sort is a sorting method that improves on simpler methods like bubble sort. Instead of
comparing and swapping items one by one, it first sorts far-apart items and then gradually
reduces the gap between the items being compared.
By starting with large gaps and making them smaller, Shell sort moves items closer to their
correct positions faster, making the final sorting quicker. It’s like organizing a messy pile of
books by first roughly grouping them and then fine-tuning the order.
How it works:
It divides the list into smaller sublists based on a gap value, sorts those sub
lists, and gradually reduces the gap until it becomes 1 (like insertion sort).
Efficiency:
Faster than basic sorts (like bubble sort) for larger datasets but slower than
advanced sorts (like quicksort)
Key advantage:
Works well for medium-sized lists and performs fewer comparisons and
swaps than simpler sorting methods.
Program:
Output:
13
Lab 8:
Heap sort:
Heap sort is a sorting method that uses a special tree-like structure called a heap to organize data.
It works in two main steps:
Build a heap from the data, which ensures the largest (or smallest) value is at the top.
Remove the top value (the largest or smallest), place it in the sorted part of the list, and adjust the
heap. Repeat this until everything is sorted.
Program:
Output:
14
Lab 9:
Binary search:
Binary search is a quick way to find a specific item in a sorted list. It works by repeatedly
dividing the list into halves:
Program:
Output:
15
Lab 10:
Tuple:
A tuple is a type of data structure in Python that stores multiple items in a single, ordered
collection. It's like a list, but with one big difference: tuples cannot be changed once they are
created (they are immutable).
Key Points:
Program:
Output:
16
Lab 11:
List:
A list in Python is a flexible data structure used to store multiple items in a single, ordered
collection. Unlike tuples, lists can be changed (they are mutable).
Key Points:
Program:
Output:
Lab 12:
17
Concept of Set:
A set in Python is a collection of unique, unordered items. Unlike lists or tuples, sets do not
allow duplicate elements. They are commonly used when you need to store items without
duplicates and don’t care about the order.
Key Points:
Program:
Output:
Lab 13:
18
Dictionary:
A dictionary in Python is a collection of key-value pairs, where each key is unique, and it is used
to access the corresponding value. You can think of it like a real-world dictionary, where you
look up a word (key) to find its definition (value).
Key Points:
Program:
Output:
Lab 14:
19
Linked list: A linked list is a collection of items called nodes, where each node contains two
parts:
The nodes are connected in a chain-like structure, and you can only access each node by
following the "next" links from one node to the next.
Key Points:
Dynamic size: The size of a linked list can grow or shrink as needed.
Types: There are different types of linked lists, such as singly linked lists, doubly linked
lists, and circular linked lists.
Program:
Output:
Lab 15:
20
Classes
Objects
Encapsulation
Inheritance
Abstraction
Polymorphism
Output:
Encapsulation in python:
21
Encapsulation:
Encapsulation is the concept of hiding the internal details of an object and only exposing the
necessary functionality. It helps in protecting the data from unauthorized access and modification.
Example: Using private variables and public methods (getters and setters) to control access to an
object's attributes.
Program:
Output:
Inheritance in python:
22
Inheritance allows a new class (called a subclass) to inherit the attributes and methods of an
existing class (called a superclass). This promotes code reuse.
Example: An ElectricCar class could inherit from the Car class, adding extra
functionality for electric-specific features while keeping all general car behavior.
Program:
Output:
Polymorphism in python:
23
Polymorphism means that different classes can have methods with the same name, but each
class can implement the method in its own way.
Program:
Output:
Abstraction in python:
24
Abstraction involves hiding the complex implementation details and exposing only the essential
features of an object.
Program:
Output:
Lab 16:
Arrays:
25
An array is a collection of items (such as numbers, strings, or other data types) stored together
in a specific order. Each item in the array is called an element, and each element has a specific
position, called an index.
Key Points:
Program:
Output: