C Program & Algorithm to sort an array using function.




#include<stdio.h>
#include<conio.h>
int array[20];
void sort(int x)
{
    int temp,i,j;
    for(i=0;i<x;i++)
      for(j=0;j<x-(i+1);j++)
      {
        if(array[j]>array[j+1])
        {
          temp=array[j];
          array[j]=array[j+1];
          array[j+1]=temp;
        }
      }

}
void main()
{
    int limit,i;
    clrscr();
    printf("\nEnter the limit of array : ");
    scanf("%d",&limit);
    printf("\nEnter the array elements : ");
    for(i=0;i<limit;i++)
      scanf("%d",&array[i]);
    sort(limit);
    printf("\nSorted elements are\t: ");
    for(i=0;i<limit;i++)
      printf(" %d",array[i]);
    getch();
}


Algorithm

sort(x)
step1:start
step2:intilize i<-0
step3:check whether i<x, then go to step 4
    else go to step14
step4:intilize j<-0
step5:check whether j<x-i+1,then go to step6
    else go to step12
step6:if array[j]>array[j+1], then go to step7
    else go to step10
step7:temp=array[j]
step8:array[j]=array[j+1]
step9:array[j+1]=temp
step10:j=j+1
step11:go to step5
step12:i=i+1
step13:go to step3
step14:stop
main()
step1:start
step2:read limit
step3:intilize i<-0
step4:check whether i<limit ,then go to step 5
    else go to step8
step5:read arrar[i]
step6:i=i+1
step7:go to step 4
step8:call sort (limit)
step9:intilize i<-0
step10:check whether i<limit,then go to step11
    else go to step13
step11:print array[i]
step12:i=i+1
step13:stop