write a program which accepts 10 integers in an array and then arrange the array in ascending order.
Answers
Answered by
9
#include <iostream>
using namespace std;
#define MAX 100
int main()
{
//array declaration
int arr[MAX];
int n,i,j;
int temp;
//read total number of elements to read cout<<"Enter total number of elements to read: ";
cin>>n;
//check bound
if(n<0 || n>MAX)
{ cout<<"Input valid range!!!"<<endl; return -1; }
//read n elements for(i=0;i<n;i++) { cout<<"Enter element ["<<i+1<<"] "; cin>>arr[i]; }
//print input elements
cout<<"Unsorted Array elements:"<<endl; for(i=0;i<n;i++)
cout<<arr[i]<<"\t"; cout<<endl;
//sorting - ASCENDING ORDER for(i=0;i<n;i++) {
for(j=i+1;j<n;j++)
{ if(arr[i]>arr[j]) { temp =arr[i]; arr[i]=arr[j]; arr[j]=temp; } } }
//print sorted array elements cout<<"Sorted (Ascending Order) Array elements:"<<endl; for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
return 0; }
using namespace std;
#define MAX 100
int main()
{
//array declaration
int arr[MAX];
int n,i,j;
int temp;
//read total number of elements to read cout<<"Enter total number of elements to read: ";
cin>>n;
//check bound
if(n<0 || n>MAX)
{ cout<<"Input valid range!!!"<<endl; return -1; }
//read n elements for(i=0;i<n;i++) { cout<<"Enter element ["<<i+1<<"] "; cin>>arr[i]; }
//print input elements
cout<<"Unsorted Array elements:"<<endl; for(i=0;i<n;i++)
cout<<arr[i]<<"\t"; cout<<endl;
//sorting - ASCENDING ORDER for(i=0;i<n;i++) {
for(j=i+1;j<n;j++)
{ if(arr[i]>arr[j]) { temp =arr[i]; arr[i]=arr[j]; arr[j]=temp; } } }
//print sorted array elements cout<<"Sorted (Ascending Order) Array elements:"<<endl; for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
return 0; }
Answered by
5
The program in java
class SelectionSort{//class declarationpublic void main(){//method declaration int a[]={1,6,9,8,3,2,5,7,4,10}; int size=a.length; int i,j,small,tmp,pos; for(i=0;i<size;i++) {//i loop start small=a[i]; pos=i; for(j=i+1;j<size;j++) {//j loop start if(a[j]<small) { small=a[i]; pos=j; } }//j loop end tmp=a[i]; a[i]=a[pos]; a[pos]=tmp;}//i loop end}//method end}//class end
class SelectionSort{//class declarationpublic void main(){//method declaration int a[]={1,6,9,8,3,2,5,7,4,10}; int size=a.length; int i,j,small,tmp,pos; for(i=0;i<size;i++) {//i loop start small=a[i]; pos=i; for(j=i+1;j<size;j++) {//j loop start if(a[j]<small) { small=a[i]; pos=j; } }//j loop end tmp=a[i]; a[i]=a[pos]; a[pos]=tmp;}//i loop end}//method end}//class end
Similar questions
Physics,
7 months ago
Math,
7 months ago
English,
1 year ago
Math,
1 year ago
Social Sciences,
1 year ago