Transcript
Page 1: Program to sort a list of integers

Program to sort a list of integers#include<iostream.h>#include<conio.h>void main(){ int a[5],temp; clrscr(); cout<<"Enter elements of array a:"; for(int i=0;i<5;i++) cin>>a[i]; //iUsed to read the values of arrayfor(int j=0;j<=4;j++){ for(int i=1;i<5;i++) { if(a[i-1]>a[i]) //each I element is compared with i-1 element and swap the values if i-1 is greater then i { temp=a[i]; //swapping using third variable temp a[i]=a[i-1]; a[i-1]=temp; } }} cout<<"SORTED LIST OF ARRAY ELEMENTS IS:"<<endl;for(int k=0;k<5;k++) cout<<"Element "<<k+1<<" of Array is:"<<a[k]<<endl; // shown the sorted list getch();}

Top Related