I was wondering, how a program can be written in a perfect professional style. Many times it happens that we write a very good program/code which gives the accurate output.One might use the best algorithm to solve the given problem.
But for a person who is reading your code for his/her reference, it becomes difficult to understand the code because of improper use of variable/function names.(And many other issues)
So how one can achieve this perfection of writing code in professional manner?
You can elaborate your thoughts by directly editing the following code. (Don't edit the code in the question :)
You can also rewrite the complete code at the end in professional way.Will be a lot of help.
(A simple heap sort algorithm in C)
#include<stdio.h>
#include<conio.h>
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,item=a[i];
while((i>0) && (item>a[j]))
{
a[i]=a[j];
i=j;
j=(i-1)/2;
}
a[i]=item;
}