PPS Practical 58

PPS Practical 58
  • Aim: Write a program for sorting using pointer.
  • Code:
    //For turbo C++ remove below comments
    #include <stdio.h>
    //#include<conio.h>
    void main()
    {
        // clrscr();
        int a[99],n,i,j,*p;
        printf("Enter a length of array: ");
        scanf("%d",&n);
        printf("Enter array:\n");
        for ( i = 0; i < n; i++){
            scanf("%d",&a[i]);
        }
        p=&a[0];
        for ( i = 0; i < n-1; i++){
            for ( j = i+1; j < n; j++){
                if (*(p+i)>*(p+j)){
                    *(p+i)=*(p+i)+*(p+j);
                    *(p+j)=*(p+i)-*(p+j);
                    *(p+i)=*(p+i)-*(p+j);
                }
            }
        }
        printf("Sorted Array : \n");
        for ( i = 0; i < n; i++){
            printf(" %d",*(p+i));
        }
        // getch();
    }
  • Output:
    Output of practical 58

Comments