Lecture Notes With Answers
Lecture Notes With Answers
Bubble Sort
#include <iostream>
using namespace std;
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] << " ";
}
B. Selection Sort
Time Complexity: O(n²)
Selection Sort
Descending Order:
C. Insertion Sort
● Builds a sorted list one element at a time.
● Efficient for small datasets.
Insertion Sort
Basic C++ Code:
Operations:
#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();
}
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> s;
s.push(5);
s.push(10);
s.pop();
cout << "Top: " << s.top();
}
Operations:
#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();
}
int main() {
queue<int> q;
q.push(1);
q.push(2);
q.pop();
cout << "Front: " << q.front();
}
a. FIFO
b. LIFO
c. Random
a. LIFO
b. Random
c. FIFO
d. FILO
3. Which sorting algorithm finds the smallest value and places it at the beginning?
c. Selection Sort
a. Insertion Sort
b. Bubble Sort
c. Selection Sort
d. Merge Sort
a. Adds an item
c. Error or underflow
a. Stack
b. Heap
c. Queue
d. Linked List
References:
A. Sorting Algorithms
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
● 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