SlideShare une entreprise Scribd logo
1  sur  4
LINEAR SEARCH



#include<stdio.h>

main()
{
  int array[100], search, c, number;

    printf("Enter the number of elements in arrayn");
    scanf("%d",&number);

    printf("Enter %d numbersn", number);

    for ( c = 0 ; c < number ; c++ )
      scanf("%d",&array[c]);

    printf("Enter the number to searchn");
    scanf("%d",&search);

    for ( c = 0 ; c < number ; c++ )
    {
       if ( array[c] == search ) /* if required element found */
       {
          printf("%d is present at location %d.n", search, c+1);
                 break;
       }
    }
    if ( c == number )
       printf("%d is not present in array.n", search);

    return 0;
}
BINARY SEARCH

#include<stdio.h>
int main(){

    int a[10],i,n,m,c=0,l,u,mid;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements in ascending order: ");
    for(i=0;i<n;i++){
       scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);

    l=0,u=n-1;
    while(l<=u){
        mid=(l+u)/2;
        if(m==a[mid]){
           c=1;
           break;
        }
        else if(m<a[mid]){
           u=mid-1;
        }
        else
           l=mid+1;
    }
    if(c==0)
        printf("The number is not found.");
    else
        printf("The number is found.");

    return 0;
}

Sample output:
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.
INSERTION SORT

#include <stdio.h>
int main()
{
  int n, array[1000], c, d, t;
  printf("Enter number of elementsn");
  scanf("%d", &n);
  printf("Enter %d integersn", n);
  for (c = 0; c < n; c++) {
    scanf("%d", &array[c]);
  }
  for (c = 1 ; c <= n - 1; c++) {
    d = c;
     while ( d > 0 && array[d] < array[d-1]) {
      t      = array[d];
      array[d] = array[d-1];
      array[d-1] = t;
      d--;
    }
  }
  printf("Sorted list in ascending order:n");
   for (c = 0; c <= n - 1; c++) {
    printf("%dn", array[c]);
  }
   return 0;
}




OUTPUT OF PROGRAM:
BUBBLE SORT

Contenu connexe

Tendances

Implementation of strassens
Implementation of  strassensImplementation of  strassens
Implementation of strassens
Vikash Dhal
 
(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
 
(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
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
vinay arora
 
C basics
C basicsC basics
C basics
MSc CST
 

Tendances (20)

One dimensional operation of Array in C- language
One dimensional operation of Array in C- language One dimensional operation of Array in C- language
One dimensional operation of Array in C- language
 
programs
programsprograms
programs
 
Program in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersProgram in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointers
 
C programming BY Mazedur
C programming BY MazedurC programming BY Mazedur
C programming BY Mazedur
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
Computer programing w
Computer programing wComputer programing w
Computer programing w
 
Implementation of strassens
Implementation of  strassensImplementation of  strassens
Implementation of strassens
 
C file
C fileC file
C file
 
(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++
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++
 
Document
DocumentDocument
Document
 
C & Python Introduction
C & Python IntroductionC & Python Introduction
C & Python Introduction
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical order
 
Progr2
Progr2Progr2
Progr2
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
C basics
C basicsC basics
C basics
 
C program to add n numbers
C program to add n numbers C program to add n numbers
C program to add n numbers
 
C program to implement linked list using array abstract data type
C program to implement linked list using array abstract data typeC program to implement linked list using array abstract data type
C program to implement linked list using array abstract data type
 
1346
13461346
1346
 

Similaire à Ds

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 

Similaire à Ds (20)

ADA FILE
ADA FILEADA FILE
ADA FILE
 
Ada file
Ada fileAda file
Ada file
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
1D Array
1D Array1D Array
1D Array
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
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
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Arrays
ArraysArrays
Arrays
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
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
 
Array Programs.pdf
Array Programs.pdfArray Programs.pdf
Array Programs.pdf
 
C programms
C programmsC programms
C programms
 

Ds

  • 1. LINEAR SEARCH #include<stdio.h> main() { int array[100], search, c, number; printf("Enter the number of elements in arrayn"); scanf("%d",&number); printf("Enter %d numbersn", number); for ( c = 0 ; c < number ; c++ ) scanf("%d",&array[c]); printf("Enter the number to searchn"); scanf("%d",&search); for ( c = 0 ; c < number ; c++ ) { if ( array[c] == search ) /* if required element found */ { printf("%d is present at location %d.n", search, c+1); break; } } if ( c == number ) printf("%d is not present in array.n", search); return 0; }
  • 2. BINARY SEARCH #include<stdio.h> int main(){ int a[10],i,n,m,c=0,l,u,mid; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements in ascending order: "); for(i=0;i<n;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); l=0,u=n-1; while(l<=u){ mid=(l+u)/2; if(m==a[mid]){ c=1; break; } else if(m<a[mid]){ u=mid-1; } else l=mid+1; } if(c==0) printf("The number is not found."); else printf("The number is found."); return 0; } Sample output: Enter the size of an array: 5 Enter the elements in ascending order: 4 7 8 11 21 Enter the number to be search: 11 The number is found.
  • 3. INSERTION SORT #include <stdio.h> int main() { int n, array[1000], c, d, t; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); } for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } printf("Sorted list in ascending order:n"); for (c = 0; c <= n - 1; c++) { printf("%dn", array[c]); } return 0; } OUTPUT OF PROGRAM: