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

  • Post author:
  • Post category:c++
  • Post comments:0 Comments

 

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.

 

 

 

Get Started

 

 

 

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;
}

 

See also  C Graphics Program to Implement Liang Barsky Line Clipping Algorithm

 

 

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;
}

Leave a Reply