Dsucode
Dsucode
#include <stdio.h>
int main()
{
int arr[] = {5, 10, 15, 20, 25};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 15;
int result = linearSearch(arr, n, key);
if (result == -1)
{
printf("Element not found\n");
}
else
{
printf("Element found at index %d\n", result);
}
return 0;
***********************OUTPUT**********************
#Search a data using Binary search
#include <stdio.h>
if (arr[mid] == key) {
return mid; // Return the index if the key is found
}
int main() {
int arr[] = {5, 10, 15, 20, 25};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 15;
int result = binarySearch(arr, 0, n - 1, key);
if (result == -1) {
printf("Element not found\n");
} else {
printf("Element found at index %d\n", result);
}
return 0;
}
***********************OUTPUT**********************
#Program to sort an array using bubble sort
#include <stdio.h>
int main() {
int arr[] = {5, 3, 8, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
return 0;
}
***********************OUTPUT**********************
#Program to sort an array using selection sort
#include <stdio.h>
int main() {
int arr[] = {15, 72, 8, 20, 61};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
return 0;
}
***********************OUTPUT**********************
#Program to sort an array using insertion sort
#include <stdio.h>
arr[j + 1] = key;
}
}
int main() {
int arr[] = {5, 3, 8, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
***********************OUTPUT**********************
#Perform push and pop operation on stack
#include <stdio.h>
int stack[MAX_SIZE];
int top = -1;
stack[++top] = value;
printf("%d pushed to stack\n", value);
}
void pop()
{
if (top < 0)
{
printf("Stack Underflow\n");
return;
}
int main()
{
push(5);
push(3);
push(8);
pop();
pop();
pop();
return 0;
}
***********************OUTPUT**********************