0% found this document useful (0 votes)
10 views11 pages

DSA Lab 8,9

This lab manual outlines the implementation of sorting algorithms, specifically Bubble Sort, and the use of stacks to determine if a string is a palindrome. Students will learn to implement Bubble Sort and write a program using stack operations to check for palindromes. The document includes theoretical explanations, procedures, code examples, and evaluation rubrics for assessing student performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

DSA Lab 8,9

This lab manual outlines the implementation of sorting algorithms, specifically Bubble Sort, and the use of stacks to determine if a string is a palindrome. Students will learn to implement Bubble Sort and write a program using stack operations to check for palindromes. The document includes theoretical explanations, procedures, code examples, and evaluation rubrics for assessing student performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

LAB MANUAL Data Structures and Algorithms

LAB NO. 08 22/ 10 / 2024

Implementation of Sorting Algorithms (Bubble Sort)


Lab outcomes:
After completing this lab, students will be able to.
 Implement Bubble Sort.
Corresponding CLO and PLO:
 CLO-2, PLO-3
Tools:
 VS Code
 Dev C++

Theory:
Bubble Sort Algorithm:
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in the wrong order. This algorithm is not suitable for large data sets as its
average and worst-case time complexity is quite high.
How does Bubble Sort work:
Suppose we are trying to sort the elements in ascending order.

First Iteration (Compare and Swap)

 Starting from the first index, compare the first and the second elements.
 If the first element is greater than the second element, they are swapped.
 Now, compare the second and the third elements. Swap them if they are not in order.
 The above process goes on until the last element.

Remaining Iteration:
 The same process goes on for the remaining iterations.
 After each iteration, the largest element among the unsorted elements is placed at the end.
 In each iteration, the comparison takes place up to the last unsorted element.
 The array is sorted when all the unsorted elements are placed at their correct positions.
LAB MANUAL Data Structures and Algorithms

Procedure:
Bubble Sort Algorithm:
bubble-sort(array)
for i <- 1 to indexOfLastUnsortedElement-1
if left Element > right Element
swap left Element and right Element
end bubble Sort

Lab Experiment/ Task


Write a program to implement Bubble Sort.
Code

#include <iostream>
using namespace std;
int main()
{
int array[40] , size, element ;
cout<<" Huzaifa Ali / 59384 "<<endl;
cout<<" Enter the Size of array : ";
cin>>size; cout<<" Enter the element of array :"<<endl;
for(int i=0; i<size; i++)
{
cout<<"arr["<<i<<"] = ";
cin>>array[i];
}
for (int step = 0; step < size -1; ++step)
{
for (int i = 0; i < size - step - 1; ++i)
{
if (array[i] > array[i + 1])
{
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
cout<<endl;
cout<<"Step :"<<step+1<<endl;
for (int i = 0; i < size; ++i)
{ cout << " " << array[i];
}
}
}
LAB MANUAL Data Structures and Algorithms

Output

Observations:
During this lab, we explored sorting technique, delving into their implementations and
distinguishing characteristics.
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 MANUAL Data Structures and Algorithms

LAB NO. 9 12 / 11 / 2024


Write a program to find palindrome of input string using Stack operations.
Lab outcomes:
After completing this lab, students will be able to.

 Use stack to solve problems.


Corresponding CLO and PLO:
 CLO-2, PLO-3 
Tools:
 Eclipse IDE
 JDK

Theory:

Stack is an abstract data type with a bounded (predefined) capacity. It is a simple data structure that
allows adding and removing elements in a particular order. Every time an element is added, it goes on
the top of the stack and the only element that can be removed is the element that is at the top of the
stack, just like a pile of objects.
Basic features of Stack
Stack is an ordered list of similar data type.
Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
push() function is used to insert new elements into the Stack and pop() function is used to remove an
element from the stack. Both insertion and removal are allowed at only one end of Stack called Top.
Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it
is completely empty.
Applications of Stack
The simplest application of a stack is to reverse a word. You push a given word to stack - letter by letter
- and then pop letters from the stack.
There are other uses also like:
 Parsing
 Expression Conversion (Infix to Postfix, Postfix to Prefix etc)
Tasks:
Task-1:
Write a program to check if a string is a palindrome or not, a string needs to be compared with the
reverse of itself.
LAB MANUAL Data Structures and Algorithms

Example:
 “madam” reverse string still “madam”  palindrome  true
 “test” reverse “test”  Not palindrome  false

1. Make a function “Is Palindrome”.

2. Take a string from user.

3. Call “is Palindrome” method pass string array as argument to that function.

4. If return value is true, then print “string is palindrome”

5. Else, print “not palindrome” Note:


Must use previous Stack implementation to solve above problem
Code
//Huzaifa Ali 59384
#include <iostream>
#include <stdlib.h>

using namespace std;

struct myNode {
int cms;
struct myNode* link;
};

struct myNode* head = NULL;

void insertAtBegin(int value) {


struct myNode* new_node = (struct myNode*)malloc(sizeof(struct myNode));
new_node->cms = value;
new_node->link = head;
head = new_node;
}

void display() {
struct myNode* ptr = head;
while (ptr != NULL) {
cout << ptr->cms << " ";
ptr = ptr->link;
}
cout << endl;
}

void deleteAtBegin() {
if (head == NULL) {
cout << "Linked List is Empty\n";
return;
}
else {
struct myNode* temp = head;
head = head->link;
free(temp);
}
}
LAB MANUAL Data Structures and Algorithms

void insertAtEnd(int value) {


struct myNode* new_node = (struct myNode*)malloc(sizeof(struct myNode));
new_node->cms = value;
new_node->link = NULL;
if (head == NULL) {
head = new_node;
}
else {
struct myNode* current = head;
while (current->link != NULL) {
current = current->link;
}
current->link = new_node;
}
}
void deleteAtEnd() {
if (head == NULL) {
cout << "Linked List is Empty\n";
return;
}
else if (head->link == NULL) {
free(head);
head = NULL;
}
else {
struct myNode* prev = head;
struct myNode* current = head;
while (current->link != NULL) {
prev = current;
current = current->link;
}
prev->link = NULL;
free(current);
}
}

void deleteAtPos(int item) {


if (head == NULL) {
cout << "List is empty\n";
return;
}
if (head->cms == item) {
struct myNode* temp = head;
head = head->link;
free(temp);
return;
}
struct myNode* previous = head;
struct myNode* current = head;
while (current != NULL && current->cms != item) {
previous = current;
current = current->link;
}
if (current == NULL) {
cout << "Item not found\n";
return;
}
previous->link = current->link;
free(current);
}
LAB MANUAL Data Structures and Algorithms

void insertAtPos(int value) {


int item;
cout << "Enter the item after which you want to insert: ";
cin >> item;
struct myNode* new_node = (struct myNode*)malloc(sizeof(struct myNode));
new_node->cms = value;
new_node->link = NULL;
if (head == NULL) {
head = new_node;
}
else {
struct myNode* previous = head;
struct myNode* current = head;
while (current != NULL && current->cms != item) {
previous = current;
current = current->link;
}
if (current == NULL) {
cout << "Item not found\n";
return;
}
new_node->link = current->link;
current->link = new_node;
}
}
int main() {
int opt;
do {
cout << "Select an option given below:\n";
cout << "1: Insert At First\n";
cout << "2: Delete First\n";
cout << "3: Display\n";
cout << "4: Insert at end\n";
cout << "5: Delete at end\n";
cout << "6: Insert at specific pos\n";
cout << "7: Delete at specific pos\n";
cout << "8: Exit\n";
cin >> opt;
switch (opt) {
case 1: {
int data;
cout << "Please enter your Number: ";
cin >> data;
insertAtBegin(data);
break;
}
case 2: {
deleteAtBegin();
break;
}
case 3: {
display();
break;

case 4: {
int data;
cout << "Please enter your Number: ";
cin >> data;
insertAtEnd(data);
break;
}
LAB MANUAL Data Structures and Algorithms

case 5: {
deleteAtEnd();
break;
}
case 6: {
int data;
cout << "Please enter your Number: ";
cin >> data;
insertAtPos(data);
break;
}
case 7:
int item;
cout << "Which item you want to delete: ";
cin>>item;
deleteAtPos(item);
break;
}
}
while(opt!=8);
return 0;
}

Result

Observations:
In this lab we write a program to find palindrome of input string using Stack operations and Use stack to
solve problems.
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 MANUAL Data Structures and Algorithms

You might also like