daa lab programms

Upload: lokesh045

Post on 04-Jun-2018

258 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 DAA Lab Programms

    1/25

    1

    INDEX

    1. Program for Linear search in C.

    2. Program for Binary search in C.

    3. Program for Insertion Sort in C.

    4. Program for Selection Sort in C.

    5. Program for Quick Sort in C.

    6. Program for Merge Sort in C.

    7. Program to Implement knapsack

    problem in C.

    8. Program to Implement Kruskals

    Algorithm in C.

    9. Program to Implement Floyd

    Warshalls algorithm in C.10. Program to Implement Dijakstras

    algorithm in C.

    S.NO. CONTENTS DATE SIGN.

  • 8/13/2019 DAA Lab Programms

    2/25

    2

    1.Write a Program for Linear search in C.

    #include

    int main()

    {int array[100], search, c, n;

    printf("Enter the number of elements in array\n");

    scanf("%d",&n);

    printf("Enter %d integer(s)\n", n);

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

    printf("Enter the number to search\n");

    scanf("%d", &search);

    for (c = 0; c < n; c++){

    if (array[c] == search) /* if required element found */{

    printf("%d is present at location %d.\n", search, c+1);break;

    }}

    if (c == n)printf("%d is not present in array.\n", search);

    return 0;

    }

  • 8/13/2019 DAA Lab Programms

    3/25

    3

    OUTPUT

    Enter the number of elements in array

    5

    Enter 5 numbers

    5

    6

    4

    2

    9

    Enter the number to search

    4

    4 is present at location 3.

  • 8/13/2019 DAA Lab Programms

    4/25

    4

    2.Write a Program for Binary search in C.

    #include

    int main()

    {int c, first, last, middle, n, search, array[100];

    printf("Enter number of elements\n");scanf("%d",&n);

    printf("Enter %d integers\n", n);

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

    scanf("%d",&array[c]);

    printf("Enter value to find\n");

    scanf("%d",&search);

    first = 0;

    last = n - 1;

    middle = (first+last)/2;

    while( first last )

    printf("Not found! %d is not present in the list.\n", search);

  • 8/13/2019 DAA Lab Programms

    5/25

    5

    return 0;

    }

    Output

    Enter number of elements

    7

    Enter 7 integers

    -4

    5

    8

    9

    11

    43

    485

    Enter value to find

    11

    11 found at location 5.

  • 8/13/2019 DAA Lab Programms

    6/25

    6

    3.Write a Program for Insertion Sort in C.

    #include

    int main(){

    int n, array[1000], c, d, t;

    printf("Enter number of elements\n");scanf("%d", &n);

    printf("Enter %d integers\n", n);

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

    }

    for (c = 1 ; c 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

  • 8/13/2019 DAA Lab Programms

    7/25

    7

    OUTPUT

    Enter number of elements

    5

    Enter 5 integers

    5

    45

    96

    85

    9

    Sorted list in ascending order :

    5

    9

    45

    85

    96

  • 8/13/2019 DAA Lab Programms

    8/25

    8

    4.Write a Program for Selection Sort in C.

    #include

    int main()

    {int array[100], n, c, d, position, swap;

    printf("Enter number of elements\n");scanf("%d", &n);

    printf("Enter %d integers\n", n);

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

    scanf("%d", &array[c]);

    for ( c = 0 ; c < ( n - 1 ) ; c++ )

    {position = c;

    for ( d = c + 1 ; d < n ; d++ )

    {if ( array[position] > array[d] )

    position = d;}

    if ( position != c )

    {swap = array[c];

    array[c] = array[position];array[position] = swap;

    }}

    printf("Sorted list in ascending order:\n");

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

    printf("%d\n", array[c]);

    return 0;

    }

  • 8/13/2019 DAA Lab Programms

    9/25

    9

    OUTPUT

    Enter number of elements

    5

    Enter 5 integers

    15

    456

    96

    18

    9

    Sorted list in ascending order :

    9

    15

    18

    96

    456

  • 8/13/2019 DAA Lab Programms

    10/25

    10

    5.Write a Program for Quick Sort in C.

    #include

    void quicksort(int [10],int,int);

    int main(){

    int x[20],size,i;

    printf("Enter size of the array: ");

    scanf("%d",&size);

    printf("Enter %d elements: ",size);

    for(i=0;i

  • 8/13/2019 DAA Lab Programms

    11/25

    11

    x[i]=x[j];

    x[j]=temp;}

    }

    temp=x[pivot];

    x[pivot]=x[j];x[j]=temp;

    quicksort(x,first,j-1);quicksort(x,j+1,last);

    }}

    OUTPUT

    Enter size of the array :

    5

    Enter 5 elements :

    5

    23

    2

    96

    44

    Sorted list is :

    2 5 23 44 96

  • 8/13/2019 DAA Lab Programms

    12/25

    12

    6.Write a Program for Merge Sort in C.

    #include

    #define MAX 50

    void mergeSort(int arr[],int low,int mid,int high);void partition(int arr[],int low,int high);

    int main(){

    int merge[MAX],i,n;

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

    scanf("%d",&n);

    printf("Enter the elements which to be sort: ");for(i=0;i

  • 8/13/2019 DAA Lab Programms

    13/25

    13

    }

    void mergeSort(int arr[],int low,int mid,int high){

    int i,m,k,l,temp[MAX];

    l=low;i=low;

    m=mid+1;

    while((l

  • 8/13/2019 DAA Lab Programms

    14/25

    14

    OUTPUT

    Enter the total no. of elements:

    4Enter the element which to be sort:

    4

    5

    6

    1

    After merge sorting, elements are:

    1 4 5 6

  • 8/13/2019 DAA Lab Programms

    15/25

    15

    7.Write a Program to Implement knapsack problem in C.

    # include

    # include

    void knapsack(int n, float weight[], float profit[], float capacity){

    float x[20], tp= 0;int i, j, u;

    u=capacity;for (i=0;i

  • 8/13/2019 DAA Lab Programms

    16/25

    16

    for (i=0; i

  • 8/13/2019 DAA Lab Programms

    17/25

    17

    OUTPUT

    Enter the number of objects :-

    Enter the weights and profits of each object :-

    Enter the capacity of knapsack :-

    The result vector is :-

    Maximum profit is :-

    Enter the no. of objects:-

    3Enter the weight and profit of each object:-

    5 6

    4 5

    3 4

    Enter the capacity of knapsack:-

    7

    The result vector is:3 2

    Maximum profit is:-9

  • 8/13/2019 DAA Lab Programms

    18/25

    18

    8.Write a Program to Implement Kruskals Algorithm in C.

    #include

    #include

    #includeint i,j,k,a,b,u,v,n,ne=1;

    int min,mincost=0,cost[9][9],parent[9];int find(int);

    int uni(int,int);void main()

    {

    clrscr();printf("\n\n\tImplementation of Kruskal's algorithm\n\n");

    printf("\nEnter the no. of vertices\n");

    scanf("%d",&n);printf("\nEnter the cost adjacency matrix\n");for(i=1;i

  • 8/13/2019 DAA Lab Programms

    19/25

    19

    u=find(u);

    v=find(v);if(uni(u,v))

    {

    printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min);mincost +=min;

    }cost[a][b]=cost[b][a]=999;

    }printf("\n\tMinimum cost = %d\n",mincost);

    getch();

    }int find(int i)

    {

    while(parent[i])i=parent[i];return i;

    }

    int uni(int i,int j){

    if(i!=j)

    {parent[j]=i;

    return 1;

    }return 0;

    }

  • 8/13/2019 DAA Lab Programms

    20/25

    20

    OUTPUT

    Implementaion of Kruskals' algorithm

    Enter the no. of vertices:4

    Enter the cost adjacency matrix:

    4

    5

    8

    7

    21

    6

    7

    9

    2

    5

    6

    2

    7

    8

    6

    The edges of minimum cost spanning Tree are

    1 edge (2,1) =2

    2 edge (3,2)=2

    3 edge (4,1)=2

    Minimum Cost =6

  • 8/13/2019 DAA Lab Programms

    21/25

    21

    9.Write a Program to Implement Floyd Warshalls algorithm in C.

    #include

    #include

    int max(int,int);void warshal(int p[10][10],int n)

    {

    int i,j,k;

    for(k=1;k

  • 8/13/2019 DAA Lab Programms

    22/25

    22

    for(i=1;i

  • 8/13/2019 DAA Lab Programms

    23/25

    23

    OUTPUT

    WARSHALS ALGORITHM

    Enter the number of verices:3

    Enter the number of edges:3Enter the end vertices of edges1:

    1

    2

    Enter the end vertices of edges2:

    2

    3Enter the end vertices of edges3:

    1

    3

    Matrix of Input Data:

    0 1 1

    0 0 1

    0 0 0

    Ttraversitive Closure:

    0 1 1

    0 0 1

    0 0 0

  • 8/13/2019 DAA Lab Programms

    24/25

    24

    10.Write a Program to Implement Dijakstras algorithm in C.

    #include

    #include

    int n;int weight[100][100];

    char intree[100];

    int d[100];

    int whoTo[100];

    void updatedistance(int target)

    {

    int i;

    for(i=0;iweight[target][i]))

    {

    d[i]=weight[target][i];

    whoTo[i]=target;

    }

    }

    void main()

    {

    FILE *f=fopen("dist.txt","r");

    int i,j,total,treesize;

    clrscr();

    fscanf(f,"%d",&n);

    for(i=0;i

    for(j=0;j

    fscanf(f,"%d",&weight[i][j]);

    fclose(f);

    for(i=0;i

    intree[i]=0;

    printf("Adding Nodes:%c\n",0+'A');

    intree[0]=1;

    updatedistance(0);

    total=1;

  • 8/13/2019 DAA Lab Programms

    25/25

    25

    for(treesize=1;treesize

    {

    int min=-1;

    for(i=0;id[i]))

    min=i;

    printf("Adding Edge%c-%c\n",whoTo[min]+'A',min+'A');

    intree[min]=1;

    total+=d[min];

    updatedistance(min);

    }

    printf("Total Distance:%d \n",total);getch();

    }

    OUTPUT:

    DIJIKSTRAS ALGORITHM

    Adding Nodes: A

    Total Distance: 1