0% found this document useful (0 votes)
7 views5 pages

Assigment 6

The document contains multiple programming assignments that implement various search algorithms in C. It includes linear search for finding an element, binary search for a sorted array, counting occurrences of an element, finding the largest and smallest elements, and identifying a peak element in an array. Each assignment is accompanied by code snippets and expected output descriptions.

Uploaded by

allennagaur
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 views5 pages

Assigment 6

The document contains multiple programming assignments that implement various search algorithms in C. It includes linear search for finding an element, binary search for a sorted array, counting occurrences of an element, finding the largest and smallest elements, and identifying a peak element in an array. Each assignment is accompanied by code snippets and expected output descriptions.

Uploaded by

allennagaur
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/ 5

‭ASSIGNMENT 6‬

‭ .1.Write a program to implement linear search to find an‬


Q
‭element in an array‬‭.‬
‭CODE‬
‭ include <stdio.h>‬
#
‭int linearSearch(int arr[], int n, int target) {‬
‭for (int i = 0; i < n; i++) {‬
‭if (arr[i] == target) {‬
‭return i;‬
‭}‬
‭}‬
‭return -1;‬
‭}‬

‭int main (){‬


‭int arr[]={1,2,3,4,5,6};‬
‭int n =sizeof(arr) / sizeof(arr[0]);‬
‭int target= 6;‬

‭int result = linearSearch(arr, n, target);‬

‭if (result != -1) {‬


‭printf("Element found at index : %d\n", result);‬
‭} else {‬
‭printf("Element not found in the array\n");‬
‭}‬
‭return 0;‬
‭}‬

‭OUTPUT‬
‭ .2.Write a program to implement binary search to find an‬
Q
‭element in a sorted array.‬
‭CODE‬
‭#include <stdio.h>‬

‭int binarySearch(int arr[], int st, int end, int target) {‬


‭while (st <= end) {‬
‭int mid = st + (end - st) / 2;‬

‭if (arr[mid] == target)‬


‭return mid;‬

‭if (arr[mid] < target)‬


‭st = mid + 1;‬

‭else‬
‭end = mid - 1;‬
}‭ ‬
‭return -1;‬
‭}‬

‭int main (){‬


‭int arr[]={1,2,3,4,5,6};‬
‭int n = sizeof(arr) / sizeof(arr[0]);‬
‭int target= 10;‬

‭int result = binarySearch(arr, 0, n - 1, target);‬

‭if (result != -1) {‬


‭printf("Element found at index : %d\n", result);‬
‭} else {‬
‭printf("Element not found in the array\n");‬
‭}‬

‭return 0;‬
}‭ ‬
‭OUTPUT‬
‭ .Q.Write a program to count how many times a target element‬
3
‭appears using linear search in array.‬
‭CODE‬
‭ include <stdio.h>‬
#
‭int linearSearch(int arr[], int size, int target) {‬
‭int count = 0;‬
‭for (int i = 0; i < size; i++) {‬
‭if (arr[i] == target) {‬
‭count++;‬
‭}‬
‭}‬
‭return count;‬
‭}‬

‭int main (){‬


‭int arr[]={1,2,3,4,5,6,7,34,2,3,1,2,4};‬
‭int size = sizeof(arr) / sizeof(arr[0]);;‬
‭int target= 2;‬

‭int count = linearSearch(arr, size, target);‬

‭if (count >0) {‬


‭printf("%d time are Element found\n", count,target);‬
‭} else {‬
‭printf("Element %d not found in the array\n",target);‬
‭}‬

‭return 0;‬
‭}‬
‭OUTPUT‬
‭ .4.Write a program to find the largest and smallest element in‬
Q
‭an array using linear search.‬
‭CODE‬
‭#include <stdio.h>‬

‭void findLargestAndSmallest(int arr[], int size, int *largest, int *smallest) {‬


‭*largest = arr[0];‬
‭*smallest = arr[0];‬

‭for (int i = 1; i < size; i++) {‬


‭if (arr[i] > *largest) {‬
‭*largest = arr[i];‬
‭}‬
‭if (arr[i] < *smallest) {‬
‭*smallest = arr[i];‬
‭}‬
‭}‬
‭}‬

‭int main (){‬


‭int arr[]={1,2,3,4,5,6,7,34,2,3,-1,2,4};‬
‭int size = sizeof(arr) / sizeof(arr[0]);;‬

i‭nt largest, smallest;‬


‭findLargestAndSmallest(arr, size, &largest, &smallest);‬

/‭/ Displaying results‬


‭printf("Largest element: %d\n", largest);‬
‭printf("Smallest element: %d\n", smallest);‬

‭return 0;‬
‭}‬
‭OUTPUT‬
‭ .5.Write a program to find a peak element using binary search‬
Q
‭where elements first increase and then decrease in array.‬
‭CODE‬
‭ include <stdio.h>‬
#
‭int peakElement(int arr[], int n, int st, int end) {‬
‭while(st<end){‬
‭int mid = st + (end-st)/2;‬

‭if ((mid == 0 || arr[mid - 1] <= arr[mid]) &&‬


‭(mid == n - 1 || arr[mid + 1] <= arr[mid])) {‬
‭return mid;‬
‭}‬
‭if(arr[mid]<arr[mid+1]){‬
‭st = mid+1;‬
‭}‬
‭if(arr[mid]>arr[mid+1]){‬
‭end = mid;‬
‭}‬
}‭ ‬
‭return -1;‬
‭}‬

‭int main (){‬


‭int arr[]={10,20,30,34,28,23,11,2,-1};‬
‭int n = sizeof(arr) / sizeof(arr[0]);;‬

‭int peakIndex = peakElement(arr, n, 0, n-1);‬

‭if (peakIndex != -1) {‬


‭printf("Peak element is %d at index: %d\n", arr[peakIndex], peakIndex);‬
‭} else {‬
‭printf("No peak element found.\n");‬
‭}‬
‭return 0;‬
‭}‬
‭OUTPUT‬

You might also like