C++ program to sort an array in ascending and descending order

Sort First half in Ascending and Second half in descending order in C++

Here, in this page we will discuss the program to sort first half in ascending and second half in descending order in C++ programming language. We are given with an array and need to print the required sorted array in the desired way.

sort array in ascending and descending order

Here, in this page we will discuss two different methods to sort the given array such that it’s first half is in ascending order and second half in descending order.

  • Method 1 : Using bubble sort
  • Method 2 : Sort the entire array then, print first half in ascending and second half in descending.

Method 1 :

This program takes a lot of inspiration from the bubble sort.

Apart from the fact that we divide the array into two halves. In the first half we sort in ascending order and in the second we sort in descending order.

Method 1 : Code in C++

Run
#include<bits/stdc++.h>
using namespace std;
void ascDecFunc(int a[], int n)
{
   int temp;
   for(int i=0;i < n-1;i++)
   {
     for(int j = 0;j < n/2; j++) { if(a[j]>a[j+1])
           {
             temp=a[j];
             a[j]=a[j+1];
             a[j+1]=temp;
           }
      }

      for(int j = n/2;j < n-1; j++)
      {
          if(a[j] < a[j+1])
          {
             temp=a[j];
             a[j]=a[j+1];
             a[j+1]=temp;
          }
      }
   }

   for(int i = 0; i < n; i++)
      cout<<a[i]<<" ";
}

int main()
{
    int arr[] = {3, 2, 4, 1, 10, 30, 40, 20};
    int len = sizeof(arr) / sizeof(arr[0]);
    ascDecFunc(arr, len);

    return 0;
}

Output

1 2 3 4 40 30 20 10 

Method 2 :

  • Sort the given array.
  • Run a loop up to half the length of the array and print the elements of the sorted array.
  • Run a loop from the last index of the array to the middle of the array and print the elements in reverse order.
Sort the array in C++

Method 2 : Code in C++

Run
#include<bits/stdc++.h>
using namespace std;

void ascDecFunc(int a[], int n)
{
   sort(a, a+n);
   
   // printing first half in ascending order
   for (int i = 0; i < n / 2; i++)
        cout<<a[i]<<" "; // printing second half in descending order 
   for (int j = n - 1; j >= n / 2; j--)
        cout<<a[j]<<" ";

}

int main()
{
    int arr[] = {3, 2, 4, 1, 10, 30, 40, 20};
    int len = sizeof(arr) / sizeof(arr[0]);
    ascDecFunc(arr, len);

    return 0;
}

Output

1 2 3 4 40 30 20 10 

2 comments on “C++ program to sort an array in ascending and descending order”


  • Satya Prakash

    //PROGRAM TO PRINT FIRST HALF IN ASCENDING AND SECOND HALF IN DESCENDING

    #include
    using namespace std;
    int main()
    {
    int a[] = {1,2,3,4,5,6,7,8,9,10};
    int n = sizeof(a)/sizeof(a[0]);
    int temp;
    for(int i=0;i<n;i++)
    {
    for(int j=0;j<=n/2;j++)
    {
    if(a[i]<a[j])
    {
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }
    }
    for(int j=n/2;ja[j])
    {
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }
    }
    }
    for(int i=0;i<n;i++)
    {
    cout<<a[i]<<" ";
    }
    }


  • Bibhudutta

    #include
    using namespace std;

    void ascending(int a[],int n){
    for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
    if(a[j]<a[i]){
    int temp=a[j];
    a[j]=a[i];
    a[i]=temp;

    }
    }
    }
    for(int i=0;i<n;i++){
    cout<<a[i]<<" ";
    }
    }

    void descending(int a[],int n){
    for(int i=0;i<n;i++){
    for(int j=i+1;ja[i]){
    int temp=a[j];
    a[j]=a[i];
    a[i]=temp;

    }
    }
    }
    for(int i=0;i<n;i++){
    cout<<a[i]<>n;
    int a[n];

    for(int i=0;i>a[i];
    }
    cout<<"Ascending :";
    ascending(a,n);

    cout<<endl;
    cout<<"Descending :";
    descending(a,n);
    return 0;
    }