Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Algorithms and Codes

Uploaded by

eshitvagoyal
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)
7 views

Algorithms and Codes

Uploaded by

eshitvagoyal
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/ 3

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;

You might also like