c++

C++ Program to Sort an Integer Array in Ascending or Descending Order Using inbuilt Sort() Function Full Project For Beginners


 

Welcome folks today in this blog post we will be sorting an integer array in ascending or descending order using inbuilt function sort() method in c++ All the full source code of the application is shown below.

 

 

 

 

 

 

In order to get started you need to make a app.cpp file and copy paste the following code

 

 

app.cpp

 

 

 

In increasing order

 

 

 

// Program to sort array in increasing order using C++ inbuilt function 
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  cout<<"Enter number of elements you want to take in array: ";
  cin>>n;
  cout<<"nEnter array elements:n";
  int arr[n];
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }

  // Inbuit Sort function
  sort(arr,arr+n);
  cout<<"nArray after sorting in increasing order:n";
  for(int i=0;i<n;i++)
  {
    cout<<arr[i]<<" ";
  }
  return 0;
}

 

 

 

In Decreasing Order

 

 

// Program to sort array in decreasing order using C++ inbuilt function
#include<bits/stdc++.h>
using namespace std;

// Declaring compare function
bool compare(int a,int b)
{
  return a>b;
}
int main()
{
  int n;
  cout<<"Enter number of elements you want to take in array: ";
  cin>>n;
  cout<<"nEnter array elements:n";
  int arr[n];
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  
  // inbuilt sort function
  sort(arr,arr+n,compare);
  cout<<"nArray aftr sorting in decreasing order:n";
  for(int i=0;i<n;i++)
  {
    cout<<arr[i]<<" ";
  }
  return 0;
}

Similar Posts:

    None Found

Leave a Reply

Your email address will not be published. Required fields are marked *