DSA Lab 8,9
DSA Lab 8,9
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.
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
#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
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
3. Call “is Palindrome” method pass string array as argument to that function.
struct myNode {
int cms;
struct myNode* link;
};
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
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: