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

C Source Code Sorting Array in Ascending and Descending Order

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

C Source Code Sorting Array in Ascending and Descending Order

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C Source Code/Sorting array in ascending and

descending order
/*
Program - Array Sorting
Author - Vishal Solanki
Language - C Language
Date - 03/02/2018 (dd/mm/yyyy)
*/

//IDE used for this code is "Visual Studio 2017"

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


other functions
#include<conio.h>

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


getch();
}

//Ouput
/*

Array size: 10

Elements : 3 4 7 6 5 1 2 8 10 9

Ascending : 1 2 3 4 5 6 7 8 9 10

Descending : 10 9 8 7 6 5 4 3 2 1
*/

Retrieved from "https://en.wikiversity.org/w/index.php?


title=C_Source_Code/Sorting_array_in_ascending_and_descending_order&oldid=2141799"

This page was last edited on 19 April 2020, at 19:22.

Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this
site, you agree to the Terms of Use and Privacy Policy.

You might also like