0% found this document useful (0 votes)
725 views

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

The document describes a C program to sort the first k elements of an integer array in ascending order and the remaining elements in descending order. The program takes an integer array, its length, and an integer k as input. It uses nested for loops to sort the first k elements in ascending order and elements from k+1 to the end in descending order. The sorted array is returned and printed.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
725 views

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

The document describes a C program to sort the first k elements of an integer array in ascending order and the remaining elements in descending order. The program takes an integer array, its length, and an integer k as input. It uses nested for loops to sort the first k elements in ascending order and elements from k+1 to the end in descending order. The sorted array is returned and printed.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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