SlideShare una empresa de Scribd logo
1 de 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]);

}

Más contenido relacionado

La actualidad más candente

(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
 

La actualidad más candente (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
 

Destacado (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)
 

Similar a week-19x

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
MAYANKBANSAL1981
 
Sorting programs
Sorting programsSorting programs
Sorting programs
Varun Garg
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
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
anujmkt
 

Similar a 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
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
 
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
 

Más 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
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

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]); }