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

Bubble sort

The document provides various algorithms and data structures, including sorting algorithms like quick sort and binary search, along with their implementations in C++. It also covers recursive functions for tasks such as checking for prime numbers, counting digits, and summing digits of a number. Additionally, it details the creation and manipulation of a linked list for managing public holidays, along with string manipulation functions for tasks like palindrome checking and character frequency counting.

Uploaded by

dang8805
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Bubble sort

The document provides various algorithms and data structures, including sorting algorithms like quick sort and binary search, along with their implementations in C++. It also covers recursive functions for tasks such as checking for prime numbers, counting digits, and summing digits of a number. Additionally, it details the creation and manipulation of a linked list for managing public holidays, along with string manipulation functions for tasks like palindrome checking and character frequency counting.

Uploaded by

dang8805
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Bubble sort:

Merge sort:
Selection sort:

Insertion sort:
Quick sort:

Quick sort using recursion:

#include <iostream>
using namespace std;

void swap(int* a, int* b) {


int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
// pi is partitioning index, arr[p] is now at right place
int pi = partition(arr, low, high);

// Separately sort elements before partition and after partition


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

Linear search:
Binary search:
Using recursion:
#include <iostream>
using namespace std;
int binarySearch(int arr[], int low, int high, int x)
{
if (high >= low) {
int mid = low + (high - low) / 2;
// If the element is present at the middle
itself
if (arr[mid] == x)
return mid;
// If the element is smaller than mid, it can
only be present in the left subarray
if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);
// Else the element can only be present in
the right subarray
return binarySearch(arr, mid + 1, high, x);
}
// Element is not present in the array
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result != -1)
cout << "Element is present at index " << result << endl;
else
cout << "Element is not present in array" << endl;
return 0;
}
Recursion prime number:
bool isPrime(int n, int i = 2)
{
// Base cases
if (n <= 2)
return (n == 2) ? true : false;
if (n % i == 0)
return false;
if (i * i > n)
return true;

// Check for next divisor


return isPrime(n, i + 1);
}

// Driver Program
int main()
{
int n = 15;
if (isPrime(n))
cout << "Yes";
else
cout << "No";

return 0;
}

Recursion count digit of a number:


#include <stdio.h>
int countDigits(int n) {
if (n == 0) {
return 0;
} else {
return 1 + countDigits(n / 10);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
int digitCount = countDigits(num);
printf("Number of digits in %d is: %d\n", num, digitCount);
return 0;
}
Recursion sum digit of number:
#include <stdio.h>
#include <stdlib.h>
int sumOfDigits(int n) {
// Handle negative numbers
if (n < 0) {
n = -n; // Make the number positive
}
if (n == 0) {
return 0;
} else {
return n % 10 + sumOfDigits(n / 10);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
int sum = sumOfDigits(num);
printf("Sum of digits of %d is: %d\n", num, sum);
return 0;
Linked list: Create a linked-list consisting of public holidays of a
year and description of each day (as string), so that: A new public
holiday can be added to the beginning of the list Search for the
description of the day (input argument is a date including day and
month) Delete a public holiday at the beginning of the list Delete a
public holiday in the middle of the list (input argument is a date
including day and month) Clear the whole list Write a program to
demonstrate the usage of the above list.

#include <iostream>
#include <string>
using namespace std;
// Structure to represent a holiday item
struct holidayItem {
string date; // Holiday date, format: "dd-mm-yyyy"
string description; // Holiday description
holidayItem* next; // Pointer to the next node
};

// Structure to represent a linked list


struct holidayList {
holidayItem* head; // Pointer to the head of the list
};

// Function to initialize a linked list


void initHolidayList(holidayList& l) {
l.head = nullptr; // Set the head pointer to null
}

// Function to add a holiday to the beginning of the list


void addNote(holidayList& l, const string& d, const string& des) {
holidayItem* item = new holidayItem; // Create a new node
item->date = d; // Assign the holiday date
item->description = des; // Assign the holiday description
item->next = l.head; // Link the new node to the current list
l.head = item; // Update the head to point to the new node
}

// Function to delete a holiday at the beginning of the list


void deleteAtBeginning(holidayList& l) {
if (l.head == nullptr) { // Check if the list is empty
cout << "The list is empty.\n";
return;
}
holidayItem* temp = l.head; // Store the pointer to the first node
l.head = l.head->next; // Move the head to the next node
delete temp; // Free the memory of the first node
cout << "Deleted the holiday at the beginning of the list.\n";
}

// Function to delete a holiday by date


void deleteByDate(holidayList& l, const string& d) {
if (l.head == nullptr) { // Check if the list is empty
cout << "The list is empty.\n";
return;
}
if (l.head->date == d) { // If the holiday to be deleted is at the beginning
deleteAtBeginning(l);
return;
}
holidayItem* current = l.head;
while (current->next != nullptr && current->next->date != d) {
current = current->next;
}
if (current->next == nullptr) { // If the holiday is not found
cout << "Holiday with date: " << d << " not found.\n";
return;
}
holidayItem* temp = current->next;
current->next = temp->next; // Skip the node to be deleted
delete temp; // Free the memory
cout << "Deleted the holiday with date: " << d << ".\n";
}

// Function to clear the entire list


void clearList(holidayList& l) {
while (l.head != nullptr) {
deleteAtBeginning(l);
}
cout << "Cleared the entire list.\n";
}

// Function to search for the description of a holiday by date


void searchDescription(const holidayList& l, const string& d) {
holidayItem* current = l.head;
while (current != nullptr) {
if (current->date == d) {
cout << "Description for date " << d << ": " << current-
>description << endl;
return;
}
current = current->next;
}
cout << "Holiday with date: " << d << " not found.\n";
}

// Function to display the list of holidays


void displayHolidays(const holidayList& l) {
if (l.head == nullptr) {
cout << "The list is empty.\n";
return;
}
holidayItem* current = l.head;
cout << "List of holidays:\n";
while (current != nullptr) {
cout << current->date << " - " << current->description << endl;
current = current->next;
}
}

// Main function
int main() {
holidayList list;
initHolidayList(list);
// Add some holidays to the list
addNote(list, "01-01-2023", "New Year's Day");
addNote(list, "30-04-2023", "Independence Day");
addNote(list, "02-09-2023", "National Day");
// Display the list
displayHolidays(list);
// Search for a holiday
searchDescription(list, "30-04-2023"); // Find holiday on 30-04-2023
searchDescription(list, "01-05-2023"); // Find a non-existing holiday
// Delete the first holiday
deleteAtBeginning(list);
displayHolidays(list);
// Delete a holiday by date
deleteByDate(list, "30-04-2023");
displayHolidays(list);
// Clear the entire list
clearList(list);
displayHolidays(list);
return 0;
}
Write a function that converts a string like “124” to an integer 124.
Write a program to demonstrate the usage.

Write a function to check whether string inputted by user is


palindrome or not. Demonstrate the usage
Write a function to obtain a string entered via keyboard in the
reverse order as per word, e.g. “How Are You” change to “You Are
How”. Write a program to demonstrate the usage.
Write a program to enter a sentence (or paragraph) and a word (or a
string), then replace the word (string) within the sentence
(paragraph) with the equal number of "*" at all occurring. Design
and use appropriate functions to perform the task.
Write a function to remove all the extra blanks in a string.
Demonstrate the usage.
Write a program to enter any string and print the frequency of each
character within the string. The character with multiple frequencies
should be displayed only once in the output, with the frequency
value. Design and use appropriate functions to perform the task.

NOTE:
- (*p)++ increment the value
- *p++ increment the address
-

You might also like