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

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

  • In this problem, we are required to display the first half of the array in ascending order and the remaining half in descending order.
#include<stdio.h>
int main ()
{
int i = 0, temp, len, arr[50];
printf ("Enter the length of the array : ");
scanf ("%d", &len);
printf ("Enter the element of the array : ");

for (i = 0; i < len; i++)
{
scanf ("%d", &arr[i]);
}

for (i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
if(arr[j] < arr[i])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//printing first half of the array
printf ("Sorted Array ");

for (i = 0; i < len / 2; i++)
{
printf ("%d ", arr[i]);
}
//printing second half of the array

for (i = len - 1; i >= len / 2; i--)
{
printf ("%d ", arr[i]);
}
}
Disclaimer-: The questions provided on this page are only model practice questions there is no surety that these questions have been previously asked in any company placement papers, these questions here only have the sole purpose to make you practice coding questions