This blog consists of C and C++ programs. C and C++ programming is the basics to learn any programming language. Most of the programs are provided with their respective outputs. This blog also contains Data Structures programs using Object-Oriented Programming (C++). Some of the best books for learning C and C++ programming are also mentioned.

Friday 11 October 2013

C program using Dynamic Memory Allocation to Sort 'n' integers in Ascending Order

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *a,n,i,j,t;
clrscr();
printf("\nEnter the number of integers to be sorted in ascending order: ");
scanf("%d",&n);
a=(int *)malloc(n *sizeof(int));
printf("\nEnter the %d numbers to be sorted in ascending order one by one: ",n);
for(i=0;i<=n-1;i++)
{
scanf("%d", (a+i));
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(*(a+i)<*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
printf("\nAfter sorting in ascending order: ");
for(i=0;i<n;i++)
printf("\n%d",*(a+i));
getch();
}


No comments:

Post a Comment