C++ Program to Sort first half in ascending order and second half in descending order in an array

Sorting first half in ascending order and second half in descending order

In this article we will learn how to write a C++ program for sorting first half in ascending order and second half in descending order.

we will sort the array in ascending order and divided it into 2 equal parts. Then we will sort second part in descending order and merge both the parts.

Input array : [3, 5, 2, 7, 9, 1]

Output array : [1, 2, 3, 9, 7, 5]

Sorting first half array in ascending order & second half in descending order

Algorithm:-

  1. Initialize a variable len, for accepting the length of the array.
  2. Take the input from the user using a for loop.
  3.  Sort the inputted array in the ascending order.
  4. Use two for loops for printing the array
    1. Use the first loop from 0 to len/2 for printing the first ascending part of the array.
    2. Use the second for loop from len-1 to len/2 for printing the second descending part of the array.

C++ Code:-

    
    //C++ Program
    // Program for sorting first half in ascending order and second half in descending
    #include<iostream>
    using namespace std;
    int main()
    {
        int i=0, temp;
        
        cout<<“Enter the length of the array : “;
        cin>>len;
        int arr[50];
        cout<<“Enter the element of the array : “;
        for( i=0;i < len;i++)
        {
            cin>>arr[i];
        }
        for( i=0;i < len-1;i++)
        {
            for(int j=i+1;j<len;j++)
                {
                    if(a[j]<a[i]){
                        temp=arr[i];
                        arr[i]=arr[j];
                        arr[j]=temp;
                    }
                    
                }
            }
        }
 //printing first half of the array
       cout<<“Sorted array “;
        for( i=0;i < len/2;i++)
        {
            cout<<arr[i]<<\n;
        }
//printing second half of the array
       
        for( i=len-1;i >= len/2;i–)
        {
            cout<<arr[i]<<\n;
        }

    }
    
}
    
    Output
    Enter the length of the array : 
    6
    Enter the element of the array : 
    4
    5
    2
    3
    1
    6
    Sorted array
    1
    2
    3
    6
    5
    4

Important Codes related to Arrays

  • Find Smallest Element in an Array : C | C++ | Java | Python
  • Find Second Smallest Element in an Array : C | C++ | Java | Python
  • Find Largest element in an array : C | C++ | Java | Python
  • Find the Smallest and largest element in an array : C | C++ | Java | Python
  • Calculate the sum of elements in an array : C | C++ | Java | Python
  • Reverse an Array : C | C++ | Java | Python
  • Sort first half in ascending order and second half in descending : C | C++ | Java | Python
  • Sort the elements of an array : C | C++ | Java | Python
  • Finding the frequency of elements in an array : C | C++ | Java | Python
  • Finding the Longest Palindrome in an Array : C | C++ | Java| Python
  • Counting Distinct Elements in an Array : C | C++ | Java| Python
  • Finding  Repeating elements in an Array : C | C++ | Java | Python
  • Finding Non Repeating elements in an Array : C | C++ | Java | Python
  • Removing Duplicate elements from an array : C | C++ | Java
  • Finding Minimum scalar product of two vectors : C | C++ | Java | Python
  • Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
  • Counting the number of even and odd elements in an array : C | C++ | Java
  • Find all Symmetric pairs in an array : C | C++ | Java
  • Find maximum product sub-array in a given array : C | C++ | Java
  • Finding Arrays are disjoint or not : C | C++ | Java
  • Determine Array is a subset of another array or not : C | C++ | Java
  • Determine can all numbers of an array be made equal : C | C++ | Java
  • Finding Minimum sum of absolute difference of given array : C | C++ | Java
  • Sorting elements of an array by frequency : C | C++ | Java
  • Sort an array according to the order defined by another array : C | C++ | Java
  • Replace each element of the array by its rank in the array : C | C++ | Java
  • Finding equilibrium index of an array : C | C++ | Java| Python
  • Rotation of elements of array- left and right : C | C++ | Java| Python
  • Block swap algorithm for array rotation : C | C++ | Java| Python
  • Juggling algorithm for array rotation : C | C++ | Java | Python
  • Finding Circular rotation of an array by K positions : C | C++ | Java | Python

You can find the below Programs in all languages

array