SlideShare une entreprise Scribd logo
1  sur  10
/* Write C programs that use both recursive and non recursive functions to perform

    the following searching operations for a Key value in a given list of integers :

    ii) Binary search*/



#include <stdio.h>

#define MAX_LEN 10



/* Non-Recursive function*/

void b_search_nonrecursive(int l[],int num,int ele)

{

    int l1,i,j, flag = 0;

    l1 = 0;

    i = num-1;

    while(l1 <= i)

    {

        j = (l1+i)/2;

        if( l[j] == ele)

        {

               printf("nThe element %d is present at position %d in listn",ele,j);

                        flag =1;

                        break;

        }

        else

                 if(l[j] < ele)
l1 = j+1;

                 else

                           i = j-1;

    }

    if( flag == 0)

    printf("nThe element %d is not present in the listn",ele);

}



/* Recursive function*/

int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)

{

    int m,pos;

    if (arrayStart<=arrayEnd)

    {

        m=(arrayStart+arrayEnd)/2;

        if (l[m]==a)

         return m;

        else if (a<l[m])

         return b_search_recursive(l,arrayStart,m-1,a);

        else

         return b_search_recursive(l,m+1,arrayEnd,a);

    }

    return -1;

}
void read_list(int l[],int n)

{

    int i;

    printf("nEnter the elements:n");

    for(i=0;i<n;i++)

       scanf("%d",&l[i]);

}



void print_list(int l[],int n)

{

    int i;

    for(i=0;i<n;i++)

       printf("%dt",l[i]);

}



/*main function*/

void main()

{

    int l[MAX_LEN], num, ele,f,l1,a;

    int ch,pos;



    clrscr();
printf("======================================================");

printf("ntttMENU");

printf("n=====================================================");

printf("n[1] Binary Search using Recursion method");

printf("n[2] Binary Search using Non-Recursion method");

printf("nnEnter your Choice:");

scanf("%d",&ch);



if(ch<=2 & ch>0)

{

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

    scanf("%d",&num);

    read_list(l,num);

    printf("nElements present in the list are:nn");

    print_list(l,num);

    printf("nnEnter the element you want to search:nn");

    scanf("%d",&ele);




switch(ch)

{

    case 1:printf("nRecursive method:n");

            pos=b_search_recursive(l,0,num,ele);

            if(pos==-1)
{

                        printf("Element is not found");

               }

               else

               {

                        printf("Element is found at %d position",pos);

               }

               getch();

               break;



        case 2:printf("nNon-Recursive method:n");

               b_search_nonrecursive(l,num,ele);

               getch();

               break;

        }

    }

getch();

}
/* Write C programs that use both recursive and non recursive functions

to perform the following searching operation for a Key value in a given list of integers :

i) Linear search */



#include <stdio.h>

#define MAX_LEN 10



void l_search_recursive(int l[],int num,int ele);

void l_search(int l[],int num,int ele);

void read_list(int l[],int num);

void print_list(int l[],int num);



void main()

{

    int l[MAX_LEN], num, ele;

    int ch;



    clrscr();
printf("======================================================");

printf("ntttMENU");

printf("n=====================================================");

printf("n[1] Linary Search using Recursion method");

printf("n[2] Linary Search using Non-Recursion method");

printf("nnEnter your Choice:");

scanf("%d",&ch);



if(ch<=2 & ch>0)

{

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

scanf("%d",&num);

read_list(l,num);

printf("nElements present in the list are:nn");

print_list(l,num);

printf("nnElement you want to search:nn");

scanf("%d",&ele);



switch(ch)

{

    case 1:printf("n**Recursion method**n");

           l_search_recursive(l,num,ele);

           getch();

           break;
case 2:printf("n**Non-Recursion method**n");

                    l_search_nonrecursive(l,num,ele);

                    getch();

                    break;

        }

    }

    getch();

}

/*end main*/



/* Non-Recursive method*/

void l_search_nonrecursive(int l[],int num,int ele)

{

    int j, f=0;

    for(j=0;j<num;j++)

    if( l[j] == ele)

    {

        printf("nThe element %d is present at position %d in listn",ele,j);

        f=1;

        break;

    }

    if(f==0)

            printf("nThe element is %d is not present in the listn",ele);
}



/* Recursive method*/

void l_search_recursive(int l[],int num,int ele)

{

    int f = 0;



    if( l[num] == ele)

    {

        printf("nThe element %d is present at position %d in listn",ele,num);

        f=1;

    }

    else

    {

    if((num==0) && (f==0))

    {

        printf("The element %d is not found.",ele);

    }

    else

    {

        l_search(l,num-1,ele);

    }

    }

    getch();
}



void read_list(int l[],int num)

{

    int j;

    printf("nEnter the elements:n");

    for(j=0;j<num;j++)

       scanf("%d",&l[j]);

}



void print_list(int l[],int num)

{

    int j;

    for(j=0;j<num;j++)

       printf("%dt",l[j]);

}

Contenu connexe

Tendances

C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Dr. Loganathan R
 
Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2Dr. Loganathan R
 
Implement a queue using two stacks.
Implement a queue using two stacks.Implement a queue using two stacks.
Implement a queue using two stacks.Dr. Loganathan R
 
Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Dr. Loganathan R
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked ListSayantan Sur
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4Rumman Ansari
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by DivyaDivya Kumari
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by DivyaDivya Kumari
 
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftSwift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftTomohiro Kumagai
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Dr. Loganathan R
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nmmohamed sikander
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5Rumman Ansari
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ Eli Diaz
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ Eli Diaz
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shortingargusacademy
 

Tendances (20)

C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3
 
Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2
 
Implement a queue using two stacks.
Implement a queue using two stacks.Implement a queue using two stacks.
Implement a queue using two stacks.
 
Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Array menu
Array menuArray menu
Array menu
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by Divya
 
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswiftSwift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
Operators php
Operators phpOperators php
Operators php
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 

En vedette (10)

Android beginner getting started with android
Android beginner getting started with androidAndroid beginner getting started with android
Android beginner getting started with android
 
week-1x
week-1xweek-1x
week-1x
 
week-18x
week-18xweek-18x
week-18x
 
week-10x
week-10xweek-10x
week-10x
 
week-11x
week-11xweek-11x
week-11x
 
Part4 graph algorithms
Part4 graph algorithmsPart4 graph algorithms
Part4 graph algorithms
 
BrainFingerprintingpresentation
BrainFingerprintingpresentationBrainFingerprintingpresentation
BrainFingerprintingpresentation
 
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENTDISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
 
ch6
ch6ch6
ch6
 
PPT (2)
PPT (2)PPT (2)
PPT (2)
 

Similaire à week-19x

Similaire à week-19x (20)

week-20x
week-20xweek-20x
week-20x
 
week-14x
week-14xweek-14x
week-14x
 
a) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdfa) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdf
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Tugas1
Tugas1Tugas1
Tugas1
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
C programs
C programsC programs
C programs
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
week-16x
week-16xweek-16x
week-16x
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Array list
Array listArray list
Array list
 
Pnno
PnnoPnno
Pnno
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 

Plus de KITE www.kitecolleges.com (20)

ch14
ch14ch14
ch14
 
ch16
ch16ch16
ch16
 
holographic versatile disc
holographic versatile discholographic versatile disc
holographic versatile disc
 
week-22x
week-22xweek-22x
week-22x
 
week-5x
week-5xweek-5x
week-5x
 
week-6x
week-6xweek-6x
week-6x
 
week-3x
week-3xweek-3x
week-3x
 
ch8
ch8ch8
ch8
 
Intro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.ukIntro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.uk
 
ch17
ch17ch17
ch17
 
ch4
ch4ch4
ch4
 
week-7x
week-7xweek-7x
week-7x
 
week-9x
week-9xweek-9x
week-9x
 
week-4x
week-4xweek-4x
week-4x
 
AIRBORNE
AIRBORNEAIRBORNE
AIRBORNE
 
ch2
ch2ch2
ch2
 
week-23x
week-23xweek-23x
week-23x
 
week-2x
week-2xweek-2x
week-2x
 
ch3
ch3ch3
ch3
 
Entity Classes and Attributes
Entity Classes and AttributesEntity Classes and Attributes
Entity Classes and Attributes
 

Dernier

ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.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
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 

Dernier (20)

ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
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
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.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
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 

week-19x

  • 1. /* Write C programs that use both recursive and non recursive functions to perform the following searching operations for a Key value in a given list of integers : ii) Binary search*/ #include <stdio.h> #define MAX_LEN 10 /* Non-Recursive function*/ void b_search_nonrecursive(int l[],int num,int ele) { int l1,i,j, flag = 0; l1 = 0; i = num-1; while(l1 <= i) { j = (l1+i)/2; if( l[j] == ele) { printf("nThe element %d is present at position %d in listn",ele,j); flag =1; break; } else if(l[j] < ele)
  • 2. l1 = j+1; else i = j-1; } if( flag == 0) printf("nThe element %d is not present in the listn",ele); } /* Recursive function*/ int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a) { int m,pos; if (arrayStart<=arrayEnd) { m=(arrayStart+arrayEnd)/2; if (l[m]==a) return m; else if (a<l[m]) return b_search_recursive(l,arrayStart,m-1,a); else return b_search_recursive(l,m+1,arrayEnd,a); } return -1; }
  • 3. void read_list(int l[],int n) { int i; printf("nEnter the elements:n"); for(i=0;i<n;i++) scanf("%d",&l[i]); } void print_list(int l[],int n) { int i; for(i=0;i<n;i++) printf("%dt",l[i]); } /*main function*/ void main() { int l[MAX_LEN], num, ele,f,l1,a; int ch,pos; clrscr();
  • 4. printf("======================================================"); printf("ntttMENU"); printf("n====================================================="); printf("n[1] Binary Search using Recursion method"); printf("n[2] Binary Search using Non-Recursion method"); printf("nnEnter your Choice:"); scanf("%d",&ch); if(ch<=2 & ch>0) { printf("nEnter the number of elements : "); scanf("%d",&num); read_list(l,num); printf("nElements present in the list are:nn"); print_list(l,num); printf("nnEnter the element you want to search:nn"); scanf("%d",&ele); switch(ch) { case 1:printf("nRecursive method:n"); pos=b_search_recursive(l,0,num,ele); if(pos==-1)
  • 5. { printf("Element is not found"); } else { printf("Element is found at %d position",pos); } getch(); break; case 2:printf("nNon-Recursive method:n"); b_search_nonrecursive(l,num,ele); getch(); break; } } getch(); }
  • 6. /* Write C programs that use both recursive and non recursive functions to perform the following searching operation for a Key value in a given list of integers : i) Linear search */ #include <stdio.h> #define MAX_LEN 10 void l_search_recursive(int l[],int num,int ele); void l_search(int l[],int num,int ele); void read_list(int l[],int num); void print_list(int l[],int num); void main() { int l[MAX_LEN], num, ele; int ch; clrscr();
  • 7. printf("======================================================"); printf("ntttMENU"); printf("n====================================================="); printf("n[1] Linary Search using Recursion method"); printf("n[2] Linary Search using Non-Recursion method"); printf("nnEnter your Choice:"); scanf("%d",&ch); if(ch<=2 & ch>0) { printf("Enter the number of elements :"); scanf("%d",&num); read_list(l,num); printf("nElements present in the list are:nn"); print_list(l,num); printf("nnElement you want to search:nn"); scanf("%d",&ele); switch(ch) { case 1:printf("n**Recursion method**n"); l_search_recursive(l,num,ele); getch(); break;
  • 8. case 2:printf("n**Non-Recursion method**n"); l_search_nonrecursive(l,num,ele); getch(); break; } } getch(); } /*end main*/ /* Non-Recursive method*/ void l_search_nonrecursive(int l[],int num,int ele) { int j, f=0; for(j=0;j<num;j++) if( l[j] == ele) { printf("nThe element %d is present at position %d in listn",ele,j); f=1; break; } if(f==0) printf("nThe element is %d is not present in the listn",ele);
  • 9. } /* Recursive method*/ void l_search_recursive(int l[],int num,int ele) { int f = 0; if( l[num] == ele) { printf("nThe element %d is present at position %d in listn",ele,num); f=1; } else { if((num==0) && (f==0)) { printf("The element %d is not found.",ele); } else { l_search(l,num-1,ele); } } getch();
  • 10. } void read_list(int l[],int num) { int j; printf("nEnter the elements:n"); for(j=0;j<num;j++) scanf("%d",&l[j]); } void print_list(int l[],int num) { int j; for(j=0;j<num;j++) printf("%dt",l[j]); }