SlideShare una empresa de Scribd logo
1 de 3
Write a recursive version of an array-based linear search algorithm.
Write a recursive version of a linked-list-based linear search algorithm.
Solution
// Recursive C program to implement linear search on array
#include <stdio.h>
//A linear search algorithm to find and return index of the searched element
int array_linear_search_recursion(int numbers[], int length, int element, int index)
{
if(index<length)
{
if(numbers[index] == element)
return index;
else
return array_linear_search_recursion(numbers, length, element, index+1);
}
else
{
// element not present
return -1;
}
}
int main()
{
int array[] = {44, 5, 78, 90, 111, 3, 45, 133, 54, 2, 3}; // input array
int element = 5; //element to be searched
int length = sizeof(array)/sizeof(array[0]);
int result = array_linear_search_recursion(array, length, element, 0); //calling function to apply
linear search recursively
if(result == -1)
printf("Element is not present in array");
else
printf("Element is present at index %d", result);
return 0;
}
// Recursive C program to implement linear search on linked list
#include <stdio.h>
#include <stdlib.h>
/* Link list node */
struct node
{
int key;
struct node* next;
};
/* push a new node on the front of the list. */
void push(struct node** head, int new_element)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
/* put in the key */
new_node->key = new_element;
/* link the old list off the new node */
new_node->next = (*head);
/* move the head to point to the new node */
(*head) = new_node;
}
/* Counts no. of nodes in linked list */
int search(struct node* head, int target)
{
// Base case
if (head == NULL)
return 0;
// If key is present in current node, return true
if (head->key == target)
return 1;
// Recursion for remaining list
return search(head->next, target);
}
/* Drier program to test count function*/
int main()
{
/* create an empty list */
struct node* head = NULL;
int target = 77;
/* Use push() to construct below list
55->77->87->3->9 */
push(&head, 9);
push(&head, 3);
push(&head, 87);
push(&head, 77);
push(&head, 55);
if(search(head, target) == 1)
printf("Element is present in Linked list ");
else
printf("Element is not not present in Linked list ");
return 0;
}

Más contenido relacionado

Similar a Write a recursive version of an array-based linear search algorithm- W.docx

Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfankit11134
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfherminaherman
 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docxajoy21
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdfaathmaproducts
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdfarihantelehyb
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfcallawaycorb73779
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdfaathiauto
 
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...davidwarner122
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docxajoy21
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docxKomlin1
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdfalmaniaeyewear
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfaminbijal86
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxtienlivick
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfrajkumarm401
 
Java Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docxJava Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docxmichael1810
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdfsudhirchourasia86
 

Similar a Write a recursive version of an array-based linear search algorithm- W.docx (20)

Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdf
 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docx
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
 
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
 
Data structure
Data  structureData  structure
Data structure
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
 
Java Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docxJava Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docx
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
 

Más de noreendchesterton753

Suppose the population of interest in a particular study is all Queen'.docx
Suppose the population of interest in a particular study is all Queen'.docxSuppose the population of interest in a particular study is all Queen'.docx
Suppose the population of interest in a particular study is all Queen'.docxnoreendchesterton753
 
Suppose the Federal Reserve increases deposits at financial institutio.docx
Suppose the Federal Reserve increases deposits at financial institutio.docxSuppose the Federal Reserve increases deposits at financial institutio.docx
Suppose the Federal Reserve increases deposits at financial institutio.docxnoreendchesterton753
 
Suppose that Edison and Hilary represent the only two consumers of blu.docx
Suppose that Edison and Hilary represent the only two consumers of blu.docxSuppose that Edison and Hilary represent the only two consumers of blu.docx
Suppose that Edison and Hilary represent the only two consumers of blu.docxnoreendchesterton753
 
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docx
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docxSuppose that 42- of Americans are obese- A) If 100 people are randomly.docx
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docxnoreendchesterton753
 
Suppose I am teaching a class and the test comes and goes with two stu.docx
Suppose I am teaching a class and the test comes and goes with two stu.docxSuppose I am teaching a class and the test comes and goes with two stu.docx
Suppose I am teaching a class and the test comes and goes with two stu.docxnoreendchesterton753
 
Suppose an economy is given by the following- Population -274 million.docx
Suppose an economy is given by the following- Population -274 million.docxSuppose an economy is given by the following- Population -274 million.docx
Suppose an economy is given by the following- Population -274 million.docxnoreendchesterton753
 
Suppose a researcher conducted a study examining the effectiveness of.docx
Suppose a researcher conducted a study examining the effectiveness of.docxSuppose a researcher conducted a study examining the effectiveness of.docx
Suppose a researcher conducted a study examining the effectiveness of.docxnoreendchesterton753
 
Supoese you have 'n' different individuals- and all of them to rearran.docx
Supoese you have 'n' different individuals- and all of them to rearran.docxSupoese you have 'n' different individuals- and all of them to rearran.docx
Supoese you have 'n' different individuals- and all of them to rearran.docxnoreendchesterton753
 
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docxSunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docxnoreendchesterton753
 
Sunland Company has the following data-Compute total manufacturing cos.docx
Sunland Company has the following data-Compute total manufacturing cos.docxSunland Company has the following data-Compute total manufacturing cos.docx
Sunland Company has the following data-Compute total manufacturing cos.docxnoreendchesterton753
 
Successful negotiating includes some kind of manipulation in order to.docx
Successful negotiating includes some kind of manipulation in order to.docxSuccessful negotiating includes some kind of manipulation in order to.docx
Successful negotiating includes some kind of manipulation in order to.docxnoreendchesterton753
 
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docx
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docxSubsidiary holds an allocated net operating loss (NOL) when it leaves.docx
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docxnoreendchesterton753
 
Substances that dissolve readily in water are termed hydrophilic- They.docx
Substances that dissolve readily in water are termed hydrophilic- They.docxSubstances that dissolve readily in water are termed hydrophilic- They.docx
Substances that dissolve readily in water are termed hydrophilic- They.docxnoreendchesterton753
 
subject FASHION The combination of which Roman terms most closely co.docx
subject FASHION   The combination of which Roman terms most closely co.docxsubject FASHION   The combination of which Roman terms most closely co.docx
subject FASHION The combination of which Roman terms most closely co.docxnoreendchesterton753
 
Styling Conventions By only changing the case of some of the letters-.docx
Styling Conventions By only changing the case of some of the letters-.docxStyling Conventions By only changing the case of some of the letters-.docx
Styling Conventions By only changing the case of some of the letters-.docxnoreendchesterton753
 
Students spend an average of $520 each semester on textbooks- Suppose.docx
Students spend an average of $520 each semester on textbooks- Suppose.docxStudents spend an average of $520 each semester on textbooks- Suppose.docx
Students spend an average of $520 each semester on textbooks- Suppose.docxnoreendchesterton753
 
Species interactions determine an organism's fundamental niche- True F.docx
Species interactions determine an organism's fundamental niche- True F.docxSpecies interactions determine an organism's fundamental niche- True F.docx
Species interactions determine an organism's fundamental niche- True F.docxnoreendchesterton753
 
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docxSpongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docxnoreendchesterton753
 
SPSS output of a binomial probability distribution with succes probabi.docx
SPSS output of a binomial probability distribution with succes probabi.docxSPSS output of a binomial probability distribution with succes probabi.docx
SPSS output of a binomial probability distribution with succes probabi.docxnoreendchesterton753
 
Starting on the day Holly was born- her mother has invested $30 at the.docx
Starting on the day Holly was born- her mother has invested $30 at the.docxStarting on the day Holly was born- her mother has invested $30 at the.docx
Starting on the day Holly was born- her mother has invested $30 at the.docxnoreendchesterton753
 

Más de noreendchesterton753 (20)

Suppose the population of interest in a particular study is all Queen'.docx
Suppose the population of interest in a particular study is all Queen'.docxSuppose the population of interest in a particular study is all Queen'.docx
Suppose the population of interest in a particular study is all Queen'.docx
 
Suppose the Federal Reserve increases deposits at financial institutio.docx
Suppose the Federal Reserve increases deposits at financial institutio.docxSuppose the Federal Reserve increases deposits at financial institutio.docx
Suppose the Federal Reserve increases deposits at financial institutio.docx
 
Suppose that Edison and Hilary represent the only two consumers of blu.docx
Suppose that Edison and Hilary represent the only two consumers of blu.docxSuppose that Edison and Hilary represent the only two consumers of blu.docx
Suppose that Edison and Hilary represent the only two consumers of blu.docx
 
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docx
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docxSuppose that 42- of Americans are obese- A) If 100 people are randomly.docx
Suppose that 42- of Americans are obese- A) If 100 people are randomly.docx
 
Suppose I am teaching a class and the test comes and goes with two stu.docx
Suppose I am teaching a class and the test comes and goes with two stu.docxSuppose I am teaching a class and the test comes and goes with two stu.docx
Suppose I am teaching a class and the test comes and goes with two stu.docx
 
Suppose an economy is given by the following- Population -274 million.docx
Suppose an economy is given by the following- Population -274 million.docxSuppose an economy is given by the following- Population -274 million.docx
Suppose an economy is given by the following- Population -274 million.docx
 
Suppose a researcher conducted a study examining the effectiveness of.docx
Suppose a researcher conducted a study examining the effectiveness of.docxSuppose a researcher conducted a study examining the effectiveness of.docx
Suppose a researcher conducted a study examining the effectiveness of.docx
 
Supoese you have 'n' different individuals- and all of them to rearran.docx
Supoese you have 'n' different individuals- and all of them to rearran.docxSupoese you have 'n' different individuals- and all of them to rearran.docx
Supoese you have 'n' different individuals- and all of them to rearran.docx
 
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docxSunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
Sunspot Beverages- Limited- of Fiji uses the weighted-average method i.docx
 
Sunland Company has the following data-Compute total manufacturing cos.docx
Sunland Company has the following data-Compute total manufacturing cos.docxSunland Company has the following data-Compute total manufacturing cos.docx
Sunland Company has the following data-Compute total manufacturing cos.docx
 
Successful negotiating includes some kind of manipulation in order to.docx
Successful negotiating includes some kind of manipulation in order to.docxSuccessful negotiating includes some kind of manipulation in order to.docx
Successful negotiating includes some kind of manipulation in order to.docx
 
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docx
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docxSubsidiary holds an allocated net operating loss (NOL) when it leaves.docx
Subsidiary holds an allocated net operating loss (NOL) when it leaves.docx
 
Substances that dissolve readily in water are termed hydrophilic- They.docx
Substances that dissolve readily in water are termed hydrophilic- They.docxSubstances that dissolve readily in water are termed hydrophilic- They.docx
Substances that dissolve readily in water are termed hydrophilic- They.docx
 
subject FASHION The combination of which Roman terms most closely co.docx
subject FASHION   The combination of which Roman terms most closely co.docxsubject FASHION   The combination of which Roman terms most closely co.docx
subject FASHION The combination of which Roman terms most closely co.docx
 
Styling Conventions By only changing the case of some of the letters-.docx
Styling Conventions By only changing the case of some of the letters-.docxStyling Conventions By only changing the case of some of the letters-.docx
Styling Conventions By only changing the case of some of the letters-.docx
 
Students spend an average of $520 each semester on textbooks- Suppose.docx
Students spend an average of $520 each semester on textbooks- Suppose.docxStudents spend an average of $520 each semester on textbooks- Suppose.docx
Students spend an average of $520 each semester on textbooks- Suppose.docx
 
Species interactions determine an organism's fundamental niche- True F.docx
Species interactions determine an organism's fundamental niche- True F.docxSpecies interactions determine an organism's fundamental niche- True F.docx
Species interactions determine an organism's fundamental niche- True F.docx
 
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docxSpongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
Spongy moths- Lymantria dispar- have four life cycle stages- egg- cate.docx
 
SPSS output of a binomial probability distribution with succes probabi.docx
SPSS output of a binomial probability distribution with succes probabi.docxSPSS output of a binomial probability distribution with succes probabi.docx
SPSS output of a binomial probability distribution with succes probabi.docx
 
Starting on the day Holly was born- her mother has invested $30 at the.docx
Starting on the day Holly was born- her mother has invested $30 at the.docxStarting on the day Holly was born- her mother has invested $30 at the.docx
Starting on the day Holly was born- her mother has invested $30 at the.docx
 

Último

Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 

Último (20)

Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 

Write a recursive version of an array-based linear search algorithm- W.docx

  • 1. Write a recursive version of an array-based linear search algorithm. Write a recursive version of a linked-list-based linear search algorithm. Solution // Recursive C program to implement linear search on array #include <stdio.h> //A linear search algorithm to find and return index of the searched element int array_linear_search_recursion(int numbers[], int length, int element, int index) { if(index<length) { if(numbers[index] == element) return index; else return array_linear_search_recursion(numbers, length, element, index+1); } else { // element not present return -1; } } int main() { int array[] = {44, 5, 78, 90, 111, 3, 45, 133, 54, 2, 3}; // input array int element = 5; //element to be searched int length = sizeof(array)/sizeof(array[0]); int result = array_linear_search_recursion(array, length, element, 0); //calling function to apply linear search recursively if(result == -1)
  • 2. printf("Element is not present in array"); else printf("Element is present at index %d", result); return 0; } // Recursive C program to implement linear search on linked list #include <stdio.h> #include <stdlib.h> /* Link list node */ struct node { int key; struct node* next; }; /* push a new node on the front of the list. */ void push(struct node** head, int new_element) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the key */ new_node->key = new_element; /* link the old list off the new node */ new_node->next = (*head); /* move the head to point to the new node */ (*head) = new_node; } /* Counts no. of nodes in linked list */ int search(struct node* head, int target) { // Base case if (head == NULL) return 0; // If key is present in current node, return true if (head->key == target) return 1; // Recursion for remaining list return search(head->next, target); }
  • 3. /* Drier program to test count function*/ int main() { /* create an empty list */ struct node* head = NULL; int target = 77; /* Use push() to construct below list 55->77->87->3->9 */ push(&head, 9); push(&head, 3); push(&head, 87); push(&head, 77); push(&head, 55); if(search(head, target) == 1) printf("Element is present in Linked list "); else printf("Element is not not present in Linked list "); return 0; }