0% found this document useful (0 votes)
20 views21 pages

Program 1: W A P To Search An Element in Array Using: - I) Linear Search (Iterative & Recursive)

Uploaded by

Utkarsh Miglani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views21 pages

Program 1: W A P To Search An Element in Array Using: - I) Linear Search (Iterative & Recursive)

Uploaded by

Utkarsh Miglani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PROGRAM 1

W A P to search an element in array using: -


I) Linear search (iterative & recursive)
Code
#include <stdio.h>
int linearSearchIterative(int arr[], int n, int x)
{
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
int linearSearchRecursive(int arr[], int x, int start, int end)
{
if (start > end) {
return -1;
}

if (arr[start] == x) {
return start;
}
return linearSearchRecursive(arr, x, start + 1, end);
}
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 30;
int indexIterative = linearSearchIterative(arr, n, x);
if (indexIterative != -1) {
printf("%d is present at index: %d (Iterative search)\n", x, indexIterative);
} else {
printf("%d is not present in the array (Iterative search)\n", x);
}
int indexRecursive = linearSearchRecursive(arr, x, 0, n - 1);
if (indexRecursive != -1) {
printf("%d is present at index: %d (Recursive search)\n", x, indexRecursive);
} else {
printf("%d is not present in the array (Recursive search)\n", x);
}
return 0;
}
Output
ii. Binary search (iterative & recursive)
Code
#include <stdio.h>
int binarySearchIterative(int arr[], int n, int x)
{
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) {
return mid;
}
if (arr[mid] < x) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int binarySearchRecursive(int arr[], int x, int left, int right)
{
if (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) {
return mid;
}
if (arr[mid] < x) {
return binarySearchRecursive(arr, x, mid + 1, right);
} else {
return binarySearchRecursive(arr, x, left, mid - 1);
}
}
return -1;
}

int main()
{
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 30;
int indexIterative = binarySearchIterative(arr, n, x);
if (indexIterative != -1) {
printf("%d is present at index: %d (Iterative binary search)\n", x,
indexIterative);
} else {
printf("%d is not present in the array (Iterative binary search)\n", x);
}
int indexRecursive = binarySearchRecursive(arr, x, 0, n - 1);
if (indexRecursive != -1) {
printf("%d is present at index: %d (Recursive binary search)\n", x,
indexRecursive);
} else {
printf("%d is not present in the array (Recursive binary search)\n", x);
}
return 0;
}
Output
PROGRAM 2
2.W A P to sort an array using menu driven program.
I. Selection sort
Code
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
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], &arr[minIndex]);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int choice, n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
do {
printf("\nMenu:\n");
printf("1. Sort the array using Selection Sort\n");
printf("2. Print the sorted array\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
selectionSort(arr, n);
printf("Array sorted using Selection Sort.\n");
break;
case 2:
printf("Sorted array: ");
printArray(arr, n);
break;
case 3:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please enter a valid choice.\n");
}
} while (choice != 3);
return 0;
}
Output
ii. Bubble sort
Code
#include <stdio.h>
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int choice, n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
do {
printf("\nMenu:\n");
printf("1. Sort the array using Bubble Sort\n");
printf("2. Print the sorted array\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
bubbleSort(arr, n);
printf("Array sorted successfully using Bubble Sort.\n");
break;
case 2:
printf("Sorted array: ");
printArray(arr, n);
break;
case 3:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please enter a valid choice.\n");
}
} while (choice != 3);
return 0;
}
Output
iii)Merge Sort
Code
#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
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++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

// Merge the sorted halves


merge(arr, l, m, r);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
int choice;
printf("Menu:\n");
printf("1. Sort the array using Merge Sort\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Given array is \n");
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size);
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Output
iv. Insertion sort
Code
#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
int choice;
printf("Enter your choice:\n");
printf("1. Sort the array using Insertion Sort\n");
printf("2. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Array before sorting: \n");
printArray(arr, n);
insertionSort(arr, n);
printf("Array after sorting: \n");
printArray(arr, n);
break;
case 2:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}

You might also like