Breadth First Search
Breadth First Search
#include <stdio.h>
#include <stdlib.h>
#define N 10
q[++rear] = start;
visited[start] = 1;
start = q[++front];
if (start == 9)
printf("10\t");
else
q[++rear] = i;
visited[i] = 1;
int main() {
int adj[N][N] = {
{0,1,1,0,0,0,0,0,0,1},
{0,0,0,0,1,0,0,0,0,1},
{0,0,0,0,1,0,1,0,0,0},
{1,0,1,0,0,1,1,0,0,1},
{0,0,0,0,0,0,1,1,0,0},
{0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,0,0,0,1,1,1},
{0,0,1,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,1,1,0}
};
return 0;
}
Linear Search Program
#include <stdio.h>
#define MAX 20
int intArray[MAX] = {1, 2, 3, 4, 6, 7, 9, 11, 12, 14, 15, 16, 17, 19, 33, 34, 43, 45, 55, 66};
int i;
printf("=");
printf("=\n");
int comparisons = 0;
int i;
comparisons++;
if (data == intArray[i]) {
index = i;
break;
return index;
}
void display() {
int i;
printf("[");
printf("]\n");
int main() {
display();
printline(50);
// Find location of 55
if (location != -1)
else
return 0;
}
Quicksort Using Stacks (Iterative)
Original array:
44 33 11 55 77 90 40 60 99 22 88 66
Sorted array:
11 22 33 40 44 55 60 66 77 88 90 99
#include <stdio.h>
int A[] = {44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66};
int n = sizeof(A)/sizeof(A[0]);
*x = *y;
*y = temp;
left++;
right--;
swap(&A[left], &A[right]);
}
swap(&A[low], &A[right]);
LOWER[++top] = 0;
UPPER[top] = n - 1;
// Left sublist
LOWER[++top] = low;
UPPER[top] = pivot - 1;
// Right sublist
LOWER[++top] = pivot + 1;
UPPER[top] = high;
printf("\n");
int main() {
printf("Original array:\n");
display(A, n);
quicksort_iterative(A, n);
printf("Sorted array:\n");
display(A, n);
return 0;
}
//PROGRAM TO IMPLEMENT SEQUENTIAL SEARCHING
#include <stdio.h>
int main() {
char opt;
scanf("%d", &n);
scanf("%d", &arr[i]);
do {
if (item == arr[i]) {
break;
if (i == n)
return 0;
}
//PROGRAM TO IMPLEMENT THE BINARY SEARCH
#include <conio.h>
#include <stdio.h>
void main() {
char opt;
clrscr();
scanf("%d", &n);
scanf("%d", &arr[i]);
getch();
do {
clrscr();
scanf("%d", &item);
start = 0;
end = n - 1;
else
end = middle - 1;
if (item == arr[middle])
else
scanf("%c", &opt);
}
Quick Sort Program in C
Implementation in C
#include <stdio.h>
#define MAX 7
int i;
printf("=");
printf("\n");
void display() {
int i;
printf("[ ");
printf("]\n");
int main() {
display();
return 0;
}
Bubble Sort Program in C
#include <stdio.h>
#include <stdbool.h>
#define MAX 10
void display() {
int i;
printf("[");
printf("%d", list[i]);
if (i < MAX - 1)
printf(", ");
printf("]\n");
int main() {
display();
return 0;
}
Accessing Array Elements
#include <iostream>
int main() {
int a[50], n;
cin >> n;
if (n > 50 || n < 1) {
cout << "Please enter a value between 1 and 50." << endl;
return 1;
a[i] = i + 200; // fills array with values like 200, 201, ...
cout << "Element[" << j << "]: " << a[j] << endl;
return 0;
}
Accessing Array elements(2dimentional)
#include <iostream>
int main()
{
int r,c;
cout<<"Enter row size:";
cin>>r;
cout<<"Enter coloum size:";
cin>>c;
int a[r][c];
#include <stdio.h>
int main() {
int array[10] = {1, 2, 4, 5}; // array size large enough to allow insertion
int i;
// insert value
// increase count
N++;
}
return 0;
}
Insertion at the Beginning of an Array
#include <stdio.h>
int main() {
int array[10] = {2, 3, 4, 5}; // Make array large enough for insertion
int i;
array[0] = value;
N++;
return 0;
}
Insertion at the Given Index of an Array
#include <stdio.h>
int main() {
int i;
array[i + 1] = array[i];
array[index] = value;
// Increase size
N++;
}
return 0;
#include <stdio.h>
int main() {
int i = 0;
int value = 3;
array[i + 1] = array[i];
array[index] = value;
N++;
return 0;
}
Deletion Operation
#include <stdio.h>
int main() {
int i, j;
j = k;
while(j < n) {
LA[j - 1] = LA[j];
j = j + 1;
n = n - 1;
return 0;
}
Update Operation
#include <stdio.h>
int main() {
int i;
LA[k-1] = item;
return 0;
}
Update Operation
#include <iostream>
int main()
{
int a[20],n,item,i,data;
cout<<"How many data:";
cin>>n;
}
cout<<"Which data do you want to update:";
cin>>data;
cout<<"Your item is:";
cin>>item;
for(i=1; i<=n; i++)
{
if(a[i]==data)
{
a[i]=item;
}
}
return 0;
}
Search Operation
#include <stdio.h>
int main() {
int item = 5, n = 5;
int i = 0, j = 0;
while(j < n) {
if(LA[j] == item) {
break;
j = j + 1;
if (j < n) {
} else {
return 0;
}
Search operation in 2D Array.txt
#include <iostream>
using namespace std;
int main() {
int a[3][3], item, i, j;
bool found = false; // Flag to track if item is found
// Input elements
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
cout << "data[" << i << "][" << j << "]: ";
cin >> a[i][j];
}
}
// Output result
if(found) {
cout << "Data has been found at position: " << i << "\t" << j << endl;
} else {
cout << "Data has not been found." << endl;
}
return 0;
}
Finding the biggest value.
How many numbers: 5
-10 -5 -20 -1 -3
The biggest value is: -1
#include <iostream>
using namespace std;
int main() {
int num[50], n, res, i;
// Input values
for (i = 0; i < n; i++) {
cin >> num[i];
}
cout << "The biggest value is: " << res << endl;
return 0;
}
Stack implementation using array
#include <stdio.h>
#include <stdlib.h>
#define MAX 8
int stack[MAX];
int top = -1;
int isempty() {
return top == -1;
}
int isfull() {
return top == MAX - 1;
}
int peek() {
if (!isempty())
return stack[top];
else {
printf("Stack is empty. Cannot peek.\n");
return -1;
}
}
int pop() {
if (!isempty()) {
int data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is empty.\n");
return -1;
}
}
int main() {
int i;
push(12);
push(14);
push(16);
push(21);
push(24);
push(16);
push(12);
push(10);
push(9); // This one will fail due to full stack
return 0;
}
Stack implementation using STD
#include <iostream>
#include <stack>
void showstack(stack<string> s) {
while (!s.empty()) {
s.pop();
int main() {
stack<string> s;
// Because the stack is now empty, all code after this using `s` will show empty stack.
s.push("A");
s.push("B");
s.push("C");
s.push("D");
cout << "Stack size is: " << s.size() << endl;
cout << "Top element is: " << s.top() << endl;
cout << "After pop Operation, the stack is: " << endl;
s.push("A");
s.push("B");
cout << "Stack size is: " << s.size() << endl;
cout << "Top element is: " << s.top() << endl;
if (s.empty())
else
return 0;
}
Queue implementation using array
#include <iostream>
#define MAX 50
int queue_arr[MAX];
void insert() {
int added_item;
if (rear == MAX - 1) {
return;
} else {
if (front == -1)
front = 0;
rear = rear + 1;
queue_arr[rear] = added_item;
void del() {
return;
} else {
cout << "\nElement deleted from queue is: " << queue_arr[front] << endl;
front = front + 1;
void display() {
int i;
return;
} else {
int main() {
int choice;
while (1) {
switch (choice) {
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
}
Fibonacci Series Using Recursion Process
0 1 1 2 3 5 8 13 21 34
#include <iostream>
int fibonaci(int i) {
if(i == 0) {
return 0;
} else if(i == 1) {
return 1;
} else {
int main() {
int i;
return 0;
}
Factorial Using Recursion Process
#include <iostream>
int factorial(int i) {
if(i <= 1) {
return 1;
} else {
int main() {
int i = 5;
cout << "Factorial of " << i << " is: " << factorial(i) << endl;
return 0;
}
Ackermann’s function
#include <stdio.h>
int main() {
int m, n;
return 0;
if (m == 0)
return n + 1;
else if (n == 0)
else
}
Tower of Hanoi using Recursive
#include <stdio.h>
#include <math.h>
int main() {
int n, moves;
printf("Enter the number of disks you want to insert: ");
scanf("%d", &n);
moves = pow(2, n) - 1;
printf("\nThe number of moves required is = %d\n", moves);
return 0;
}
Finding Biggest & Smallest Value with Position using NAÏVE Method
#include <iostream>
using namespace std;
int main() {
int num[50], n, max, min, max_loc = 0, min_loc = 0;
max = num[0];
min = num[0];
cout << "The biggest value is: " << max << endl;
cout << "And the position is: " << max_loc << endl;
cout << "The smallest value is: " << min << endl;
cout << "And the position is: " << min_loc << endl;
return 0;
}
Linear Search
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d", &n);
scanf("%d", &a[i]);
scanf("%d", &item);
loc = 1;
while(a[loc] != item) {
loc++;
if(loc == n + 1)
else
printf("%d is the location of the item\n", loc);
return 0;
}
2D Array-Linear Search
#include <iostream>
using namespace std;
int main() {
int r, c;
cout << "Enter row size: ";
cin >> r;
cout << "Enter column size: ";
cin >> c;
int a[r][c];
int item = 90, k = 0, R = -1, C = -1;
if (k == 0) {
cout << "Not found\n";
} else {
cout << "Found at position: (" << R << ", " << C << ") Cell\n";
}
return 0;
}
Bubble sort
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, j, temp, a[20];
// Bubble sort
for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
if(a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
return 0;
}
Bubble Sort_Using Swap uesr
#include <stdio.h>
*x = *y;
*y = temp;
int main() {
int n, i, j, d[50];
scanf("%d", &n);
scanf("%d", &d[i]);
}
printf("The bubble sort is:\n");
printf("%4d", d[i]);
printf("\n");
return 0;
}
Quick sort in C++
#include <iostream>
// Move from the right towards left to find an element smaller than pivot
high--;
// Move from the left towards right to find an element greater than pivot
low++;
int main() {
int n;
cin >> n;
Quick_sort(0, n - 1, a);
return 0;
}
Selection Sort without iteration
#include <stdio.h>
#include <stdlib.h>
#define MAX 7
int main() {
int indexMin, i, j;
// Selection sort
indexMin = i;
indexMin = j;
if (indexMin != i) {
intArray[indexMin] = intArray[i];
intArray[i] = temp;
}
// Print sorted list
printf("%d\n", intArray[i]);
return 0;
}
Merge Sort
#include <iostream>
int a[50];
int b[50], k;
b[i++] = a[h++];
} else {
b[i++] = a[j++];
b[i++] = a[h++];
b[i++] = a[j++];
}
// Copy back to original array
a[k] = b[k];
merge_sort(low, mid);
merge_sort(mid + 1, high);
int main() {
int num;
cout << "Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]: ";
cout << "Now, Please Enter the (" << num << ") numbers (ELEMENTS) [THEN PRESS ENTER]: " << endl;
}
merge_sort(1, num);
cout << endl << "So, the sorted list (using MERGE SORT) will be: " << endl << endl;
return 0;
}
Binary Search
#include <stdio.h>
#include <stdlib.h>
int main()
scanf("%d", &n);
scanf("%d", &a[i]);
low = 1;
high = n;
scanf("%d", &item);
high = mid - 1;
else
low = mid + 1;
return 0;
}
Breadth First Search
#include <stdio.h>
#define N 10
q[++rear] = start;
visited[start] = 1;
// Dequeue element
start = q[++front];
if (start == 9)
printf("10\t");
else
printf("%c\t", start + '1'); // '1' = ASCII 49; change to 'A' for letters
q[++rear] = i;
visited[i] = 1;
}
}
int main() {
int adj[N][N] = {
{0,1,1,0,0,0,0,0,0,1},
{0,0,0,0,1,0,0,0,0,1},
{0,0,0,0,1,0,1,0,0,0},
{1,0,1,0,0,1,1,0,0,1},
{0,0,0,0,0,0,1,1,0,0},
{0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,0,0,0,1,1,1},
{0,0,1,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,1,1,0}
};
return 0;
}
#include <iostream>
int main()
{
int a[3][3],n,item,i,j;
return 0;
}