Choosing the right algorithm is crucial for solving real-world problems because it ensures efficient use of
resources like time and memory, handles scalability, and provides accurate results. A good algorithm
leverages the specific characteristics of a problem, making it possible to solve it effectively, even for
large datasets.
Example: Searching an Element in an Array
Linear Search: Traverses each element sequentially with O(n)O(n)O(n) time complexity,
suitable for small or unsorted arrays.
Binary Search: Exploits sorted arrays, halving the search space each step, achieving O(logn)O(\
log n)O(logn) time complexity, ideal for large datasets.
For a 1,000,000-element array, Binary Search drastically outperforms Linear Search, requiring
only about 20 comparisons instead of up to 1,000,000. This demonstrates the profound impact of
selecting the right algorithm.
For an array of 10,000 elements, Binary Search is more efficient than Linear Search, provided
the array is sorted.
Linear Search: Checks each element sequentially with O(n)O(n)O(n) time complexity,
requiring up to 10,000 comparisons in the worst case.
Binary Search: Halves the search space with each step, achieving O(logn)O(\log
n)O(logn) time complexity. For 10,000 elements, it needs only about 14 comparisons.
Binary Search is faster and better suited for large, sorted arrays.
#include <iostream>
using namespace std;
int main() {
int arr[] = {3, 5, 7, 3, 8, 3, 10}; // Example array
int target;
cout << "Enter the target element: ";
cin >> target;
// Perform linear search and print indices of all occurrences
for (int i = 0; i < 7; i++) {
if (arr[i] == target) {
cout << "Target element found at index: " << i << endl;
return 0;
#include <iostream>
using namespace std;
int main() {
int arr[5] = {3, 3, 5, 7, 8}; // Example sorted array
int target, loc = -1;
cout << "Enter the target element: ";
cin >> target;
int start = 0, end = 4, mid;
// Perform binary search
while (start <= end) {
mid = (start + end) / 2;
if (arr[mid] == target) {
loc = mid; // Target found, store index
break;
else if (arr[mid] < target) {
start = mid + 1; // Search in the right half
} else {
end = mid - 1; // Search in the left half
if (loc != -1) {
cout << "Target element found at index: " << loc << endl;
} else {
cout << "Target element not found in the array." << endl;
return 0;
4b
#include <iostream>
using namespace std;
int main() {
int arr[5] = {3, 3, 5, 7, 8}; // Example sorted array
int target, loc = -1;
int lowerRange, upperRange;
cout << "Enter the target element: ";
cin >> target;
// Input the range limits
cout << "Enter the lower range: ";
cin >> lowerRange;
cout << "Enter the upper range: ";
cin >> upperRange;
// Check if the target is within the range
if (target < lowerRange || target > upperRange) {
cout << "Target is out of the specified range." << endl;
} else {
int start = 0, end = 4, mid;
// Perform binary search
while (start <= end) {
mid = (start + end) / 2;
if (arr[mid] == target) {
loc = mid; // Target found, store index
break;
else if (arr[mid] < target) {
start = mid + 1; // Search in the right half
} else {
end = mid - 1; // Search in the left half
if (loc != -1) {
cout << "Target element found at index: " << loc << endl;
} else {
cout << "Target element not found in the array." << endl;
return 0;
#include <iostream>
using namespace std;
int main() {
int arr[] = {64, 25, 12, 22, 11}; // Example array
int size = sizeof(arr) / sizeof(arr[0]);
int swapCount = 0; // Counter for swaps
// Selection Sort algorithm
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
// Find the minimum element in the unsorted part of the array
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
// Swap the found minimum element with the first element of the unsorted part
if (minIndex != i) {
// Manual swap using a temporary variable
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
swapCount++; // Increment the swap counter
// Output the sorted array
cout << "Sorted array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
cout << endl;
// Output the number of swaps
cout << "Number of swaps performed: " << swapCount << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arr[] = {64, 25, 12, 22, 11}; // Example array
int size = sizeof(arr) / sizeof(arr[0]);
bool swapped; // Flag to check if any swap was performed
// Bubble Sort algorithm (Descending order)
for (int i = 0; i < size - 1; i++) {
swapped = false; // Reset the swapped flag
// Compare adjacent elements
for (int j = 0; j < size - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {
// Swap if the current element is smaller than the next
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true; // Mark that a swap occurred
// If no elements were swapped, the array is already sorted
if (!swapped) {
break;
// Output the sorted array
cout << "Sorted array in descending order: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
cout << endl;
return 0;
#include <iostream>
using namespace std;
class student {
public:
string Sname;
string Sage;
student* next;
// Insert student at the head of the list
void insertAtHead(student*& head, student*& tail) {
student* temp = new student();
cout << "Enter name: ";
cin >> temp->Sname;
cout << "Enter age: ";
cin >> temp->Sage;
temp->next = head;
head = temp;
// If the list was empty, both head and tail will point to the new node
if (tail == nullptr) {
tail = temp;
// Insert student at a specific position
void insertAtPosition(student*& head, student*& tail, int position) {
if (position == 1) {
insertAtHead(head, tail); // If position is 1, use insertAtHead
return;
student* temp = new student();
cout << "Enter name: ";
cin >> temp->Sname;
cout << "Enter age: ";
cin >> temp->Sage;
student* current = head;
int count = 1;
// Traverse to the position just before the desired position
while (current != nullptr && count < position - 1) {
current = current->next;
count++;
if (current != nullptr) {
temp->next = current->next;
current->next = temp;
// If the inserted student is at the tail, update the tail pointer
if (temp->next == nullptr) {
tail = temp;
} else {
cout << "Position is out of range!" << endl;
// Delete student by its name
void deleteByValue(student*& head, student*& tail, const string& value) {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
// If the node to be deleted is the head
if (head->Sname == value) {
student* temp = head;
head = head->next;
cout << "Deleted student: " << temp->Sname << " " << temp->Sage << endl;
delete temp;
if (head == nullptr) {
tail = nullptr; // If list becomes empty, update tail
return;
student* current = head;
while (current->next != nullptr && current->next->Sname != value) {
current = current->next;
// If the node is found
if (current->next != nullptr) {
student* temp = current->next;
current->next = current->next->next;
// If the node to be deleted is the tail, update the tail pointer
if (temp->next == nullptr) {
tail = current;
cout << "Deleted student: " << temp->Sname << " " << temp->Sage << endl;
delete temp;
} else {
cout << "Student not found!" << endl;
}
// Display all students in the list
void display(student* head) {
student* temp = head;
while (temp != nullptr) {
cout << "Name: " << temp->Sname << " Age: " << temp->Sage << endl;
temp = temp->next;
};
int main() {
student* head = nullptr;
student* tail = nullptr;
student s1;
// Insert students at the head of the list
s1.insertAtHead(head, tail);
s1.insertAtHead(head, tail);
// Insert student at a specific position
int position;
cout << "Enter position to insert a new student: ";
cin >> position;
s1.insertAtPosition(head, tail, position);
// Display the linked list
cout << "\nDisplaying list after insertions:\n";
s1.display(head);
// Delete student by value
string value;
cout << "\nEnter name of the student to delete: ";
cin >> value;
s1.deleteByValue(head, tail, value);
// Display the list after deletion
cout << "\nDisplaying list after deletion:\n";
s1.display(head);
return 0;
Bubble Sort Dry Run (Descending Order)
Initial Array: [8, 3, 5, 1, 9, 6, 7, 2]
Pass 1:
o Compare 8 and 3: No swap needed (8 > 3).
o Compare 8 and 5: No swap needed (8 > 5).
o Compare 8 and 1: No swap needed (8 > 1).
o Compare 8 and 9: Swap (9 > 8) → [3, 5, 1, 8, 6, 7, 2, 9]
o Compare 8 and 6: Swap (8 > 6) → [3, 5, 1, 6, 7, 2, 8, 9]
o Compare 8 and 7: Swap (8 > 7) → [3, 5, 1, 6, 2, 7, 8, 9]
o Compare 8 and 2: Swap (8 > 2) → [3, 5, 1, 6, 2, 7, 8, 9]
State after Pass 1: [3, 5, 1, 6, 2, 7, 8, 9] (Swapped elements are 9, 6, 7, 8)
Pass 2:
o Compare 3 and 5: Swap (5 > 3) → [5, 3, 1, 6, 2, 7, 8, 9]
o Compare 3 and 1: No swap needed (3 > 1).
o Compare 3 and 6: Swap (6 > 3) → [5, 6, 3, 1, 2, 7, 8, 9]
o Compare 6 and 2: Swap (6 > 2) → [5, 6, 3, 2, 1, 7, 8, 9]
o Compare 6 and 7: Swap (7 > 6) → [5, 7, 6, 2, 1, 3, 8, 9]
o Compare 6 and 8: Swap (8 > 6) → [5, 7, 8, 2, 1, 3, 6, 9]
State after Pass 2: [5, 7, 8, 2, 1, 3, 6, 9] (Swapped elements are 5, 6, 7, 8)
Pass 3:
o Compare 5 and 7: Swap (7 > 5) → [7, 5, 8, 2, 1, 3, 6, 9]
o Compare 5 and 8: Swap (8 > 5) → [7, 8, 5, 2, 1, 3, 6, 9]
o Compare 5 and 2: No swap needed (5 > 2).
o Compare 5 and 1: No swap needed (5 > 1).
o Compare 5 and 3: No swap needed (5 > 3).
o Compare 5 and 6: Swap (6 > 5) → [7, 8, 6, 5, 1, 3, 2, 9]
State after Pass 3: [7, 8, 6, 5, 1, 3, 2, 9] (Swapped elements are 5, 6)
Selection Sort Dry Run (Descending Order)
Initial Array: [8, 3, 5, 1, 9, 6, 7, 2]
Pass 1:
o Find the maximum element in the entire array (9).
o Swap 9 with the first element (8).
State after Pass 1: [9, 3, 5, 1, 8, 6, 7, 2] (Swapped 9 and 8)
Pass 2:
o Find the maximum element from index 1 to 7 (8).
o Swap 8 with the second element (3).
State after Pass 2: [9, 8, 5, 1, 3, 6, 7, 2] (Swapped 8 and 3)
Pass 3:
o Find the maximum element from index 2 to 7 (7).
o Swap 7 with the third element (5).
State after Pass 3: [9, 8, 7, 1, 3, 6, 5, 2] (Swapped 7 and 5)
Pass 4:
o Find the maximum element from index 3 to 7 (6).
o Swap 6 with the fourth element (1).
State after Pass 4: [9, 8, 7, 6, 3, 1, 5, 2] (Swapped 6 and 1)
Pass 5:
o Find the maximum element from index 4 to 7 (5).
o Swap 5 with the fifth element (3).
State after Pass 5: [9, 8, 7, 6, 5, 1, 3, 2] (Swapped 5 and 3)
Pass 6:
o Find the maximum element from index 5 to 7 (3).
o Swap 3 with the sixth element (1).
State after Pass 6: [9, 8, 7, 6, 5, 3, 1, 2] (Swapped 3 and 1)
Pass 7:
o Find the maximum element from index 6 to 7 (2).
o Swap 2 with the seventh element (1).
State after Pass 7: [9, 8, 7, 6, 5, 3, 2, 1] (Swapped 2 and 1)
Final Sorted Array:
Bubble Sort Final: [9, 8, 7, 6, 5, 3, 2, 1]
Selection Sort Final: [9, 8, 7, 6, 5, 3, 2, 1]
Both sorting algorithms result in the array being sorted in descending order after the given
passes.