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

Complete Algorithms and Codes

Uploaded by

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

Complete Algorithms and Codes

Uploaded by

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

Complete Algorithms and Codes

Frequency of Occurrence in Array

#include <iostream>

using namespace std;

void countFrequency(int arr[], int n) {

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

if (arr[i] == -1) continue;

int count = 1;

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

if (arr[i] == arr[j]) {

count++;

arr[j] = -1; // Mark as visited

cout << arr[i] << " occurs " << count << " times." << endl;

int main() {

int arr[] = {1, 2, 2, 3, 4, 1, 5};

int n = 7;

countFrequency(arr, n);

return 0;
}

BFS (Breadth-First Search)

#include <iostream>

using namespace std;

void BFS(int graph[10][10], int start, int n) {

int queue[10], front = 0, rear = 0;

int visited[10] = {0};

cout << "BFS Traversal: ";

queue[rear++] = start;

visited[start] = 1;

while (front < rear) {

int current = queue[front++];

cout << current << " ";

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

if (graph[current][i] && !visited[i]) {

queue[rear++] = i;

visited[i] = 1;

}
cout << endl;

int main() {

int graph[10][10] = {

{0, 1, 1, 0},

{1, 0, 1, 1},

{1, 1, 0, 1},

{0, 1, 1, 0}

};

int n = 4;

BFS(graph, 0, n);

return 0;

DFS (Depth-First Search)

#include <iostream>

using namespace std;

void DFS(int graph[10][10], int visited[], int v, int n) {

cout << v << " ";

visited[v] = 1;

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

if (graph[v][i] && !visited[i])

DFS(graph, visited, i, n);


}

int main() {

int graph[10][10] = {

{0, 1, 1, 0},

{1, 0, 1, 1},

{1, 1, 0, 1},

{0, 1, 1, 0}

};

int n = 4;

int visited[10] = {0};

cout << "DFS Traversal: ";

DFS(graph, visited, 0, n);

cout << endl;

return 0;

Heap Sort

#include <iostream>

using namespace std;

void heapify(int arr[], int n, int i) {

int largest = i;

int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])

largest = left;

if (right < n && arr[right] > arr[largest])

largest = right;

if (largest != i) {

swap(arr[i], arr[largest]);

heapify(arr, n, largest);

void heapSort(int arr[], int n) {

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);

for (int i = n - 1; i >= 0; i--) {

swap(arr[0], arr[i]);

heapify(arr, i, 0);

int main() {

int arr[] = {4, 10, 3, 5, 1};

int n = 5;
heapSort(arr, n);

cout << "Sorted array: ";

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

cout << arr[i] << " ";

cout << endl;

return 0;

Greedy: Fractional Knapsack

#include <iostream>

using namespace std;

void fractionalKnapsack(int weight[], int profit[], int n, int capacity) {

double ratio[10];

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

ratio[i] = (double)profit[i] / weight[i];

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

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

if (ratio[j] < ratio[j + 1]) {

swap(ratio[j], ratio[j + 1]);

swap(weight[j], weight[j + 1]);

swap(profit[j], profit[j + 1]);

}
}

double totalProfit = 0;

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

if (capacity >= weight[i]) {

capacity -= weight[i];

totalProfit += profit[i];

} else {

totalProfit += profit[i] * ((double)capacity / weight[i]);

break;

cout << "Maximum Profit: " << totalProfit << endl;

int main() {

int weight[] = {10, 20, 30};

int profit[] = {60, 100, 120};

int capacity = 50;

int n = 3;

fractionalKnapsack(weight, profit, n, capacity);

return 0;

You might also like