Program to enter n set of elements inside 1-D array and arrange these elements using bubble sort & selection sort.

Selection Sort:

#include<stdio.h>
main()
{
    int i,j,n,a[50],temp;
    printf("Enter the size of the array \n");
    scanf("%d",&n);
    printf("Enter elements\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",(a+i));
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        {
            if(*(a+i)< *(a+j))
            {
                temp = *(a+i);
                *(a+i)=*(a+j);
                *(a+j)=temp;
            }
        }
    }
    printf("\nThe sorted array is ");
    for(i=0;i<n;i++)
    printf("%d",*(a+i));
}

 

Bubble Sort:

#include<stdio.h>
main()
{
    int a[100],i,j,n,temp;
    printf("Enter size of array");
    scanf("%d",&n);
    printf("Enter elements\n");
    for(i=0;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\n");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=(n-i);j++)
        {
            if(*(a+j)>*(a+j+1))
            {
                temp=*(a+j);
                *(a+j)=*(a+j+1);
                *(a+j+1)=temp;

            }
        }
    }

    printf("The sorted array is \n");
    for(i=1;i<=n;i++)
    {
        printf("%d\n",*(a+i));
    }
}

 

Output [Selection Sort]:

selection

Output [Bubble Sort]:

bubble

Program to calculate no. of alphabets, digits & white spaces using user defined functions along with pointers.

#include<stdio.h>
main()
{
    char str[100];
    int i,j,a=0,b=0,c=0;
    char *p,*q,*r;
    printf("Enter a string\n");
    gets(str);
    j=strlen(str);
    p=&str;
    for(i=0;i<=j-1;i++)
    {
        if(isalpha(*(p+i)))
        {
            a++;
        }
        if(isdigit(*(p+i)))
        {
            b++;
        }
       if(isspace(*(p+i)))
       {
           c++;
       }
    }
    printf("Alphabets - %d, Digits - %d & Spaces - %d",a,b,c);
}

 

OUTPUT [Click to enlarge]:

white space