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

Lecture Notes

The document consists of lecture notes on Data Structures and Algorithms, covering sorting algorithms (Bubble Sort, Selection Sort, Insertion Sort) and data structures (Stacks and Queues). It includes definitions, importance, time complexities, basic and advanced C++ code implementations, and tasks for practice. Additionally, it provides references for further learning on each topic.

Uploaded by

22-05068
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)
3 views

Lecture Notes

The document consists of lecture notes on Data Structures and Algorithms, covering sorting algorithms (Bubble Sort, Selection Sort, Insertion Sort) and data structures (Stacks and Queues). It includes definitions, importance, time complexities, basic and advanced C++ code implementations, and tasks for practice. Additionally, it provides references for further learning on each topic.

Uploaded by

22-05068
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/ 10

Lecture notes

Owner Assistant Professor POUL ISAAC C DE CHAVEZ

College College of Informatics and Computing Sciences

Subject Data Structure and Algorithm

Lecture #1: SORTING

● Definition:
Sorting is the process of arranging elements in a specific order, typically ascending
or descending.

● Why it's important:


- Easier to search data
- Necessary for binary search
- Prepares data for reporting or decision making

● Why Sorting Matters:


-Speeds up searching (e.g., binary search)
-Organizes data for human readability
-Enables efficient algorithms in databases and analytics
A. Bubble Sort
Time Complexity: O(n²)
● Simple sorting algorithm.
● Repeatedly compares adjacent elements and swaps them if they are in the wrong
order.

Bubble Sort

Basic C++ Code (Ascending Order):

#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1])
swap(arr[j], arr[j+1]);
}
}
}

int main() {
int arr[] = {5, 1, 4, 2};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

Optimized Bubble Sort:

void optimizedBubbleSort(int arr[], int n) {


bool swapped;
for (int i = 0; i < n-1; i++) {
swapped = false;
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
swapped = true;
}
}
if (!swapped) break;
}
}

B. Selection Sort
Time Complexity: O(n²)

● Repeatedly finds the minimum element and moves it to the front.

Selection Sort

Basic C++ Code:

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++)
if (arr[j] < arr[minIndex])
minIndex = j;
swap(arr[i], arr[minIndex]);
}
}

Descending Order:

void selectionSortDescending(int arr[], int n) {


for (int i = 0; i < n-1; i++) {
int maxIndex = i;
for (int j = i+1; j < n; j++)
if (arr[j] > arr[maxIndex])
maxIndex = j;
swap(arr[i], arr[maxIndex]);
}
}

C. Insertion Sort
● Builds a sorted list one element at a time.
● Efficient for small datasets.

Insertion Sort
Basic C++ Code:

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; i++) {
int key = arr[i], j = i-1;
while (j >= 0 && arr[j] > key)
arr[j+1] = arr[j--];
arr[j+1] = key;
}
}

With Insert Function:

void insert(int arr[], int n) {


int key = arr[n], j = n - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; i++)
insert(arr, i);
}

Lecture #2: STACKS


● Definition:
A stack is a linear data structure that follows the LIFO (Last-In, First-Out) principle.

LIFO (Last-In, First-Out)

Operations:

● Push(item): Add item on top

● Pop(): Remove item from top


● Peek(): View top item

● IsEmpty(): Check if empty

Basic C++ Code Using Array

#include <iostream>
using namespace std;

#define SIZE 5
int stack[SIZE], top = -1;

void push(int x) {
if (top < SIZE - 1)
stack[++top] = x;8
}

int pop() {
if (top >= 0)
return stack[top--];
return -1;
}

int main() {
push(10); push(20);
cout << "Popped: " << pop();
}

Advanced C++ Code Using STL

#include <iostream>
#include <stack>
using namespace std;

int main() {
stack<int> s;
s.push(5);
s.push(10);
s.pop();
cout << "Top: " << s.top();
}
Lecture #3: QUEUES
● Definition:
A queue is a linear data structure that follows the FIFO (First-In, First-Out)
principle.

FIFO (First-In, First-Out)

Operations:

● Enqueue(item): Add to rear


● Dequeue(): Remove from front
● Front(): Peek first item
● IsEmpty()

Basic C++ Code Using Array

#define SIZE 5
int queue[SIZE], front = 0, rear = -1;

void enqueue(int x) {
if (rear < SIZE - 1)
queue[++rear] = x;
}

int dequeue() {
if (front <= rear)
return queue[front++];
return -1;
}

int main() {
enqueue(3); enqueue(5);
cout << "Dequeued: " << dequeue();
}

Advanced C++ Code Using STL


#include <iostream>
#include <queue>
using namespace std;

int main() {
queue<int> q;
q.push(1);
q.push(2);
q.pop();
cout << "Front: " << q.front();
}

TASK 1: Activities with Answers (SHOW SOLUTION)


1. Trace Bubble Sort on [6, 3, 2]:
Answer: Pass 1: [3, 2, 6], Pass 2: [2, 3, 6]

2. Simulate Stack - Push(4), Push(6), Pop(), Push(2)


Answer: Stack from top: [2, 4]

3. Simulate Queue - Enqueue(7), Enqueue(9), Dequeue(), Enqueue(5)


Answer: Queue from front: [9, 5]

TASK 2: Short Answers


1. What principle does a stack follow?
Answer:

2. What does a queue use to manage order?


Answer:

3. Which sort algorithm swaps adjacent elements repeatedly?


Answer:

4. What is the result of popping an empty stack?


Answer:

5. What C++ STL class is used for stack implementation?


Answer:

TASK 3: Multiple Choice (Choose the correct answer):


1. Which of the following is a characteristic of stacks?

a. FIFO

b. LIFO

c. Random

d. None of the above

2. What is the main principle of a queue?

a. LIFO

b. Random

c. FIFO

d. FILO

3. Which sorting algorithm finds the smallest value and places it at the beginning?

a. Insertion Sort

b. Bubble Sort

c. Selection Sort

d. Merge Sort

4. What is the result of popping from an empty stack?

a. Adds an item

b. Views top item

c. Error or underflow

d. Removes bottom item

5. Which structure is best for a printer task queue?

a. Stack

b. Heap

c. Queue
d. Linked List

References:

A. Sorting Algorithms
1. Bubble Sort

● Learn Bubble Sort in 7 minutes


This tutorial offers a concise explanation of the Bubble Sort algorithm with
practical examples.
Watch on YouTubeYouTube+2YouTube+2YouTube+2

● Bubble Sort Algorithm using C++


A step-by-step guide to implementing Bubble Sort in C++, including code
walkthroughs.
Watch on YouTube

2. Selection Sort

● Selection Sort in 3 minutes


A quick overview of the Selection Sort algorithm, ideal for rapid revision.
Watch on YouTube

● Selection Sort | C++ Example


Demonstrates how to implement Selection Sort in C++ with clear code examples.
Watch on YouTube

3. Insertion Sort

● Learn Insertion Sort in 7 minutes


An easy-to-follow tutorial explaining the Insertion Sort algorithm with examples.
Watch on YouTubeYouTube

● Insertion Sort | C++ Example


Provides a detailed C++ implementation of Insertion Sort, suitable for beginners.
Watch on YouTube

B. Stack Data Structure


● What is STACK data structure in C++?
Explains the concept of stacks in C++ and how to work with them using STL.
Watch on YouTubeYouTube+6YouTube+6YouTube+6

● What is STACK data structure in C++? Stack in C++ STL with Example
A practical guide to implementing stacks in C++ using the Standard Template
Library.
Watch on YouTube

C. Queue Data Structure


● Queue Data Structure In STL | C++ Tutorial
Learn how to use the queue data structure built into the C++ Standard Template
Library.
Watch on YouTubeYouTube

● How to Use C++ STL Queue with an Example Program


A tutorial on implementing queues in C++ using STL, complete with example
programs.
Watch on YouTube

You might also like