Recursive Algorithm
Recursive Algorithm
The process in which a function calls itself directly or indirectly is called recursion and
the corresponding function is called as recursive function.
Using recursive algorithm, certain problems can be solved quite easily.
Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder
Tree Traversals, DFS of Graph, etc.
Towers of Hanoi
It is a mathematical puzzle where we have three rods and n disks. The objective of the
puzzle is to move the entire stack to another rod.
SEARCHING :
The primary objective of searching is to determine whether the desired element exists within the
data, and if so, to identify its precise location or retrieve it. It plays an important role in various
computational tasks and real-world applications, including information retrieval, data analysis,
decision-making processes, and more.
Importance of Searching in DSA
Efficiency: Efficient searching algorithms improve program performance.
Data Retrieval: Quickly find and retrieve specific data from large datasets.
Database Systems: Enables fast querying of databases.
Problem Solving: Used in a wide range of problem-solving tasks.
Linear/Sequential Search
In computer science, searching is the process of finding an item with specified properties from a
collection of items.
In computer science, linear search or sequential search is a method for finding a
particular value in a list that consists of checking every one of its elements, one at a time
and in sequence, until the desired one is found.
Linear search is the simplest search algorithm.
It is a special case of brute-force search. Its worst case cost is proportional to the
number of elements in the list.
Algorithm
# Input: Array A, integer key
# Output: first index of key in A, # or -1 if not found
for i = 0 to last index of A:
if A[i] equals key:
return i
return -1
Program
#include <stdio.h>
void main() {
int array[100], key, i, n;
printf("Enter the number of elements in array\n"); scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++) {
printf("Array[%d]=", i);
scanf("%d", &array[i]);
}
printf("Enter the number to search\n"); scanf("%d", &key);
for (i = 0; i < n; i++) {
if (array[i] == key) { /* if required element found */
printf("%d is present at location %d.\n", key, i+1); break;
}
}
if (i == n) {
printf("%d is not present in array.\n", search);
} }
Binary Search
If we have an array that is sorted, we can use a much more efficient algorithm called a
Binary Search.
In binary search each time we divide array into two equal half and compare middle
element with search element.
If middle element is equal to search element then we got that element and return that
index otherwise if middle element is less than search element we look right part of array
and if middle element is greater than search element we look left part of array.
Program:
#include <stdio.h>
void main() {
int i, first, last, middle, n, key, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
p rintf("Enter %d integers in sorted order\n", n);
for ( i = 0 ; i < n ; i++ )
scanf("%d",&array[i]);
printf("Enter value to find\n");
scanf("%d",&key);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last ) {
if (array[middle] == key) {
printf("%d found at location %d.\n", key, middle+1);
break;
}
else if ( array[middle]>key )
last = middle-1;
else
first = middle + 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", key);
//getch();
}
SORTING:
Most of the data is requested in sorted in sorted order
Example : Find students in increasing GPA order.
Sorting is useful for eliminating duplicate copies in a collection of records.
sorting is memory based like
1. Internal Sorting (or) Inplace sorting.
2. External Sorting
1.Define data structure. Discuss different types of data structure their implementations
2.Explain about asymptotic notations
3.Define sorting Discuss Bubble sort for following elements 10, 6, 3, 7, 17, 26,56,32,72
4.Define searching and explain binary search with an example