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

Dsucode

Dsu

Uploaded by

Manthan Bhavsar
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)
5 views

Dsucode

Dsu

Uploaded by

Manthan Bhavsar
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/ 13

#Search a data using linear search

#include <stdio.h>

int linearSearch(int arr[], int n, int key) {


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}

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>

int binarySearch(int arr[], int low, int high, int key) {


while (low <= high) {
int mid = low + (high - low) / 2;

if (arr[mid] == key) {
return mid; // Return the index if the key is found
}

if (arr[mid] < key) {


low = mid + 1;
} else {
high = mid - 1;
}
}

return -1; // Return -1 if the key is not 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>

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


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arr[] = {5, 3, 8, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, n);

printf("Sorted array: ");


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

return 0;
}

***********************OUTPUT**********************
#Program to sort an array using selection sort
#include <stdio.h>

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


for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] and arr[minIndex]
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

int main() {
int arr[] = {15, 72, 8, 20, 61};
int n = sizeof(arr) / sizeof(arr[0]);

selectionSort(arr, n);

printf("Sorted array: ");


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

return 0;
}

***********************OUTPUT**********************
#Program to sort an array using insertion sort
#include <stdio.h>

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


for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}

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>

#define MAX_SIZE 100

int stack[MAX_SIZE];
int top = -1;

void push(int value)


{
if (top >= MAX_SIZE - 1)
{
printf("Stack Overflow\n");
return;
}

stack[++top] = value;
printf("%d pushed to stack\n", value);
}
void pop()
{
if (top < 0)
{
printf("Stack Underflow\n");
return;
}

int value = stack[top--];


printf("%d popped from stack\n", value);
}

int main()
{
push(5);
push(3);
push(8);

pop();
pop();
pop();

return 0;
}

***********************OUTPUT**********************

You might also like