Data Structure Lab Manual
Data Structure Lab Manual
Algorithm:
Step 1: Start
Step 2: Read number n
Step 3: Set f=0
Step 4: For i=2 to n-1
Step 5: If n mod 1=0 then
Step 6: Set f=1 and break
Step 7: Loop
Step 8: If f=0 then print 'The given number is prime'
Step 9:else print 'The given number is not prime'
Step 10: Stop
Program:
#include <stdio.h>
int main()
{
int tally; int number;
unsigned char
flag=0;
if(flag==0)
printf("\n %d is a prime number.",number);
else
printf("\n %d is not a prime number.",number);
return 0;
}
OUTPUT:
RESULT:
Algorithm:
Step 4: the condition is true then the statements in the body of the while are executed, Step 5:
again the condition is checked if it is still true then once again statements in the body of the while
are executed
Program:
#include <stdio.h>
int main()
{ int number, revNumber=0, rem=0,tempNumber;
tempNumber=number;
while(tempNumber!=0)
{ rem=tempNumber%10;
revNumber=revNumber*10+rem
; tempNumber/=10;
}
/* checking if number is equal to reverse number */
if(revNumber==number)
printf("%d is a palindrome.", number);
else
printf("%d is not a palindrome.", number);
return 0;
}
OUTPUT
12321 is a palindrome.
RESULT:
To write the program for print the numbers using do while loop
Algorithm:
Step 4:This means that the code must always be executed first and then the expression or
test condition is evaluated.
Step 5: If it is true, the code executes the body of the loop again.
Program
#include <stdio.h>
int main()
{
int j=0;
do
}
OUTPUT:
Value of variable j is : 0
Value of variable j is : 1
Value of variable j is : 2
Value of variable j is : 3
RESULT:
Aim:
To write a program for Reversing the number using Data Manipulations.
Algorithm:
Program
#include <stdio.h>
int main()
{ int n; int dig,
revNumber;
/*Reversing Number*/
revNumber=0;
while(n>0)
{ dig=n%10; /*get digit*/
revNumber=(revNumber*10)+dig;
n=n/10; }
printf("\nReverse Number is :
%d\n",revNumber); return 0;
}
OUTPUT:
Aim:
Algorithm:
Step1: Create an array of integer of some certain maximum capacity (10, in this case). Step 2:
From users, take a number N as input, which will indicate the number of elements in the array
(N <= maximum capacity).
Step 3: Iterating through for loops (from [0 to N) ), take integers as input from user and
print them. These input are the elements of the array.
Step 4: Now, start summation by iterating through all the elements and adding positive and
negative number separately to print as well as combined to calculate average. Step 5: To
calculate average, the overall sum (sum of positive + negative integers) is divided by total
number of elements in the array.
Program:
#include <stdio.h>
#define MAXSIZE 10
void main()
{ int array[MAXSIZE];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
OUTPUT
Enter the value of N
10
Enter 10 numbers (-ve, +ve and zero)
-8
9
-100
-80
90
45
-23
-1
0
16
Input array elements
-8
+9
-100
-80
+90
+45
-23
-1
+0
+16
Sum of all negative numbers = -212
Sum of all positive numbers = 160
Average of all input numbers = -5.20
RESULT:
Thus the program was written and executed successfully.
Ex.no:1.f Program to Find the Largest Two Numbers in a given Array
Date:
Aim:
Program
#include <stdio.h>
#define MAX 4
void main()
{ int array[MAX], i, largest1, largest2, temp;
printf("\n");
/* assume first element of array is the first larges t*/
largest1 = array[0];
/* assume first element of array is the second largest */
largest2 = array[1]; if
(largest1 < largest2)
{ temp = largest1;
largest1 =
largest2;
largest2 = temp;
}
}
OUTPUT
Enter 4 integer numbers
80
23
79
58
Input integer are
80 23 79 58
Average of 80 and 79 = 79
RESULT:
Ex.no:2.a Read & write Strings in C using Printf() and Scanf() functions
Date:
Aim:
To write a Program for Read & write Strings in C using Printf() and Scanf()
functions Algorithm:
Program:
#include <stdio.h>
#include <string.h>
int main()
{ char nickname[20]; printf("Enter
nickname);
printf("%s",nickname);
return 0;
}
Output:
Negan
RESULT:
Aim:
To write a program for find length of the string using string function in c -
strnlen
Algorithm:
Step 1: Start the program
Step 2: String Declaration
Step3: Reading the input string
Step 4:Strnlen function used to finr the length of the function
Step 5:print the length of the String
Step 6: End the program
Program:
#include <stdio.h>
#include <string.h>
int main()
}
OUTPUT:
RESULT:
Thus the program was written and executed successfully.
Ex.no:2.c String function in C – strcmp Date:
Aim:
To write a program to find the string using string function in C
Algorithm:
Program:
#include <stdio.h>
#include <string.h>
int main()
s2[20] = "BeginnersBook.COM"; if
different");
return 0;
}
OUTPUT:
string 1 and 2 are different
Result: Thus the program was written and executed
successfully. Ex.no:2.d String function in C – strncat
Date:
Aim:
To write a program for concatenation of two string using string
function Algorithm:
Program:
#include <stdio.h>
#include <string.h>
int main()
}
OUTPUT:
Concatenation using strncat: HelloWorld
RESULT:
Aim:
Algorithm:
Step1: Start the program
Step 2:size_t is unassigned short and n is a number.
Step 3:Case1: If length of str2 > n then it just copies first n characters of str2 into str1. Step
4:Case2: If length of str2 < n then it copies all the characters of str2 into str1 Step 5:And
appends several terminator chars(‘\0’) to accumulate the length of str1 to make it n. Step 6:
End the program
Program:
#include <stdio.h>
#include <string.h>
int main()
strcpy(s1,s2); printf("String
}
OUTPUT:
successfully.
Algorithm:
Step 1: Start the Program
Step 2: Structures can be created and accessed using pointers.
Step 3: the pointer variable of type struct name is created.
Step 4: A structure's member can be accesssed through pointer
Step 5:Referencing pointer to another address to access memory
Step 6:Then only the structure member through pointer can can
accessed. Step 7:End the program
Program:
#include <stdio.h>
#include <string.h>
struct student
{
int id; char
name[30]; float
percentage;
};
int main()
{
int i;
struct student record1 = {1, "Raju", 90.5};struct
student *ptr;
ptr = &record1;
printf("Records of STUDENT1: \n"); printf(" Id
is: %d \n", ptr->id); printf(" Name is: %s \n", ptr-
>name); printf(" Percentage is: %f \n\n", ptr-
>percentage);
return 0;
}
OUTPUT:
Records of STUDENT1:
Id is: 1
Name is: Raju
Percentage is: 90.500000
RESULT:
Thus the program was written and executed successfully.
Ex.no:3.b Date: Program To copy a
Structure in C
Aim:
Algorithm:
Step 1:Start the program
Step 2:define pointers to structures in the same way as you define pointer to any other variable
Step 3:can store the address of a structure variable in the above defined pointer variable. Step
4:To find the address of a structure variable, place the '&'; operator before the structure's name
Step 5:To access the members of a structure using a pointer to that structure, you must use the
→ operator
Step 6:End the program
Program:
#include <stdio.h>
#include <string.h>
struct student
{
int id; char
name[30]; float
percentage;
};
int main()
{
int i;
struct student record1 = {1, "Raju", 90.5};
struct student record2, *record3, *ptr1, record4;
return 0;
}
OUTPUT:
RESULT:
Aim:
To write a dynamic memory allocation program to create memory for int , char and
float variable at run time.
Algorithm:
Step 1:Start the program
Step 2: Initializing and allocating memory dynamically
Step 3:malloc () function is used to allocate space in memory during the execution of
the program.
Step 4:malloc () does not initialize the memory allocated during execution. Step
5:It carries garbage value. malloc () function returns null pointer if it couldn't able
toallocate requested amount of memory. Step 6: End the program
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{ int *iVar;
char *cVar;
float *fVar;
iVar=(int*)malloc(1*sizeof(int));
cVar=(char*)malloc(1*sizeof(char));
fVar=(float*)malloc(1*sizeof(float));
return 0;
}
OUTPUT:
Enter integer value: 100
Aim:
To write a program for implementation of stack using array in C
Algorithm:
Step 1:Start the program
Step 2:Stack operations may involve initializing the stack,
Step 3:We initialize top at -1, as the index in array starts from 0. So we check if the top is
below zero or -1 to determine if the stack is empty.
Step 4:The process of putting a new data element onto stack, Push operation involves Checks if
the stack is full ,produces an error and exit.
Step 5: If the stack is not full, increments top to point next empty space. Adds data element to
the stack location, where top is pointing.
Step 6:A Pop operation may involve Checks if the stack is empty, produces an error and exit.
Step 7: If the stack is not empty, accesses the data element at which top is pointing. Decreases
the value of top by 1.
Step 8:End the program
Program:
#include<stdio.h>
int
stack[100],choice,n,top,x,i;
void push(void); void
pop(void); void display(void);
int main()
{
//clrscr(); top=-
1;
printf("\n Enter the size of
STACK[MAX=100]:"); scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t ");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{ printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{ case 1:
{ push();
break; }
case 2:
{ pop();
break; }
case 3:
{
display()
; break; }
case 4:
{ printf("\n\t EXIT POINT ");
break;
}
default:
{ printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
} while(choice!
=4); return 0; } void
push()
{
if(top>=n-1)
{ printf("\n\tSTACK is over
flow");
}
else
{ printf(" Enter a value to be pushed:");
scanf("%d",&x); top++;
stack[top]=x;
}
} void
pop()
{ if(top<=-1)
{ printf("\n\t Stack is under flow");
}
else
{ printf("\n\t The popped elements is
%d",stack[top]); top--;
}
} void
display()
{ if(top>=0)
{ printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{ printf("\n The STACK is empty");
}
}
OUTPUT:
Enter the size of STACK[MAX=100]:10
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12
Enter the Choice:1
Enter a value to be pushed:24
98
24
12
Press Next Choice
Enter the Choice:2
24
12
Press Next Choice
Enter the Choice:4
EXIT POINT
RESULT:
Thus the program was written and executed successfully.
Ex.no:5.b Implementation of Queue using Array in C Date:
Aim:
To write a program for implementation of Queue using array in C
Algorithm:
Step 1:Queue operations may involve initializing or defining the queue, utilizing it, and
then completely erasing it from the memory.
Step2:This function helps to see the data at the front of the queue. The algorithm
of peek() ,Implementation of peek() function
Step 3:we just check for the rear pointer to reach at MAXSIZE to determine that the queue
is full,Implementation of isfull() function
Step 4:If the value of front is less than MIN or 0, it tells that the queue is not yet
initialized, hence empty.
Step 5:Check if the queue is full produce overflow error and exit.
Step 6:If the queue is not full, increment rear pointer to point the next empty
space. Step 7: Add data element to the queue location, where the rear is pointing.
Step 8:Check if the queue is empty.produce underflow error and exit.
Step9:If the queue is not empty, access the data where front is pointing. Increment front pointer
to point to the next available data element.
Program:
#include<stdio.h>
#include<conio.h
> #define n 5 void
main()
{ int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{ printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch) { case
1:
if(rear==x) printf("\n Queue
is Full");
else
{ printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==
rear)
RESULT:
Thus the program was written and executed successfully.
Ex.no:6.a C Program to Implement a Stack using Linked List Date:
Aim:
To write a C Program to Implement a Stack using Linked List
Algorithm:
Step 1:Start the program
Step 2:push an element into stack.
Step 3:struct node* push(struct node* head,int data)
struct node* tmp = (struct node*)malloc(sizeof(struct node)); if(tmp ==
NULL) Step 4:exit(0);
Step 5: tmp->data = data;
Step 6:tmp->next = head; head = tmp;
Step 7:return head Step
8:Stop the program
Program:
#include <stdio.h>
#include
<stdlib.h>
struct node
{ int info;
struct node
*ptr; }*top,*top1,*temp;
int topelement();
void push(int
data); void pop();
void empty(); void
display(); void
destroy(); void
stack_count(); void
create(); int count
= 0;
void main()
{ int no, ch, e;
while (1)
{ printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
/* Create empty stack
/* Pushdataintostack*/
voidpush(int data)
{
if (top == NULL)
*/ void
{ create()
{ toptop
= =(structnode*)malloc(1*sizeof(struct
NULL;
} node)); top->ptr = NULL;
top->info= data;
/ }
else
{ {
=(struct tempnode *)malloc(1*sizeof(struct
node)); = temp->ptr top; temp->info data;
top = } = count++;
temp;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
return(top->info);
1 - Push
2 - Pop
3 - Top
- Empty
4
- Exit
5 - Dipslay
6
- Stack Count
7 - Destroy stack
8 Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
90 78 56
Enter choice : 7
Stack is empty
Enter choice : 5
RESULT:
Thus the program was written and executed successfully.
Ex.no:6.b C Program to Implement a Queue using Linked List Date:
Aim:
To write a C Program to Implement a Queue using Linked List
Algorithm:
Step 1: Include all the header files which are used in the program. And declare all the
user defined functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define two Node pointers 'front' and 'rear' and set both to NULL. Create a newNode
with given value and set 'newNode → next' to NULL.
Step 4:Check whether queue is Empty (rear == NULL), If it is Empty then,
set front = newNode and rear = newNode. If it is Not Empty then, set rear →
next = newNode and rear = newNode.
Step 5:If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and
terminate from the function. If it is Not Empty then, define a Node pointer 'temp' and set it to
'front'. Step 6: Then set 'front = front → next' and delete 'temp' (free(temp)).
If it is Not Empty then, define a Node pointer 'temp' and initialize with front. Step 7: Display
'temp → data --->' and move it to the next node. Repeat the same until 'temp' reaches to 'rear'
(temp → next != NULL). Finally! Display 'temp → data ---> NULL'.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{ int Data; struct
Node* next; }*rear,
*front;
void delQueue()
{ struct Node *temp, *var=rear;
if(var==rear)
{ rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void display()
{ struct Node *var=rear;
if(var!=NULL)
{ printf("\nElements are as: ");
while(var!=NULL)
{ printf("\t%d",var->Data);
var=var->next;
}
printf("\
n"); }
else
printf("\nQueue is Empty");
}
int main()
{ int i=0; front=NULL; printf(" \n1. Push
to Queue"); printf(" \n2. Pop from
Queue"); printf(" \n3. Display Data of
Queue");
printf(" \n4. Exit\n");
while(1)
{ printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{ case 1:
{ int value; printf("\nEnter a valueber to push into
Queue: ");
scanf("%d",&value);
push(value);
display();
break;
} case 2:
{ delQueue();
display();
break;
} case 3:
{
display();
break;
} case 4:
{
exit(0)
;}
default:
{ printf("\nwrong choice for operation");
}
}
}
}
OUTPUT:
1.Push to Queue
2.Pop from Queue
3.Display Data of Queue
4.Exit
Choose Option: 1
Enter a valueber to push into Queue:
10 Elements are as: 10
Choose option : 1
Enter a valueber to push into Queue:
20 Elements are option : 1 as:
Choose 10 20
Enter a 20
valueber to
Elements are push 20
as: Choose into Queue
option : 2 30 20 30
Elements are
as: Choose
Option: 1
Enter a 30
valueber to
Elements are into Queue
as: Choose 40 30 40
Option:
RESULT:
push 10
Thus the program was written and executed successfully.
Ex.no:7.a Application of Stacks Date:
Algorithm:
Step 1: Scan the Infix string from left to right.
Step 2: Initialize an empty stack.
Step 3: If the scanned character is an operand, add it to the Postfix string. Step 4: If
the scanned character is an operator and if the stack is empty push the character to
stack. Step 5: If the scanned character is an Operator and the stack is not empty,
compare the precedence of the character with the element on top of the stack.
Step 6: character to stack. Repeat this step until the stack is not empty and top Stack
has precedence over the character.
Step 7: Repeat 4 and 5 steps till all the characters are scanned.
Step 8: After all characters are scanned, we have to add any character that the stack may have
to the Postfix string.
Step 9: If stack is not empty add top Stack to Postfix string and Pop the
stack. Step 10: Repeat this step as long as stack is not empty.
Program
#include <stdio.h>
#include
<conio.h>
#include <ctype.h>
#define SIZE 50
char s[SIZE]; int
top=-1; push(char
elem)
{ s[++top]=elem;
}
char pop()
{
return(s[top--]);
}
void main()
{ char infx[50],pofx[50],ch,elem;
int i=0,k=0; clrscr(); printf("\n\nRead the
Infix Expression ? ");
scanf("%s",infx); push('#');
while( (ch=infx[i++]) != '\0')
{ if( ch == '(') push(ch); else
if(isalnum(ch)) pofx[k++]=ch;
else
if( ch == ')')
{ while( s[top] != '(')
pofx[k++]=pop();
elem=pop(); }
else
{ while( pr(s[top]) >= pr(ch) )
pofx[k++]=pop();
push(ch);
}
} while( s[top] !=
'#')
pofx[k++]=pop();
pofx[k]='\0';
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n",infx,pofx);
getch();
}
OUTPUT:
Aim:
To write a program for First Come First Served
Algorithm:
Step 1:A Non-Preemptive scheduling algorithm. FIFO (First In First Out) Step
2:strategy assigns priority to process in the order in which they request the processor.
Step 3:The process that requests the CPU first is allocated the CPU first. Step 4:This is
easily implemented with a FIFO queue for managing the
tasks. Step 5:As the process come in, they are put at the end of the queue.
Step 6:As the CPU finishes each task, it removes it from the start of the queue and heads on to
the next task.
Program:
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
is 0
Time");
//calculating turnaround time for(i=0;i<n;i+
+)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i])
;}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt); printf("\
nAverage Turnaround Time:%d",avtat);
return
0; }
OUTPUT
Aim:
To write a program for implementation of Trees ,Tree Traversals.
Algorithm:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
while(1) {
parent = current;
while(current->data != data)
{ if(current != NULL)
printf("%d ",current-
>data); //go to left tree
if(current->data > data) {
current = current->leftChild;
} //else go to right tree else
{ current = current->rightChild;
}
return
current; }
int main()
{ int i;
int array[7] = { 27, 14, 35, 10, 19, 31,
42 }; for(i = 0; i < 7; i++) insert(array[i]);
if(temp != NULL) {
printf("[%d] Element found.", temp-
>data); printf("\n");
}else { printf("[ x ] Element not found
(%d).\n",
i); }
i = 15; temp =
search(i);
if(temp != NULL) {
printf("[%d] Element found.", temp-
>data); printf("\n");
}else { printf("[ x ] Element not found
(%d).\n",
i); }
return 0;
}
OUTPUT:
Preorder traversal: 27 14 10 19 35 31 42
Inorder traversal: 10 14 19 27 31 35 42
Aim:
To write a program for Implementation of Binary Search Trees
Algorithm:
Program:
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
}; typedef struct bin_tree
node;
void print_preorder(node *
tree) { if
(tree)
{ printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
void print_postorder(node *
tree) { if
(tree)
{
print_postorder(tree->left); print_postorder(tree-
>right)
; printf("%d\n",tree->data);
}
}
void main()
{ node *root;
node *tmp;
//int i;
root = NULL;
/* Inserting nodes into tree
*/ insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
Aim:
To write a program a for Implementation of Linear Search
Algorithm:
Step 1: Read the search element from the user
Step 2: Compare, the search element with the first element in the list.
Step 3: If both are matching, then display "Given element found!!!" and terminate the function
Step 4: If both are not matching, then compare search element with the next element in the list.
Step 5: Repeat steps 3 and 4 until the search element is compared with the last element in the
list. Step 6: If the last element in the list is also doesn't match, then display "Element not
found!!!" and terminate the function.
Program:
#include <stdio.h>
int main()
{ int array[100], search, c,
n;
%d integer(s)\n", n);
return 0;
}
OUTPUT:
Aim:
To write a program to Implementation of Binary Search
Algorithm:
Step 1:Given an array A of n elements with values or records A0, A1, ..., An−1, Step 2: sorted
such that A0 ≤ A1 ≤ ... ≤ An−1, and target value T, the following subroutine uses binary search
to find the index of T in A.
Step 3:Set L to 0 and R to n − 1.
Step 4: If L > R, the search terminates as unsuccessful.
Step 5: Set m (the position of the middle element) to the floor, or the greatest integer
less than (L + R) / 2.
Step 6: If Am < T, set L to m + 1 and go to step 2.
Step 7: If Am > T, set R to m − 1 and go to step 2.
Step 8:Now Am = T, the search is done; return m.
Program:
#include <stdio.h>
int main()
{ int c, first, last, middle, n, search,
array[100];
%d integers\n", n);
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
OUTPUT:
RESULT:
Thus the program was written and executed successfully.
Aim:
To write a program for implementation of Insertion Sort
Algorithm:
Step 1 − If it is the first element, it is already sorted. return
1; Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list Step 4 − Shift
all the elements in the sorted sub-list that is greater than the value to be
sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Program:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],i,j,k,n;
clrscr( ); printf("How many elements you want to
sort?\n"); scanf("%d",&n);
printf("\nEnter the Elements into an array:\
n"); for (i=0;i<n;i++) scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
k=a[i]; for(j= i-1; j>=0 &&
k<a[j]; j--)
a[j+1]=a[j];
a[j+1]=k;
} printf("\n\n Elements after sorting: \n");
for(i=0;i<n;i++) printf("%d\n", a[i]);
getch( );
}