Ex.No:4c Sorting
AIM:
To write a C program to sort an array of n integers.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the array a.
Step 3: Repeat for i ← 0 to ArraySize
Repeat for j← i to ArraySize
If a[i] > a[j], swap a[i] and a[j]
Step 4: Print the sorted array a.
Step 5: Stop the program.
PROGRAM CODE:
#include<stdio.h> #include<conio.h> void main() { int a[10],temp,i,j,size; clrscr(); printf("n Enter the array size : "); scanf("%d",&size); printf("n Enter the array to be sorted : "); for(i=0;i<size;i++) scanf("%d",&a[i]); for(i=0;i<size;i++) for(j=i+1;j<size;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("n Sorted Array : "); for(i=0;i<size;i++) printf("%d ",a[i]); getch(); }
OUTPUT:
RESULT:
Thus the C program to sort an array of n integers is written and executed successfully.