0% found this document useful (0 votes)
8 views

DSA lab manuals

The document is a lab manual for the Data Structure and Algorithms course at the University of Baltistan, Skardu, detailing various data structures and sorting algorithms. It includes labs on queues, sorting methods (like bubble sort, merge sort, and selection sort), and data structures (like lists, sets, and dictionaries). Each lab provides definitions, key points, and programming examples related to the concepts discussed.

Uploaded by

faizbaksh2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DSA lab manuals

The document is a lab manual for the Data Structure and Algorithms course at the University of Baltistan, Skardu, detailing various data structures and sorting algorithms. It includes labs on queues, sorting methods (like bubble sort, merge sort, and selection sort), and data structures (like lists, sets, and dictionaries). Each lab provides definitions, key points, and programming examples related to the concepts discussed.

Uploaded by

faizbaksh2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

1

UNIVERSITY OF BALTISTAN, SKARDU

DEPARTMENT OF COMPUTER SCIENCE

LAB MANUAL

Data Structure and Algorithms

SUBMITTED TO: SIR GHULAM HUSSAIN

SUBMITTED BY: FAIZULLAH MUHAMMAD

REG NO: S23BSCS100

SEMESTER: 3rd

SECTION: A

SESSION: SPRING (2023-2027)


2
3

Table Contents

Lab 1 Queue

Lab 2 Sorting

Lab 3 Insertion Sort

Lab 4 Selection Sort...................................................................................4

Lab 5 Merge Sort........................................................................................5

Lab 6 Bubble Sort.......................................................................................6

Lab 7 Shell Sort...........................................................................................7

Lab 8 Heap Sort..........................................................................................8

Lab 9 Binary Search...................................................................................9

Lab 10 Tuple.................................................................................................10

Lab 11List.....................................................................................................11

Lab 12Set......................................................................................................12

Lab 13Dictionary.........................................................................................13

Lab 14Linked Lists......................................................................................14

Lab 15Concept of OOP...............................................................................15

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:

 Enqueue: Add an item to the back of the queue.


 Dequeue: Remove an item from the front of the queue.
5

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]

After sorting (ascending): [1, 3, 5, 8]

How to Sort:

Python provides built-in ways to sort data:

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:

1. A sorted part (starts empty).


2. An unsorted part (starts with all elements).

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.

How Merge Sort Works:

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.

Combine: Continue merging until there’s only one sorted list.

Key Points:

Efficient: Works well for large datasets.

Stable: Keeps the relative order of equal elements.

Requires extra space: Because it creates temporary lists during merging


9

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.

Named after: Its inventor, Donald Shell (1959)


12

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:

1. Check the middle item of the list.


2. If it’s the item you’re looking for, you’re done!
3. If your item is smaller, search the left half; if it’s larger, search the right half.
4. Repeat this process until you find the item or the list can’t be divided anymore.

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:

 Created by putting items inside parentheses ().


 Can hold different types of items (e.g., numbers, strings).
 Useful when you want to group related data together but don't need to modify it.

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:

 Created using square brackets [].


 Can store different types of items (e.g., numbers, strings, other lists).
 Items in a list are ordered, so you can access them by their position (index).

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:

 Unordered: The items do not have a specific order.


 Unique: Duplicate items are not allowed.
 Mutable: You can add or remove items after the set is created.
 Supports mathematical operations: You can perform operations like union, intersection,
and difference.

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:

 Keys: Unique identifiers (can be strings, numbers, etc.).


 Values: The data associated with the keys (can be any data type).
 Unordered: The items are not stored in a specific order.

Program:

Output:

Lab 14:
19

Linked list: A linked list is a collection of items called nodes, where each node contains two
parts:

1. Data: The actual value or information.


2. Next: A pointer (reference) to the next node in the sequence.

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

Concept of the oop in python:

Object-Oriented Programming (OOP) in Python is a programming paradigm that organizes


code into objects. These objects represent real-world entities and have properties (called
attributes) and behaviors (called methods).

 Classes
 Objects
 Encapsulation
 Inheritance
 Abstraction
 Polymorphism

Programs: to define class and object in python:

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:

 Ordered: The items are stored in a specific sequence.


 Indexed: Each item has an index, starting from 0 (the first item is at index 0, the
second at index 1, and so on).
 Fixed size (in some languages): In languages like C or Java, arrays have a fixed size,
meaning the number of elements is determined when the array is created. In Python, lists
(which are like arrays) can grow or shrink dynamically.

Program:

Output:

You might also like