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

Recursive Algorithm

The document discusses recursion, particularly through the example of the Towers of Hanoi puzzle, which involves moving disks between rods under specific rules. It also covers searching algorithms, including linear and binary search, emphasizing their importance in data retrieval and efficiency. Additionally, it touches on sorting methods, highlighting internal sorting techniques like Bubble Sort, and provides a series of questions related to data structures, asymptotic notations, sorting, and searching.
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)
0 views5 pages

Recursive Algorithm

The document discusses recursion, particularly through the example of the Towers of Hanoi puzzle, which involves moving disks between rods under specific rules. It also covers searching algorithms, including linear and binary search, emphasizing their importance in data retrieval and efficiency. Additionally, it touches on sorting methods, highlighting internal sorting techniques like Bubble Sort, and provides a series of questions related to data structures, asymptotic notations, sorting, and searching.
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/ 5

RECURSION

 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.

The following are rules:


 Only one disk can be moved at a time.
 Each move consists of taking the upper disk from one of the stacks and placing it on
top of another stack
 i.e. a disk can only be moved if it is the uppermost disk on a stack.
 No disk may be placed on top of a smaller disk.
Image illustration for 3 disks :

void TOH(int num,char S,char D,char I) {


if(num>0) {
TOH(num-1,S,I,D);
printf("\n %c --> %c",S,D);
TOH(num-1,I,D,S);
} }

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

Internal Sorting: Records are sorted in main memory (or) RAM


Ex: Bubble Sort, Selection Sort, Insertion Sort.

Cost of accessing data is negligible.


Random access is possible.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy