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

LAB PROGRAM DSA .WEEK 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

LAB PROGRAM DSA .WEEK 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Linear Search Using C

1 (a) . Linear Search without function:

int main() {
int arr[100], size, target, i, result=0;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
// Input the key to search
printf("Enter the target to search: ");
scanf("%d", &target);
// Input the elements of the array
printf("Enter the elements of the
array:\n");
for(i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < size; i++){
if(arr[i] == target) {
result=1;
break;
}
}
if (result==1)
{
printf("Element found at index
%d\n", i);
}

else {
printf("Element not found in the
array\n");
}

return 0;
}
1(b). Linear Search with function:

int main() {
int arr[100], size, target, i, result;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &size);

// Input the elements of the array


printf("Enter the elements of the array:\
n");
for(i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

// Input the key to search


printf("Enter the target to search: ");
scanf("%d", &target);

// Call linearSearch function


result = linearSearch(arr, size, target);

// Print the result


if(result != -1) {
printf("Element found at index %d\n",
result);
} else {
printf("Element not found in the array\
n");
}

return 0;
}

// Function to per form linear search


int linearSearch(int arr[], int size, int target) {
int i;
for(i = 0; i < size; i++) {
if(arr[i] == target) {
return i;
}
}
return -1;
}
1( c) . Recursive function for linear search

#include <stdio.h>

// Recursive function for linear search


int recursiveLinearSearch(int arr[], int size, int target)
{
// Base case: if size is 0, the element is not present
in the array
if (size == 0)
return -1;

// If the last element matches the target, return its


index
if (arr[size - 1] == target)
return size - 1;

// Otherwise, continue the search in the remaining


part of the array
return recursiveLinearSearch(arr, size - 1, target);
}

int main() {
int size, target;
// Ask user for the size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);

// Declare an array of the given size


int arr[size];

// Ask user to input the elements of the


array
printf("Enter %d elements of the array:\n",
size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

// Ask user for the target value


printf("Enter the target value to search: ");
scanf("%d", &target);

// Call the recursive linear search function


int result = recursiveLinearSearch(arr, size,
target);
// Check the result and print the outcome
if (result != -1)
printf("Element found at index %d\n",
result);
else
printf("Element not found in the array\
n");

return 0;
}

Binary Search Using Python


2 (a) Binary Search without function:

#include <stdio.h>

int main() {
int n, i, key, low, high, mid;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];
printf("Enter %d elements in sorted order: \n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the key to search: ");


scanf("%d", &key);

low = 0;
high = n - 1;

while (low <= high) {


mid = (low + high) / 2;

if (arr[mid] == key) {
printf("Key found at index %d\n", mid);
return 0;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}

printf("Key not found\n");


return 0;
}

2 (b ) Binary Search with function:

#include <stdio.h>

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


int low = 0, high = n - 1;

while (low <= high) {


int mid = (low + high) / 2;

if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}

return -1;
}

int main() {
int n, i, key;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements in sorted order: \n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the key to search: ");


scanf("%d", &key);

int result = binarySearch(arr, n, key);

if (result != -1) {
printf("Key found at index %d\n", result);
} else {
printf("Key not found\n");
}

return 0;
}
2 ( c) Recursive Binary Search with function:

#include <stdio.h>

// Recursive function to perform binary search


int binarySearchRecursive(int arr[], int low, int high, int key) {
if (low <= high) {
int mid = (low + high) / 2; // Calculate the middle index

if (arr[mid] == key) { // Check if the key is found


return mid; // Return the index if found
} else if (arr[mid] < key) { // If the key is greater, search the right half
return binarySearchRecursive(arr, mid + 1, high, key);
} else { // If the key is smaller, search the left half
return binarySearchRecursive(arr, low, mid - 1, key);
}
}
return -1; // Return -1 if the key is not found
}

int main() {
int n, i, key;

// Prompt user to enter the number of elements


printf("Enter the number of elements: ");
scanf("%d", &n); // Read the number of elements

int arr[n]; // Declare an array of size n


printf("Enter %d elements in sorted order: \n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]); // Read n elements into the array
}

// Prompt user to enter the key to search for


printf("Enter the key to search: ");
scanf("%d", &key); // Read the key

// Call the recursive binary search function and store the result
int result = binarySearchRecursive(arr, 0, n - 1, key);

// Check the result and print the appropriate message


if (result != -1) {
printf("Key found at index %d\n", result); // Print the index if found
} else {
printf("Key not found\n"); // Print a message if not found
}

return 0;
}

Linear Search Using Python


1. Linear Search with function:

def linear_search(arr, key):


# Traverse the array from the start to the end
for i in range(len(arr)):
if arr[i] == key:
return i # Return the index if the key is found
return -1 # Return -1 if the key is not found

# Input the number of elements


n = int(input("Enter the number of elements: "))
# Input the elements
arr = []
print(f"Enter {n} elements:")
for i in range(n):
arr.append(int(input()))

# Input the key to search for


key = int(input("Enter the key to search: "))

# Call the linear search function


result = linear_search(arr, key)

# Print the result


if result != -1:
print(f"Key found at index {result}")
else:
print("Key not found")

Binary Search Using Python

2(a). Binary Search with function:


def binary_search_iterative(arr, key):
low, high = 0, len(arr) - 1

while low <= high:


mid = (low + high) // 2

if arr[mid] == key:
return mid # Return the index if the key is found
elif arr[mid] < key:
low = mid + 1 # Search the right half
else:
high = mid - 1 # Search the left half

return -1 # Return -1 if the key is not found

# Input the number of elements


n = int(input("Enter the number of elements: "))

# Input the elements in sorted order


arr = []
print(f"Enter {n} elements in sorted order:")
for i in range(n):
arr.append(int(input()))

# Input the key to search for


key = int(input("Enter the key to search: "))

# Call the binary search function


result = binary_search_iterative(arr, key)

# Print the result


if result != -1:
print(f"Key found at index {result}")
else:
print("Key not found")

2(b). Recursive Binary Search with function using Python:

def binary_search_recursive(arr, low, high, key):


if low <= high:
mid = (low + high) // 2
if arr[mid] == key:
return mid # Return the index if the key is found
elif arr[mid] < key:
return binary_search_recursive(arr, mid + 1, high, key) # Search the right half
else:
return binary_search_recursive(arr, low, mid - 1, key) # Search the left half

return -1 # Return -1 if the key is not found

# Input the number of elements


n = int(input("Enter the number of elements: "))

# Input the elements in sorted order


arr = []
print(f"Enter {n} elements in sorted order:")
for i in range(n):
arr.append(int(input()))

# Input the key to search for


key = int(input("Enter the key to search: "))

# Call the recursive binary search function


result = binary_search_recursive(arr, 0, n - 1, key)

# Print the result


if result != -1:
print(f"Key found at index {result}")
else:
print("Key not found")

You might also like