how to arrange array in ascending order in c 2b 2b 5c

Solutions on MaxInterview for how to arrange array in ascending order in c 2b 2b 5c by the best coders in the world

showing results for - "how to arrange array in ascending order in c 2b 2b 5c"
Bear
24 Sep 2019
1#include <iostream>
2using namespace std;
3
4#define MAX 100
5
6int main()
7{
8	//array declaration
9	int arr[MAX];
10	int n,i,j;
11	int temp;
12	
13	//read total number of elements to read
14	cout<<"Enter total number of elements to read: ";
15	cin>>n;
16	
17	//check bound
18	if(n<0 || n>MAX)
19	{
20		cout<<"Input valid range!!!"<<endl;
21		return -1;
22	}
23	
24	//read n elements
25	for(i=0;i<n;i++)
26	{
27		cout<<"Enter element ["<<i+1<<"] ";
28		cin>>arr[i];
29	}
30	
31	//print input elements
32	cout<<"Unsorted Array elements:"<<endl;
33	for(i=0;i<n;i++)
34		cout<<arr[i]<<"\t";
35	cout<<endl;
36	
37	//sorting - ASCENDING ORDER
38	for(i=0;i<n;i++)
39	{		
40		for(j=i+1;j<n;j++)
41		{
42			if(arr[i]>arr[j])
43			{
44				temp  =arr[i];
45				arr[i]=arr[j];
46				arr[j]=temp;
47			}
48		}
49	}
50	
51	//print sorted array elements
52	cout<<"Sorted (Ascending Order) Array elements:"<<endl;
53	for(i=0;i<n;i++)
54		cout<<arr[i]<<"\t";
55	cout<<endl;	
56	
57	
58	return 0;
59	
60}