0% found this document useful (0 votes)
13 views70 pages

Breadth First Search

The document contains multiple C programming examples demonstrating various algorithms and data structures, including Breadth First Search, Linear Search, Quicksort, and different methods for inserting elements into arrays. Each section includes code snippets and explanations for the algorithms and their implementations. The document serves as a comprehensive guide for understanding basic searching, sorting algorithms, and array manipulations in C.

Uploaded by

monir.bracu.edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views70 pages

Breadth First Search

The document contains multiple C programming examples demonstrating various algorithms and data structures, including Breadth First Search, Linear Search, Quicksort, and different methods for inserting elements into arrays. Each section includes code snippets and explanations for the algorithms and their implementations. The document serves as a comprehensive guide for understanding basic searching, sorting algorithms, and array manipulations in C.

Uploaded by

monir.bracu.edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Breadth First Search

#include <stdio.h>

#include <stdlib.h>

#define N 10

void bfs(int adj[][N], int visited[], int start) {

int q[N], rear = -1, front = -1, i;

q[++rear] = start;

visited[start] = 1;

while (rear != front) {

start = q[++front];

if (start == 9)

printf("10\t");

else

printf("%c \t", start + 49); // Use 65 for alphabets

for (i = 0; i < N; i++) {

if (adj[start][i] && !visited[i]) {

q[++rear] = i;

visited[i] = 1;

int main() {

int visited[N] = {0};

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}

};

bfs(adj, visited, 0);

return 0;

}
Linear Search Program

#include <stdio.h>

#define MAX 20

// Array of items on which linear search will be conducted.

int intArray[MAX] = {1, 2, 3, 4, 6, 7, 9, 11, 12, 14, 15, 16, 17, 19, 33, 34, 43, 45, 55, 66};

void printline(int count) {

int i;

for (i = 0; i < count - 1; i++) {

printf("=");

printf("=\n");

// This method makes a linear search.

int find(int data) {

int comparisons = 0;

int index = -1;

int i;

// Navigate through all items

for (i = 0; i < MAX; i++) {

comparisons++;

if (data == intArray[i]) {

index = i;

break;

printf("Total comparisons made: %d\n", comparisons);

return index;
}

void display() {

int i;

printf("[");

for (i = 0; i < MAX; i++) {

printf("%d ", intArray[i]);

printf("]\n");

int main() {

printf("Input Array: ");

display();

printline(50);

// Find location of 55

int location = find(55);

if (location != -1)

printf("Element found at location: %d\n", location + 1);

else

printf("Element not found.\n");

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>

#define MAX 100

int A[] = {44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66};

int n = sizeof(A)/sizeof(A[0]);

void swap(int *x, int *y) {

int temp = *x;

*x = *y;

*y = temp;

int partition(int A[], int low, int high) {

int pivot = A[low];

int left = low + 1;

int right = high;

while (left <= right) {

while (left <= right && A[left] <= pivot)

left++;

while (left <= right && A[right] > pivot)

right--;

if (left < right)

swap(&A[left], &A[right]);

}
swap(&A[low], &A[right]);

return right; // final pivot position

void quicksort_iterative(int A[], int n) {

int LOWER[MAX], UPPER[MAX];

int top = -1;

// Push initial bounds of the array

LOWER[++top] = 0;

UPPER[top] = n - 1;

while (top >= 0) {

int high = UPPER[top];

int low = LOWER[top--];

int pivot = partition(A, low, high);

// Left sublist

if (pivot - 1 > low) {

LOWER[++top] = low;

UPPER[top] = pivot - 1;

// Right sublist

if (pivot + 1 < high) {

LOWER[++top] = pivot + 1;

UPPER[top] = high;

void display(int A[], int n) {


for (int i = 0; i < n; i++)

printf("%d ", A[i]);

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

//CODED AND COMPILED USING TURBO C

#include <stdio.h>

int main() {

char opt;

int arr[20], n, i, item;

printf("\nHow many elements you want to enter in the array: ");

scanf("%d", &n);

for (i = 0; i < n; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &arr[i]);

do {

printf("\nEnter the element to be searched: ");

scanf("%d", &item); // Input the item to be searched

for (i = 0; i < n; i++) {

if (item == arr[i]) {

printf("\n%d found at position %d\n", item, i + 1);

break;

if (i == n)

printf("\nItem %d not found in array\n", item);

printf("\nPress (Y/y) to continue: ");

// Clear input buffer before reading character

while ((getchar()) != '\n');


scanf("%c", &opt);

} while (opt == 'Y' || opt == 'y');

return 0;

}
//PROGRAM TO IMPLEMENT THE BINARY SEARCH

//CODED AND COMPILED USING TURBO C

#include <conio.h>

#include <stdio.h>

void main() {

char opt;

int arr[20], start, end, middle, n, i, item;

clrscr();

printf("\nHow many elements you want to enter in the array: ");

scanf("%d", &n);

printf("\nEnter elements in **sorted order**:\n");

for (i = 0; i < n; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &arr[i]);

printf("\n\nPress any key to continue...");

getch();

do {

clrscr();

printf("\nEnter the element to be searched: ");

scanf("%d", &item);

start = 0;

end = n - 1;

middle = (start + end) / 2;

while (item != arr[middle] && start <= end) {

if (item > arr[middle])


start = middle + 1;

else

end = middle - 1;

middle = (start + end) / 2;

if (item == arr[middle])

printf("\n%d found at position %d\n", item, middle + 1);

else

printf("\n%d not found in array\n", item);

printf("\n\nPress (Y/y) to continue: ");

fflush(stdin); // Works in Turbo C only

scanf("%c", &opt);

} while (opt == 'Y' || opt == 'y');

}
Quick Sort Program in C

Implementation in C

#include <stdio.h>

#define MAX 7

int intArray[MAX] = {4, 6, 3, 2, 1, 9, 7};

// Function to print a line of '=' signs

void printline(int count) {

int i;

for (i = 0; i < count; i++) {

printf("=");

printf("\n");

// Function to display the array

void display() {

int i;

printf("[ ");

for (i = 0; i < MAX; i++) {

printf("%d ", intArray[i]);

printf("]\n");

int main() {

printf("Input Array: ");

display();

printline(30); // Print a line of 30 equal signs

return 0;

}
Bubble Sort Program in C

Array elements: [1, 8, 4, 6, 0, 3, 5, 2, 7, 9]

#include <stdio.h>

#include <stdbool.h>

#define MAX 10

int list[MAX] = {1, 8, 4, 6, 0, 3, 5, 2, 7, 9};

void display() {

int i;

printf("[");

for (i = 0; i < MAX; i++) {

printf("%d", list[i]);

if (i < MAX - 1)

printf(", ");

printf("]\n");

int main() {

printf("Array elements: ");

display();

return 0;

}
Accessing Array Elements

#include <iostream>

using namespace std;

int main() {

int a[50], n;

cout << "How many data do you want to insert: ";

cin >> n;

if (n > 50 || n < 1) {

cout << "Please enter a value between 1 and 50." << endl;

return 1;

for (int i = 0; i < n; i++) {

a[i] = i + 200; // fills array with values like 200, 201, ...

cout << "Array Data Elements are:" << endl;

for (int j = 0; j < n; j++) {

cout << "Element[" << j << "]: " << a[j] << endl;

return 0;

}
Accessing Array elements(2dimentional)
#include <iostream>

using namespace std;

int main()
{
int r,c;
cout<<"Enter row size:";
cin>>r;
cout<<"Enter coloum size:";
cin>>c;
int a[r][c];

for(int i=0; i<r; i++)


{
for(int j=0; j<c; j++)
{
cout<<"Element["<<i<<"]["<<j<<"]:";
cin>>a[i][j];
}
}
cout<<"array data elements are:"<<endl;

for(int i=0; i<r; i++)


{
for(int j=0; j<c; j++)
{
cout<<"Element["<<i<<"]["<<j<<"]:"<<a[i][j]<<endl;
}
}
return 0;
}
Insertion After the Given Index of an Array

#include <stdio.h>

int main() {

int array[10] = {1, 2, 4, 5}; // array size large enough to allow insertion

int N = 4; // current number of elements

int i;

int index = 1; // insert after this index (i.e., after array[1])

int value = 3; // value to insert

// print array before insertion

printf("Printing array before insertion −\n");

for(i = 0; i < N; i++) {

printf("array[%d] = %d\n", i, array[i]);

// shift elements to the right to make space

for(i = N; i > index; i--) {

array[i] = array[i - 1];

// insert value

array[index + 1 - 1] = value; // insert at correct position

// increase count

N++;

// print array after insertion

printf("Printing array after insertion −\n");

for(i = 0; i < N; i++) {

printf("array[%d] = %d\n", i, array[i]);

}
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 N = 4; // number of elements in array

int i;

int value = 1; // new data element to be inserted

// print array before insertion

printf("Printing array before insertion −\n");

for(i = 0; i < N; i++) {

printf("array[%d] = %d \n", i, array[i]);

// shift elements to the right

for(i = N; i > 0; i--) {

array[i] = array[i - 1];

// insert value at the beginning

array[0] = value;

// update number of elements

N++;

// print array after insertion

printf("Printing array after insertion −\n");

for(i = 0; i < N; i++) {

printf("array[%d] = %d\n", i, array[i]);

return 0;

}
Insertion at the Given Index of an Array

#include <stdio.h>

int main() {

int array[10] = {1, 2, 4, 5}; // array size increased for safety

int N = 4; // number of elements in array

int i;

int index = 2; // index to insert at (0-based index)

int value = 3; // new data element

// Print array before insertion

printf("Printing array before insertion −\n");

for(i = 0; i < N; i++) {

printf("array[%d] = %d \n", i, array[i]);

// Shift elements to the right

for(i = N; i >= index; i--) {

array[i + 1] = array[i];

// Insert the new value

array[index] = value;

// Increase size

N++;

// Print array after insertion

printf("Printing array after insertion −\n");

for(i = 0; i < N; i++) {

printf("array[%d] = %d\n", i, array[i]);

}
return 0;

Insertion Before the Given Index of an Array

#include <stdio.h>

int main() {

int array[10] = {1, 2, 4, 5}; // increase size to handle insertion

int N = 4; // number of elements in array

int i = 0;

int index = 3; // insert before index 3 (i.e., before element 5)

int value = 3;

// print array before insertion

printf("Printing array before insertion −\n");

for(i = 0; i < N; i++) {

printf("array[%d] = %d \n", i, array[i]);

// shift elements right to make space

for(i = N; i >= index; i--) {

array[i + 1] = array[i];

// insert new value before the given index

array[index] = value;

// increase element count

N++;

// print array after insertion

printf("Printing array after insertion −\n");

for(i = 0; i < N; i++) {


printf("array[%d] = %d \n", i, array[i]);

return 0;

}
Deletion Operation

#include <stdio.h>

int main() {

int LA[] = {1, 3, 5, 7, 8};

int k = 3, n = 5; // delete the 3rd index (4th element)

int i, j;

printf("The original array elements are:\n");

for(i = 0; i < n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

// Deleting element at index k

j = k;

while(j < n) {

LA[j - 1] = LA[j];

j = j + 1;

n = n - 1;

printf("The array elements after deletion:\n");

for(i = 0; i < n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

return 0;

}
Update Operation

#include <stdio.h>

int main() {

int LA[] = {1, 3, 5, 7, 8};

int k = 3, n = 5, item = 10;

int i;

printf("The original array elements are:\n");

for(i = 0; i < n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

LA[k-1] = item;

printf("The array elements after updation:\n");

for(i = 0; i < n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

return 0;

}
Update Operation
#include <iostream>

using namespace std;

int main()
{

int a[20],n,item,i,data;
cout<<"How many data:";
cin>>n;

for(int i=1; i<=n; i++)


{
cout<<"Element["<<i<<"]:";
cin>>a[i];

}
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;
}
}

cout<<"After update, Data elements are:"<<endl;

for(int i=1; i<=n; i++)


{
cout<<"Element["<<i<<"]:"<<a[i]<<endl;

return 0;
}
Search Operation

#include <stdio.h>

int main() {

int LA[] = {1, 3, 5, 7, 8};

int item = 5, n = 5;

int i = 0, j = 0;

printf("The original array elements are:\n");

for(i = 0; i < n; i++) {

printf("LA[%d] = %d \n", i, LA[i]);

while(j < n) {

if(LA[j] == item) {

break;

j = j + 1;

if (j < n) {

printf("Found element %d at position %d\n", item, j + 1);

} else {

printf("Element %d not found in the array\n", item);

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];
}
}

// Input item to search


cout << "Which data do you want to search: ";
cin >> item;

// Search for the item


for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(a[i][j] == item) {
found = true;
break; // Break inner loop
}
}
if(found) break; // Break outer loop if item found
}

// 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;

cout << "How many numbers: ";


cin >> n;

// Input values
for (i = 0; i < n; i++) {
cin >> num[i];
}

// Initialize result with the first element


res = num[0];
for (i = 1; i < n; i++) {
if (num[i] > res)
res = 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;
}
}

void push(int data) {


if (!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
}
}

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

printf("Element at top of the stack: %d\n", peek());


printf("Elements:\n");
for(i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
printf("Stack full: %s\n", isfull() ? "true" : "false");
printf("Stack empty: %s\n", isempty() ? "true" : "false");

return 0;
}
Stack implementation using STD

#include <iostream>

#include <stack>

using namespace std;

void showstack(stack<string> s) {

while (!s.empty()) {

cout << s.top() << endl;

s.pop();

cout << '\n';

int main() {

stack<string> s;

s.push("A"); // Insert "A" in the stack

s.push("B"); // Insert "B" in the stack

s.push("C"); // Insert "C" in the stack

s.push("D"); // Insert "D" in the stack

cout << "The stack is: " << endl;

showstack(s); // NOTE: this will empty the stack `s`!

// Because the stack is now empty, all code after this using `s` will show empty stack.

// Let's re-add elements for proper continuation.

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;

s.pop(); // removing "D"

s.pop(); // removing "C"

cout << "After pop Operation, the stack is: " << endl;

showstack(s); // Again this will empty the stack

// Re-push for remaining operations

s.push("A");

s.push("B");

cout << "Stack size is: " << s.size() << endl;

cout << "Top element is: " << s.top() << endl;

// Check if stack is empty

if (s.empty())

cout << "Stack is Empty" << endl;

else

cout << "Stack is not Empty" << endl;

return 0;

}
Queue implementation using array
#include <iostream>

using namespace std;

#define MAX 50

int queue_arr[MAX];

int rear = -1;

int front = -1;

void insert() {

int added_item;

if (rear == MAX - 1) {

cout << "\nQueue Overflow\n";

return;

} else {

if (front == -1)

front = 0;

cout << "\nInput the element for adding in queue: ";

cin >> added_item;

rear = rear + 1;

queue_arr[rear] = added_item;

void del() {

if (front == -1 || front > rear) {

cout << "\nQueue Underflow\n";

return;

} else {
cout << "\nElement deleted from queue is: " << queue_arr[front] << endl;

front = front + 1;

void display() {

int i;

if (front == -1 || front > rear) {

cout << "\nQueue is empty\n";

return;

} else {

cout << "\nQueue is:\n";

for (i = front; i <= rear; i++)

cout << queue_arr[i] << " ";

cout << "\n";

int main() {

int choice;

while (1) {

cout << "\n1. Insert\n";

cout << "2. Delete\n";

cout << "3. Display\n";

cout << "4. Quit\n";

cout << "\nEnter your choice: ";

cin >> choice;

switch (choice) {

case 1:

insert();
break;

case 2:

del();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

cout << "\nWrong choice\n";

}
Fibonacci Series Using Recursion Process

Fibonacci Series is:

0 1 1 2 3 5 8 13 21 34

#include <iostream>

using namespace std;

int fibonaci(int i) {

if(i == 0) {

return 0;

} else if(i == 1) {

return 1;

} else {

return fibonaci(i - 1) + fibonaci(i - 2);

int main() {

int i;

cout << "Fibonacci Series is:" << endl;

for (i = 0; i < 10; i++) {

cout << "\t" << fibonaci(i);

cout << endl;

return 0;

}
Factorial Using Recursion Process

Factorial of 5 is: 120

#include <iostream>

using namespace std;

int factorial(int i) {

if(i <= 1) {

return 1;

} else {

return i * factorial(i - 1);

int main() {

int i = 5;

cout << "Factorial of " << i << " is: " << factorial(i) << endl;

return 0;

}
Ackermann’s function

#include <stdio.h>

int A(int m, int n); // Function prototype

int main() {

int m, n;

printf("Enter two numbers :: \n");

scanf("%d%d", &m, &n);

printf("\nOUTPUT :: %d\n", A(m, n));

return 0;

int A(int m, int n) {

if (m == 0)

return n + 1;

else if (n == 0)

return A(m - 1, 1);

else

return A(m - 1, A(m, n - 1));

}
Tower of Hanoi using Recursive

#include <stdio.h>
#include <math.h>

void han(int n, char sor, char aux, char des) {


if(n == 1)
printf("%c -> %c\n", sor, des);
else {
han(n - 1, sor, des, aux);
han(1, sor, aux, des);
han(n - 1, aux, sor, des);
}
}

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);

han(n, 'A', 'B', 'C'); // A = source, B = auxiliary, C = destination

return 0;
}
Finding Biggest &amp; 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;

cout << "How many numbers: ";


cin >> n;

for (int i = 0; i < n; i++) {


cout << "num[" << i << "] = ";
cin >> num[i];
}

max = num[0];
min = num[0];

for (int i = 1; i < n; i++) {


if (num[i] > max) {
max = num[i];
max_loc = i;
}
if (num[i] < min) {
min = num[i];
min_loc = i;
}
}

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() {

int n, item, loc, a[31], i; // Declare 31 for safe a[n+1] access

printf("How many numbers do you want: ");

scanf("%d", &n);

for(i = 1; i <= n; i++) {

printf("Enter the num a[%d] = ", i);

scanf("%d", &a[i]);

printf("Which item do you want to search: ");

scanf("%d", &item);

a[n + 1] = item; // Sentinel

loc = 1;

while(a[loc] != item) {

loc++;

if(loc == n + 1)

printf("The item is not found\n");

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;

for(int i = 0; i < r; i++) {


for(int j = 0; j < c; j++) {
cout << "Element[" << i << "][" << j << "]: ";
cin >> a[i][j];
if (a[i][j] == item) {
k++;
R = i;
C = j;
}
}
}

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];

printf("How many numbers do you want (max 20):\n");


scanf("%d", &n);

if(n > 20) {


printf("Maximum allowed is 20.\n");
return 1;
}

for(i = 0; i < n; i++) {


printf("Enter the num a[%d] = ", i);
scanf("%d", &a[i]);
}

// 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;
}
}
}

printf("Sorted sequence is:\n");


for(i = 0; i < n; i++) {
printf("%d\n", a[i]);
}

return 0;
}
Bubble Sort_Using Swap uesr

#include <stdio.h>

void swap(int *x, int *y) {

int temp = *x;

*x = *y;

*y = temp;

int main() {

int n, i, j, d[50];

printf("How many numbers: ");

scanf("%d", &n);

printf("Enter the numbers: ");

for(i = 0; i < n; i++) {

scanf("%d", &d[i]);

for(i = 0; i < n - 1; i++) {

for(j = 0; j < n - i - 1; j++) {

if(d[j] > d[j + 1]) {

swap(&d[j], &d[j + 1]);

}
printf("The bubble sort is:\n");

for(i = 0; i < n; i++) {

printf("%4d", d[i]);

printf("\n");

return 0;

}
Quick sort in C++

#include <iostream>

using namespace std;

int Partition(int low, int high, int arr[]) {

int pivot = arr[low];

while (high > low) {

// Move from the right towards left to find an element smaller than pivot

while (pivot < arr[high] && high > low)

high--;

arr[low] = arr[high]; // Put this smaller element to low index

// Move from the left towards right to find an element greater than pivot

while (pivot > arr[low] && high > low)

low++;

arr[high] = arr[low]; // Put this larger element to high index

arr[low] = pivot; // Place pivot at its correct position

return low; // Return the pivot index

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

if (low < high) {

int piv_index = Partition(low, high, arr);

Quick_sort(low, piv_index - 1, arr);

Quick_sort(piv_index + 1, high, arr);


}

int main() {

int n;

cout << "Enter number of elements: ";

cin >> n;

int* a = new int[n];

cout << "Enter the elements:\n";

for (int i = 0; i < n; i++) {

cin >> a[i];

Quick_sort(0, n - 1, a);

cout << "Final Array After Sorting: ";

for (int i = 0; i < n; i++) {

cout << a[i] << " ";

cout << "\n";

delete[] a; // Free allocated memory

return 0;

}
Selection Sort without iteration

#include <stdio.h>

#include <stdlib.h>

#define MAX 7

int main() {

int intArray[MAX] = {4, 6, 3, 2, 1, 9, 7};

int indexMin, i, j;

// Selection sort

for (i = 0; i < MAX - 1; i++) {

indexMin = i;

// Find index of minimum element in the remaining unsorted part

for (j = i + 1; j < MAX; j++) {

if (intArray[j] < intArray[indexMin]) {

indexMin = j;

// Swap if the minimum is not at the current position

if (indexMin != i) {

int temp = intArray[indexMin];

intArray[indexMin] = intArray[i];

intArray[i] = temp;

}
// Print sorted list

printf("The sorted list is:\n");

for (i = 0; i < MAX; i++) {

printf("%d\n", intArray[i]);

return 0;

}
Merge Sort

#include <iostream>

using namespace std;

int a[50];

void merge(int low, int mid, int high) {

int h = low, i = low, j = mid + 1;

int b[50], k;

while (h <= mid && j <= high) {

if (a[h] <= a[j]) {

b[i++] = a[h++];

} else {

b[i++] = a[j++];

// Copy the remaining elements of left half if any

while (h <= mid) {

b[i++] = a[h++];

// Copy the remaining elements of right half if any

while (j <= high) {

b[i++] = a[j++];

}
// Copy back to original array

for (k = low; k <= high; k++) {

a[k] = b[k];

void merge_sort(int low, int high) {

if (low < high) {

int mid = (low + high) / 2;

merge_sort(low, mid);

merge_sort(mid + 1, high);

merge(low, mid, high);

int main() {

int num;

cout << "******************************************************************************"


<< endl;

cout << " MERGE SORT PROGRAM" << endl;

cout << "******************************************************************************"


<< endl << endl;

cout << "Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]: ";

cin >> num;

cout << "Now, Please Enter the (" << num << ") numbers (ELEMENTS) [THEN PRESS ENTER]: " << endl;

for (int i = 1; i <= num; i++) {

cin >> a[i];

}
merge_sort(1, num);

cout << endl << "So, the sorted list (using MERGE SORT) will be: " << endl << endl;

for (int i = 1; i <= num; i++) {

cout << a[i] << " ";

cout << endl << endl;

return 0;

}
Binary Search

#include <stdio.h>

#include <stdlib.h>

int main()

int n, i, a[100], low, high, mid, item;

printf("How many numbers: ");

scanf("%d", &n);

// Input the numbers (assuming the array is sorted)

for (i = 1; i <= n; i++)

printf("Enter the numbers[%d]: ", i);

scanf("%d", &a[i]);

low = 1;

high = n;

printf("Which item do you want to search: ");

scanf("%d", &item);

// Binary search using a while loop

while (low <= high)

mid = (low + high) / 2;


if (a[mid] == item)

printf("%d is location of the item\n", mid);

return 0; // Item found, exit program

else if (a[mid] > item)

high = mid - 1;

else

low = mid + 1;

// If we reach here, item was not found

printf("Item is not found in the array\n");

return 0;

}
Breadth First Search

#include <stdio.h>

#define N 10

void bfs(int adj[][N], int visited[], int start) {

int q[N], rear = -1, front = -1, i;

// Enqueue starting node

q[++rear] = start;

visited[start] = 1;

while (rear != front) {

// Dequeue element

start = q[++front];

// Print node (adjust output style as needed)

if (start == 9)

printf("10\t");

else

printf("%c\t", start + '1'); // '1' = ASCII 49; change to 'A' for letters

// Enqueue all adjacent unvisited nodes

for (i = 0; i < N; i++) {

if (adj[start][i] && !visited[i]) {

q[++rear] = i;

visited[i] = 1;

}
}

int main() {

int visited[N] = {0};

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}

};

bfs(adj, visited, 0);

return 0;

}
#include <iostream>

using namespace std;

int main()
{
int a[3][3],n,item,i,j;

for(i=0; i<3; i++)


{
for(j=0; j<3; j++)
{
cout << "data["<<i<<"]["<<j<<"]:";
cin>>a[i][j];
}
}
cout << "Which data do you want to search:";
cin>>item;

for(i=0; i<3; i++)


{
for(j=0; j<3; j++)
{
if(a[i][j]==item)
{
break;
}
}
break;
}
if(i==0 && j==3)
{
cout << "Data has not found";
}
else
{
cout << "Data has found at position:"<<i<< "\t"<<j<<endl;
}

return 0;
}

You might also like