46.1k views
6 votes
An algorithm to print a set of 10 integers in ascending order

User Winkbrace
by
4.6k points

1 Answer

10 votes

Answer:

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("\\\\Ascending : "); //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("\\\\Descending : "); //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

*/

Step-by-step explanation:

User Olympia
by
5.1k points