program to sort a list of integers

1
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 array for(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(); }

Upload: harman-kaur

Post on 06-May-2015

449 views

Category:

Education


1 download

DESCRIPTION

This is a C++ program that sorts the list of integer values stored in array.An array can be of any size, here i have used an array of size 5.

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();}