C++ Program to Sort and Array in
1] Ascending Order
2] Descending Order
1] Ascending Order
1] Ascending Order
2] Descending Order
1] Ascending Order
#include<iostream.h> #include<conio.h> void main() { int arr[10]; int i,j,temp; for(i=0;i<=9;i++) { cout<<"Enter the element "<<i+1<<":- "; cin>>arr[i]; } cout<<"\nArray before sorting:- "; for(i=0;i<=9;i++) cout<<arr[i]<<" "; for(i=0;i<=9;i++) { for(j=i;j<=9;j++) { if(arr[i]<arr[j]) { temp=arr[j]; arr[j]=arr[i]; arr[i]=temp; } } } cout<<"\nArray after sorting:- "; for(i=0;i<=9;i++) cout<<arr[i]<<" "; }
2] Descending Order
#include<iostream.h> #include<conio.h> void main() { int arr[10]; int i,j,temp; for(i=0;i<=9;i++) { cout<<"Enter the element "<<i+1<<":- "; cin>>arr[i]; } cout<<"\nArray before sorting:- "; for(i=0;i<=9;i++) cout<<arr[i]<<" "; for(i=0;i<=9;i++) { for(j=i;j<=9;j++) { if(arr[i]>arr[j]) { temp=arr[j]; arr[j]=arr[i]; arr[i]=temp; } } } cout<<"\nArray after sorting:- "; for(i=0;i<=9;i++) cout<<arr[i]<<" "; }
No comments:
Post a Comment