Bubble sort
Bubble sort
Merge sort:
Selection sort:
Insertion sort:
Quick sort:
#include <iostream>
using namespace std;
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;
// Driver Program
int main()
{
int n = 15;
if (isPrime(n))
cout << "Yes";
else
cout << "No";
return 0;
}
#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
};
// 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.
NOTE:
- (*p)++ increment the value
- *p++ increment the address
-