c lab programs format 1

Upload: vasudeva-murthy-s

Post on 14-Apr-2018

240 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 C Lab Programs Format 1

    1/19

    1:- Binary search

    #include

    #include

    void main ()

    {

    int a [20], first, last, middle, n, i, item;

    clrscr ();

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

    scanf ( "%d", &n );

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

    {

    printf ( "Enter element %d:", i+1);

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

    }

    printf ("Enter the element to be searched\n");

    scanf ( "%d", &item );

    first = 0;

    last = n-1;

    middle = ( first + last ) / 2;

    while ( item != a[middle] && first a[middle] )

    first = middle + 1;

    else

    last = middle - 1;

    middle = ( first + last ) / 2;

    }

  • 7/27/2019 C Lab Programs Format 1

    2/19

    if ( item == a[middle] )

    printf ( "\n %d found at position %d \n ", item, middle + 1 );

    if ( first > last )

    printf ( "%d not found in array \n", item );

    getch ( );

    }

    Output of Binary Search :-

  • 7/27/2019 C Lab Programs Format 1

    3/19

    2:- Bubble Sort

    #include

    #include

    void bubble_sort ( int a[ ], int n );

    void main()

    {

    int i, j, a[ 20 ], n, temp;

    clrscr ( );

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

    scanf ( "%d", &n );

    printf( " Enter the array elements\n " );

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

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

    bubble_sort ( a, n );

    printf ( "\n The sorted elements are\n " );

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

    printf ( " %d\n ", a[ i ] );

    getch( );

    }

    void bubble_sort ( int a[ ], int n )

    {

    intpass, temp, j;

    for ( pass = 1 ; pass < n; pass++ )

    {for ( j = 0; j a[ j+1] )

    {

    temp = a [ j ];

    a[ j ] = a[ j+1 ];

    a[ j+1] = temp;

    }

  • 7/27/2019 C Lab Programs Format 1

    4/19

    }

    }

    }

    Output of Bubble sort program :-

  • 7/27/2019 C Lab Programs Format 1

    5/19

    3:- Heap Sort

    #include

    #include

    void heapfy ( int a[ ], int n );

    int adjust ( int a[ ], int n );

    int heaps ( int a[ ], int n);

    void heapfy ( int a[ ], int n )

    {

    int x, y, z, it;

    for ( z = 2; z a[y] )

    {

    a[ x ] = a[ y ];

    x = y;

    y = x/2;

    }

    a[ x ] = it;

    }

    }

  • 7/27/2019 C Lab Programs Format 1

    6/19

    adjust ( int a[ ], int n )

    {

    int it, x, y;

    y =1;

    it = a[ y ];

    x = 2 * y;

    while ( x < n )

    {

    if ( x+1 < n )

    {

    if ( a[ x ] < a[ x+1 ] )

    x++;

    }

    if ( it < a[ x ] ){

    a[ y] = a[ x ];

    y = x;

    x = 2 * y;

    }

    else

    break;

    }

    a[ y ] = it;

    }

  • 7/27/2019 C Lab Programs Format 1

    7/19

    heaps ( int a[ ], int n )

    {

    int i, temp;

    heapfy ( a, n);

    for ( i = n; i >= 1; i-- )

    {

    temp = a[ 1 ];

    a[ 1 ] = a[ i ];

    a[ i ] = temp;

    adjust ( a, i );

    }

    }

    void main(){

    int i, n, a[ 100 ];

    clrscr ( );

    printf ( "enter the number of elements" );

    scanf ( "%d", &n );

    printf ( "enter the array elements" );

    for ( i =1; i

  • 7/27/2019 C Lab Programs Format 1

    8/19

    heaps ( a, n );

    printf ( "the sorted elements are\n" );

    for ( i =1; i

  • 7/27/2019 C Lab Programs Format 1

    9/19

    4:- Quick Sort

    #include

    #include

    void main()

    {

    int i, n, a[ 20 ];

    float f;

    clrscr( );

    printf ( " enter the size of array " );

    scanf ( "%d", &n );

    printf ( "\n enter the array elements\n" );

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

    quicksort ( a, 0, n-1);

    printf ( "the sorted elements are " );

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

    printf ( "\n%d", a[ i ] );

    getch( );

    }

  • 7/27/2019 C Lab Programs Format 1

    10/19

    int quicksort ( int a[ ], int low, int high )

    {

    int j;

    if ( low < high )

    {

    j = partition ( a, low, high );

    quicksort ( a, low, j-1 );

    quicksort ( a, j +1, high );

    }

    }

    int partition ( int a[ ], int low, int high )

    {

    int i, j, temp, key;key = a[ low ];

    i = low + 1;

    j = high;

    while ( 1 )

    {

    while ( i < high && key > a[ i ] )

    i++;

    while ( key

  • 7/27/2019 C Lab Programs Format 1

    11/19

    if ( i < j )

    {

    temp = a[ i ];

    a[ i ] = a[ j ];

    a[ j ] = temp;

    }

    else

    {

    temp = a[ low ];

    a[ low ] = a[ j ];

    a[ j ] = temp;

    return j;

    }

    }

    }Output of Quick Sort program :-

  • 7/27/2019 C Lab Programs Format 1

    12/19

    4:- Merge sort

    #include

    #include

    void main( )

    {

    int i, a[ 100 ], n;

    clrscr( );

    printf ( "enter the size of array" );

    scanf ( "%d", &n );

    printf ( "enter the array elements\n" );

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

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

    merge_sort ( a, 0, n-1);

    printf ( "\nthe sorted elements are\n" );

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

    printf ( "%d\n", a[ i ] );

    getch( );

    }

  • 7/27/2019 C Lab Programs Format 1

    13/19

    int merge_sort ( int a[ ], int low, int high )

    {

    int mid;

    if ( low < high )

    {

    mid = ( low + high ) / 2;

    merge_sort ( a, low, mid );

    merge_sort ( a, mid +1, high );

    merge ( a, low, mid, high );

    }

    }

    int merge ( int a[ ], int low, int mid, int high )

    {

    int i, j, k, res[ 30 ];i = low;

    j = mid + 1;

    k = low;

    while ( ( i

  • 7/27/2019 C Lab Programs Format 1

    14/19

    else

    {

    res[ k ] = a[ j ];

    k = k + 1;

    j = j + 1;

    }

    }

    while ( i

  • 7/27/2019 C Lab Programs Format 1

    15/19

    Output of Merge Sort program :-

  • 7/27/2019 C Lab Programs Format 1

    16/19

    5:- Stack Operation

    #include

    #include

    #define maxstk 5

    int top = -1;

    int s [ maxstk ];

    void main( )

    {

    int ch;

    clrscr( );

    while ( 1 )

    {

    printf ( "\n 1: push \n 2: pop \n 3:display \n 4: exit \n Enter

    your choice\n" );

    scanf ( "%d", &ch );

    switch ( ch )

    {

    case 1: push();

    break;

    case 2: pop();

    break;

    case 3: display();

    break;

    case 4: exit(1);

    default : printf("Wrong choice\n");

    }

    }

    }

  • 7/27/2019 C Lab Programs Format 1

    17/19

    push ( )

    {

    int item;

    if ( top == ( maxstk - 1) )

    printf ( "stack overflow \n" );

    else

    {printf ( "Enter the item to be pushed in stack\n" );

    scanf ( "%d", &item );

    top++;

    s[ top ] = item;

    }

    }

    pop ( ){

    if ( top == -1)

    printf ( "stack underflow\n" );

    else

    {

    printf ( "popped element is %d", s[ top ] );

    top--;

    }}

    display( )

    {

    int i;

    if ( top == -1)

    printf ( "stack is empty \n" );

    else{

    printf ( "stack elements are :\n" );

    for ( i = top; i >= 0; i--)

    printf ( "%d", s[ i ] );

    }

    }

  • 7/27/2019 C Lab Programs Format 1

    18/19

    Output of Stack program :-

  • 7/27/2019 C Lab Programs Format 1

    19/19