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

Ascending and Desending Order: #Include

The document discusses sorting an array of integers in both ascending and descending order. It takes user input for the array size and elements, then uses nested for loops to compare elements and swap their positions to sort the array in ascending order. It then performs the same process but compares elements in reverse order to sort the array descending. The sorted arrays are printed out at the end.

Uploaded by

Manoj Guru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Ascending and Desending Order: #Include

The document discusses sorting an array of integers in both ascending and descending order. It takes user input for the array size and elements, then uses nested for loops to compare elements and swap their positions to sort the array in ascending order. It then performs the same process but compares elements in reverse order to sort the array descending. The sorted arrays are printed out at the end.

Uploaded by

Manoj Guru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Ascending and Desending order

#include <stdio.h> //including stdio.h for printf and other


functions

int main() //default function for call


{
int a[100],n,i,j;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 0; i < n; i++) //Loop for
ascending ordering
{
for (int j = 0; j < n; j++) //Loop for
comparing other values
{
if (a[j] > a[i]) //Comparing other
array elements
{
int tmp = a[i]; //Using temporary
variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nAscending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing
array data after sorting
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++) //Loop for
descending ordering
{
for (int j = 0; j < n; j++) //Loop for
comparing other values
{
if (a[j] < a[i]) //Comparing other
array elements
{
int tmp = a[i]; //Using temporary
variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nDescending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing
array data after sorting
{
printf(" %d ", a[i]); //Printing data
}

return 0; //returning 0
status to system
}

You might also like