Professional Documents
Culture Documents
Objective: Program for Linear Search, Binary Search, Selection Sort, Insertion Sortand Bubble
Sort.
Code:
#include <stdio.h>
// Linear Search
int linearSearch(int arr[], int size, int target)
{ for (int i = 0; i < size; i++) {
if (arr[i] == target)
{ return i;
}
}
return -1; // Target not found
}
// Binary Search (Assumes a sorted array) int
binarySearch(int arr[], int size, int target) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target)
{ low = mid + 1;
} else {
high = mid - 1;
}
}
return -1; // Target not found
}
// Selection Sort
void selectionSort(int arr[], int size) { for
(int i = 0; i < size - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}
// Insertion Sort
void insertionSort(int arr[], int size) { for
(int i = 1; i < size; i++) {
int key = arr[i];
Ananya Singh
21002701300032
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
// Bubble Sort
void bubbleSort(int arr[], int size)
{ for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++)
{ if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 22;
// Linear Search
int linearResult = linearSearch(arr, size, target); if
(linearResult != -1) {
printf("Element %d found at index %d using Linear Search.\n", target, linearResult);
} else {
printf("Element %d not found using Linear Search.\n", target);
}
// Binary Search
int binaryResult = binarySearch(arr, size, target); if
(binaryResult != -1) {
printf("Element %d found at index %d using Binary Search.\n", target, binaryResult);
} else {
printf("Element %d not found using Binary Search.\n", target);
}
// Selection Sort
selectionSort(arr, size);
printf("Sorted array using Selection Sort: "); for
(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Insertion Sort
int arrInsertion[] = {12, 11, 13, 5, 6};
int sizeInsertion = sizeof(arrInsertion) / sizeof(arrInsertion[0]); insertionSort(arrInsertion,
sizeInsertion);
printf("Sorted array using Insertion Sort: "); for
(int i = 0; i < sizeInsertion; i++) {
printf("%d ", arrInsertion[i]);
}
Ananya Singh
21002701300032
printf("\n");
// Bubble Sort
bubbleSort(arr, size);
printf("Sorted array using Bubble Sort: "); for
(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Ananya Singh
21002701300032
PROGRAM 2
Ananya Singh
21002701300032
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] =
L[i]; i++;
} else {
arr[k] = R[j]; j+
+;
} k+
+;
}
while (i < n1) {
arr[k] =
L[i]; i++;
k++;
}
Ananya Singh
21002701300032
}
switch (choice)
{ case 1:
quickSort(arr, 0, size - 1);
printf("Sorted array using Quick Sort: ");
break;
case 2:
mergeSort(arr, 0, size - 1);
printf("Sorted array using Merge Sort: ");
break;
case 3:
heapSort(arr, size);
printf("Sorted array using Heap Sort: ");
break;
default:
printf("Invalid choice.\n");
return 1;
}
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Ananya Singh
21002701300032
PROGRAM 3
Code:
#include <stdio.h>
// Shell Sort
void shellSort(int arr[], int size) {
for (int gap = size / 2; gap > 0; gap /= 2) { for
(int i = gap; i < size; i++) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { arr[j]
= arr[j - gap];
}
arr[j] = temp;
}
}
}
// Counting Sort
void countingSort(int arr[], int size) { int
max = arr[0];
for (int i = 1; i < size; i++)
{ if (arr[i] > max) {
max = arr[i];
}
}
int count[max + 1];
int output[size];
Ananya Singh
21002701300032
int arr1[] = {12, 34, 54, 2, 3};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
shellSort(arr1, size1);
printf("Sorted array using Shell Sort: ");
for (int i = 0; i < size1; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
int arr2[] = {4, 2, 2, 8, 3, 3, 1};
int size2 = sizeof(arr2) / sizeof(arr2[0]);
countingSort(arr2, size2);
printf("Sorted array using Counting Sort: "); for
(int i = 0; i < size2; i++) {
printf("%d ", arr2[i]);
}
printf("\n");
return 0;
}
Ananya Singh
21002701300032
PROGRAM 4
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Graph {
int numVertices;
struct Node* adjacencyList[MAX_VERTICES];
};
return graph;
}
// Breadth-First Search
void BFS(struct Graph* graph, int startVertex)
{ int visited[MAX_VERTICES] = {0};
int queue[MAX_VERTICES];
int front = -1, rear = -1;
visited[startVertex] = 1;
queue[++rear] = startVertex;
printf("\n");
Ananya Singh 2100270130032
}
int main() {
// Create a graph with 5 vertices
struct Graph* graph =
createGraph(5);
return 0;
}
Code:
#include <stdio.h>
#include
<stdlib.h>
#include <limits.h>
#define V 5
// Structure to represent an edge in the
graph struct Edge {
int src, dest, weight;
};
// Structure to represent a subset for union-find
struct Subset {
int parent, rank;
};
// Function to compare two edges based on their weights
int compareEdges(const void *a, const void *b) {
return ((struct Edge*)a)->weight - ((struct Edge*)b)->weight;
}
kruskalMST(graph);
primMST(graph);
return 0;
}
Output:
Kruskal's Algorithm - Minimum Cost Spanning Tree:
0-1 2
0-3 6
1-2 3
1-4 5
Objective: Program for Single Source Shortest path using Dijkstra's Algorithm
Code:
#include <stdio.h>
#include <limits.h>
#define V 6
int minDistance(int dist[], int sptSet[])
{ int min = INT_MAX, min_index;
for (int v = 0; v < V; v++) {
if (sptSet[v] == 0 && dist[v] <= min)
{ min = dist[v];
min_index = v;
}
}
return min_index;
}
void printSolution(int dist[]) {
printf("Vertex \t\t Distance from Source Vertex\
n"); for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
// Function to implement Dijkstra's algorithm for a given graph
void dijkstra(int graph[V][V], int src) {
int dist[V]; // Array to store the shortest distances from the source vertex
int sptSet[V];
for (int i = 0; i < V; i++)
{ dist[i] = INT_MAX;
sptSet[i] = 0;
}
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V - 1; count++)
{ int u = minDistance(dist, sptSet);
sptSet[u] = 1;
for (int v = 0; v < V; v++) {
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
{ dist[v] = dist[u] + graph[u][v];
}
}
}
printSolution(dist);
}
int main() {
dijkstra(graph, sourceVertex);
return 0;
}
Output:
Vertex Distance from Source Vertex
0 0
1 1
2 3
3 7
4 10
5 9
Code:
#include <stdio.h>
#include <limits.h>
#define V 4
void printSolution(int dist[][V]);
// Warshall's algorithm for All-Pairs Shortest
Paths void warshall(int graph[][V]) {
int dist[V][V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j+
+)
dist[i][j] = graph[i][j];
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printf("Warshall's Algorithm - All-Pairs Shortest Paths:\n");
printSolution(dist);
}
// Hamiltonian Cycles
#define V 5
if (!graph[path[pos - 1]][v])
return 0;
if (path[i] == v)
return 0;
return 1;
if (pos == V) {
if (graph[path[pos - 1]][path[0]] == 1)
return 1;
else
return 0;
{ path[pos] = v;
return 1;
return 0;
int path[V];
path[i] = -1;
path[0] = 0;
n"); else
printSolution(path);
printf("%d\n", path[0]);
#define N 4
int isSubsetSum(int set[], int n, int sum, int subset[], int index) {
printSubset(subset, index);
return 1;
if (index == n)
return 0;
subset[index] = set[index];
return 1;
{ int subset[N];
printf("\n");
int main() {
// Hamiltonian Cycles
int graph[V][V] = {
{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0}
};
hamiltonianCycle(graph);
subsetSum(set, N, sum);
return 0;
Output:
Hamiltonian Cycle Solution:
012430
return 0;
Output:
Pattern found at index 0
Pattern found at index 9
Pattern found at index
13