
578 Programming and Data Structures
16.6 Write a program to sort a given array by using the exchange sort.
#include<Btdio.h>
#include<conio.h>
main()
{
int a [ 5 ] , i , j , temp;
c l r s c r ( ) ;
p r i n t f ( "Enter the elements of an array: ”);
fo r (i * 0 ;i< 5 ;i+ + )
sc a n f( "%d", & a[i] );
fo r (i«=0;i<5;i++ )
{
f o r (j = i ; j< 4 ;j + + )
{
i f ( a [ i ] > a [ j + l ] )
{
temp=a [ i ] ;
a [i ] »a [j+1 ] ;
a [j+1] =temp;
}
}
}
p r i n t f ( "Sorted elements in ascending ord er: ;
fo r(i = 0 ;i< 5 ;i+ + )
p r i n t f (" %d ”, a [ i ] ) ;
}
OUTPUT:
Enter the elements of an array: 9 5 14 3
Sorted elements in ascending order: 1 3 4 5 9
Ex ...