Given An Integer Array Write A Program To Sort The First K Elements of This Array in Ascending and Remaining Elements in Descending Order in C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Given An Integer Array Write A Program To Sort The First K Elements Of This Array In Ascending And

Remaining Elements In Descending Order In C

/******************************************************************************

Online C Compiler.

Code, Compile, Run and Debug C program online.

Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

int*findArrSort(int*arr,int len,int k)

int i, j, temp;

/* first k elements in an array and sort that in ascending order*/

for(i=0; i<k; i++)

for(j=0; j<k; j++)

if(arr[i]<arr[j])

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;
}

/* from k+1 elements upto n elements in an array and sort that in descending order*/

for(i=k; i<len; i++)

for(j=k; j<len; j++)

if(arr[i]>arr[j])

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

return arr;

int main()

int i,*b,arr[100],len,k;

printf("Enter the size of list : ");


scanf("%d",&len);

printf("Enter the size of k : ");

scanf("%d",&k);

printf("Enter the n elements ");

for(i=0; i<len; i++)

scanf("%d",&arr[i]);

b=findArrSort(arr,len,k);

for(i=0; i<len; i++)

printf("%d ",b[i]);

return 0;

You might also like