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

DSA Lab 5

The lab manual outlines the implementation of Stack Abstract Data Type (ADT) and sorting algorithms, specifically Selection Sort and Insertion Sort. Students will learn to implement stack operations like push and pop, and sorting techniques through coding exercises in C++. The document includes theoretical explanations, algorithms, and sample code for practical implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DSA Lab 5

The lab manual outlines the implementation of Stack Abstract Data Type (ADT) and sorting algorithms, specifically Selection Sort and Insertion Sort. Students will learn to implement stack operations like push and pop, and sorting techniques through coding exercises in C++. The document includes theoretical explanations, algorithms, and sample code for practical implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

LAB MANUAL Data Structures and Algorithms

LAB NO. 5 08/ 10 / 2024

Implementation of Stack ATD.


Lab outcomes:
After completing this lab, students will be able to.

 Implementation of Stack ADT.

Corresponding CLO and PLO:


 CLO-1, PLO-5 (Modern Tool Usage)

Tools:
 Eclipse IDE
 JDK

Theory:
Stack: It is an ordered collection of data elements into which new elements may be inserted and from
which elements may be deleted at one end called the “TOP” of stack.
 A stack is a last-in-first-out (LIFO) structure.
 Insertion operation is referred as “PUSH” and deletion operation is referred as “POP”.
 The most accessible element in the stack is the element at the position “TOP”.
 Stack must be created as empty.
 Whenever an element is pushed into stack, it must be checked whether the stack
is full or not.
 Whenever an element is popped form stack, it must be checked whether the
stack is empty or not.
 We can implement the stack ADT either with array or linked list.

Procedure:
1. Algorithm Push:
Step 1: if top> =max-1 then
Step 2: Display the stack
overflows
Step 3: else then
LAB MANUAL Data Structures and Algorithms

Step 4: top ++
Step 5: assign stack[top]=x
Step 6: Display element is inserted
2. Algorithm Pop:
Step 1: if top = =-1 then
Step 2: Display the stack is
underflows
Step 3: else
Step 4: assign
x=stack[top]
Step 5: top- -
Step 6: return x

3. Algorithm Peek:

begin procedure peek


return stack[top]
end procedure

4. Algorithm IsFull:
begin procedure isfull

if top equals to MAXSIZE


return true
else
return false
endif

end procedure
Tasks:
Make functions implement below stack operations
Task-1: Implementation for Push Code:
#include <iostream>
#define MAX 100
using namespace std; //Huzaifa Ali 59384
class Stack {
private:
int arr[MAX];
int top;
public:
Stack() {
top = -1;
}
void push(int value) {
if (top >= MAX - 1) {
LAB MANUAL Data Structures and Algorithms

cout << "Stack overflow! Cannot push " << value << endl;
} else {
top++;
arr[top] = value;
cout << value << " pushed onto the stack." << endl;
}
}
void display() {
if (top == -1) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack stack;
int choice, value;
cout << "Enter the first element to push into the stack: ";
cin >> value;
stack.push(value);
while (true) {
cout << "\nChoose an option:\n";
cout << "1. Display stack\n";
cout << "2. Push another element\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
stack.display();
break;
case 2:
LAB MANUAL Data Structures and Algorithms

cout << "Enter the value to push: ";


cin >> value;
stack.push(value);
break;
case 3:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
Output:
LAB MANUAL Data Structures and Algorithms

Task-2: Implementation of Pop


Code
#include <iostream>
#define MAX 100
using namespace std; //Huzaifa Ali 59384
class Stack {
private:
int arr[MAX];
int top;
public:
Stack() {
top = -1;
}
void push(int value) {
if (top >= MAX - 1) {
cout << "Stack overflow! Cannot push " << value << endl;
} else {
top++;
arr[top] = value;
cout << value << " pushed onto the stack." << endl;
}
}
void pop() {
if (top == -1) {
cout << "Stack underflow! No elements to pop." << endl;
} else {
cout << arr[top] << " popped from the stack." << endl;
top--;
}
}
void display() {
if (top == -1) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack stack;
int choice, value;
cout << "Enter the first element to push into the stack: ";
cin >> value;
stack.push(value);
while (true) {
cout << "\nChoose an option:\n";
cout << "1. Display stack\n";
cout << "2. Push another element\n";
cout << "3. Pop an element\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
LAB MANUAL Data Structures and Algorithms

cin >> choice;


switch (choice) {
case 1:
stack.display();
break;
case 2:
cout << "Enter the value to push: ";
cin >> value;
stack.push(value);
break;
case 3:
stack.pop();
break;
case 4:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
Output:

Observations:
In this lab, we have explored the concept of stacks and various stack-related functions. Throughout our work, we
successfully executed both the push and pop operations on the stack, in addition to verifying the stack's empty
status.
LAB MANUAL Data Structures and Algorithms

Rubrics:
Absent Student is Student can Student has Student has Student
unable to understand followed constructed perfectly
follow the the provided instructions the implemented a
provided laboratory to construct functional/ working
instructions instructions the working model/ logic/
properly. and familiar fundamental schematic/ circuit/ block
The student with the lab schematic/ model/ block diagram/ code
can name the environment block diagram/ and
Demonstration hardware or (Trainer/ diagram/ code, and successfully
simulation software/ code/ model have executed the
platform, but IDE), but on the successfully lab objective in
unable to cannot protoboard/ executed the Realtime or in
implement implement on trainer/ program/ run a simulation
anything the platform simulation circuit on environment
practically or practically or software. software and produced
on the software on the platform the desired
software results
Category Ungraded Very Poor Poor Fair Good Excellent
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.1 0.2 0.3 0.4 0.5
Date Total Marks Instructor’s Signature

Report not Plagiarized Requirements Observations Appropriate Correctly


submitted content are listed and are recorded computations drawn
presented or experimental along with or numerical conclusion
Laboratory incomplete procedure is detailed analysis is with
Reports submission presented procedure performed exact results
and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.1 0.2 0.3 0.4 0.5

Date Total Marks Instructor’s Signature

LAB NO. 7 15/ 10 / 2024

Implementation of Sorting Algorithms.


Lab outcomes:
After completing this lab, students will be able to.
 Implement Selection Sort.
LAB MANUAL Data Structures and Algorithms

Corresponding CLO and PLO:


 CLO-1, PLO-5 (Modern Tool Usage)

Tools:
 VS Code
 Dev C++

Selection Sort Algorithm:


The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering
ascending order) from the unsorted part and putting it at the beginning.
The algorithm maintains two subarrays in each array.
 The subarray which already sorted.
 The remaining subarray was unsorted.
In every iteration of the selection sort, the minimum element (considering ascending order) from the
unsorted subarray is picked and moved to the sorted subarray.
Working of Selection Sort:
1. Set the first element as minimum
2. Compare minimum with the second element. If the second element is smaller than minimum,
assign the second element as minimum.
LAB MANUAL Data Structures and Algorithms

Compare minimum with the third element. Again, if the third element is smaller, then assign
minimum to the third element otherwise do nothing. The process goes on until the last
element.

3. After each iteration, minimum is placed in the front of the unsorted list.

4. For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are repeated
until all the elements are placed at their correct positions.
Insertion Sort:
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in
your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted
part are picked and placed at the correct position in the sorted part.
Characteristics of Insertion Sort:
 This algorithm is one of the simplest algorithms with simple implementation
 Basically, Insertion sort is efficient for small data values
 Insertion sort is adaptive in nature, i.e. it is appropriate for data sets which are already
partially sorted.

Working of Insertion Sort:


1. The first element in the array is assumed to be sorted. Take the second element and store it
separately in key.
LAB MANUAL Data Structures and Algorithms

Compare key with the first element. If the first element is greater than key, then key is placed in front
of the first element.
2. Now, the first two elements are sorted.
Take the third element and compare it with the elements on the left of it. Placed it just behind
the element smaller than it. If there is no element smaller than it, then place it at the
beginning of the array.
3. Similarly, place every unsorted element at its correct position.

Selection Sort Algorithm:


selection Sort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < current Minimum
set element as new minimum
swap minimum with first unsorted position
end selection Sort
Insertion Sort:
Insertion Sort(array)
mark first element as sorted
for each unsorted element X
'extract' the element X
for j <- last Sorted Index down to
0 if current element j > X
move sorted element to the right by 1.
break loop and insert X here end insertionSort
Lab Experiment/ Tasks:
Task-1:
Write a program to implement Selection Sort
Code
#include <iostream>
using namespace std; //Huzaifa Ali 59384
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
LAB MANUAL Data Structures and Algorithms

if (arr[j] < arr[minIndex]) {


minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
selectionSort(arr, n);
cout << "Sorted elements:" << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
LAB MANUAL Data Structures and Algorithms

Output

Observations:
During this lab, we explored three sorting techniques, bubble sort, insertion sort and selection sort
delving into their implementations and distinguishing characteristics. These method include Selection
Sort.
LAB MANUAL Data Structures and Algorithms

Rubrics:
Absent Student is Student can Student has Student has Student
unable to understand followed constructed perfectly
follow the the provided instructions the implemented a
provided laboratory to construct functional/ working
instructions instructions the working model/ logic/
properly. and familiar fundamental schematic/ circuit/ block
The student with the lab schematic/ model/ block diagram/ code
can name the environment block diagram/ and
Demonstration hardware or (Trainer/ diagram/ code, and successfully
simulation software/ code/ model have executed the
platform, but IDE), but on the successfully lab objective in
unable to cannot protoboard/ executed the Realtime or in
implement implement on trainer/ program/ run a simulation
anything the platform simulation circuit on environment
practically or practically or software. software and produced
on the software on the platform the desired
software results
Category Ungraded Very Poor Poor Fair Good Excellent
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.1 0.2 0.3 0.4 0.5
Date Total Marks Instructor’s Signature

Report not Plagiarized Requirements Observations Appropriate Correctly


submitted content are listed and are recorded computations drawn
presented or experimental along with or numerical conclusion
Laboratory incomplete procedure is detailed analysis is with
Reports submission presented procedure performed exact results
and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.1 0.2 0.3 0.4 0.5

Date Total Marks Instructor’s Signature

You might also like