Quick actions

cmd+k|ctrl+k

Navigation

Languages

Sorting Algo

Snippet info

Language

Cpp

Visibility

public

Author

matho.camara

Created

2018-10-24T10:41:38Z

Updated

2018-10-24T10:43:30Z

#include <iostream>
using namespace std;

int main()                        
{
	int a[100],n,i,j;
	printf("Array size: ");
        cin>>n;
        cout<<"Input Elements:"<<endl;
        
      for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
	for (int i = 0; i < n; i++)                     //Loop for ascending ordering
	{
		for (int j = 0; j < n; j++)             //Loop for comparing other values
		{
			if (a[j] > a[i])                //Comparing other array elements
			{
				int tmp = a[i];         //Using temporary variable for storing last value
				a[i] = a[j];            //replacing value
				a[j] = tmp;             //storing last value
			}  
		}
	}
	cout<<"\n\nAscending : "<<endl;                     //Printing message
	for (int i = 0; i < n; i++)                     //Loop for printing array data after sorting
	{
		cout<<a[i]<<endl;
	}
	for (int i = 0; i < n; i++)                     //Loop for descending ordering
	{
		for (int j = 0; j < n; j++)             //Loop for comparing other values
		{
			if (a[j] < a[i])                //Comparing other array elements
			{
				int tmp = a[i];         //Using temporary variable for storing last value
				a[i] = a[j];            //replacing value
				a[j] = tmp;             //storing last value
			}
		}
	}
	cout<<"\n\nDescending : "<<endl;                    //Printing message
	for (int i = 0; i < n; i++)                     //Loop for printing array data after sorting
	{
		cout<<a[i]<<endl;                   //Printing data
	}

	return 0;                                       //returning 0 status to system
}
INFO