Here are few changes style wise, so may differ from person to person but be consistent.
- Add a comment at top of the file
- Add space after comma e.g. swap(int *,int *); -> swap(int *, int *);
- Add space after semicolon in for loop
- Add space after and before operators e.g '=', '+' , '-'
- Remove unecessary space after braces and main
- Be explicit so add braces {} in each for loop
- Group code lines in group and separate groups by newlines
- Start while loop brace from same column
Here is modified code
#include<stdio.h>
#include<conio.h>
/********************************************
Write a comment about what this program does.
General overview
*********************************************/
void insert(int i);
void swap(int *, int *);
void heap_sort(int);
int a[20];
void main()
{
int n, i;
clrscr();
printf("Enter the no of elements : ");
scanf("%d", &n);
printf("\nEnter the elements : \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
heap_sort(n);
printf("\nSorted array is : \n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
getch();
}
void swap(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
void heap_sort(int n)
{
int x=1, i;
while(x<n)
{
for(i=1; i<=n-x; i++)
{
insert(i);
}
swap(&a[0], &a[n-x]);
x++;
}
}
void insert(int i)
{
int j = (i - 1) / 2;
int item = a[i];
while((i>0) && (item>a[j]))
{
a[i] = a[j];
i = j;
j = (i - 1) / 2;
}
a[i] = item;
}