C Examples Ascending and Descending Order

Why Sorting (Ascending and Descending) is Useful?

  • Faster searching – e.g., binary search only works on sorted data.
  • Better readability – humans can understand sorted data quickly.
  • Efficiency – helps algorithms, databases, and reports run faster.

Real-world uses:

  1. Shopping websites – Sort products by price (low → high).
  2. Phone contacts – Sorted alphabetically (A → Z).
  3. Student marksheets – Roll numbers or marks sorted lowest to highest.
  4. Task management apps – Show oldest tasks first.
  5. Bank transactions – Arranging dates from earliest → latest.

Here below we can see some C Examples Ascending and Descending Order

Ascending Order of N Numbers in Array

Example:1

//Example-1:
//Write a program to arrange N given numbers in ascending
#include<stdio.h>
int main()
{
  int i, j, n, a[50], temp;
  printf("Give the value of N :\n"); // maximum N value is 50.
  scanf("%d",&n);
  printf("Enter the numbers one by one\n");
  for(i=0;i<n;i++)  scanf("%d",&a[i]);
  for(i=0;i<n;i++)
  {
  	for(j=i+1;j<n;j++)
  	{
  		if(a[i]>a[j])
  		{
  			temp = a[i];
  			a[i] = a[j];
  			a[j] = temp;
		  }
	}
  }
  printf("Printing the list = %d \n");
  for (i=0;i<n;i++) printf("%d\t",a[i]);

  return 0;
}

/* Output

Give the value of N :
4
Enter the numbers one by one
33
16
999
670
Printing the list = 999
16      33      670     999
*/

/* Output is
Give the value of N :
5
Enter the numbers one by one
10
30
15
24
1
small number = 1
*/

Program Purpose

The program is designed to arrange N user-provided numbers in ascending order (from smallest to largest) and display the sorted list.


Key Components

  1. Header File
#include<stdio.h>
  • Includes the Standard Input/Output library for functions like printf and scanf.
  1. Variable Declaration
int i, j, n, a[50], temp;
  • i, j → Loop counters.
  • n → Number of elements the user will input.
  • a[50] → Array to store up to 50 integers.
  • temp → Temporary variable for swapping numbers.

Input from User

printf("Give the value of N :\n");
scanf("%d",&n);
printf("Enter the numbers one by one\n");
for(i=0;i<n;i++)  scanf("%d",&a[i]);
  • Asks the user for the number of elements (N).
  • Reads N numbers into the array a[].

Note: Maximum N allowed is 50 because the array size is a[50].


Sorting Logic (Ascending Order)

for(i=0;i<n;i++)
{
  	for(j=i+1;j<n;j++)
  	{
  		if(a[i]>a[j])
  		{
  			temp = a[i];
  			a[i] = a[j];
  			a[j] = temp;
		  }
	}
}
  • Uses nested loops to compare all pairs of numbers.
  • if(a[i] > a[j]) → Checks if the current number is greater than the next number.
  • If true, swap the numbers using temp.
  • This is a simple selection sort algorithm.

Example:
Input: 33, 16, 999, 670

  • First pass swaps 33 and 16 → 16, 33, 999, 670
  • Next pass swaps 999 and 670 → 16, 33, 670, 999

Printing the Sorted List

printf("Printing the list = %d \n");
for (i=0;i<n;i++) printf("%d\t",a[i]);
  • Displays the sorted numbers in ascending order.
  • \t → Tab space for better formatting.

Output Example:

16    33    670    999

Notes and Observations

  1. The first printf("Printing the list = %d \n"); has an unused %d. It can be removed or corrected.
  2. The second output in your snippet mentions “small number = 1” — this is not in the current program. To find the smallest number, you can simply print a[0] after sorting:
printf("Small number = %d\n", a[0]);
  1. The algorithm used is simple but not efficient for very large N. For large datasets, algorithms like Quick Sort or Merge Sort are better.

Example:2

#include <stdio.h>

int main()
{
    int aew_temp, aew_arr[5]={20,30,10,25,1};
    
    printf("ArunEworld default array values [%d]: ",(int)(sizeof(aew_arr)/sizeof(int)));
    
    for(int i=0;i<5;i++)
    {
        printf("%d ",i[aew_arr]);
    }
        
    printf("\n\r");
    
    for(int j=0;j<(sizeof(aew_arr)/sizeof(int))-1;j++)
    {
        for(int i=0;i<(sizeof(aew_arr)/sizeof(int))-1;i++)
        {
            if(aew_arr[i]>aew_arr[i+1])
            {
                aew_temp = aew_arr[i];
                aew_arr[i] = aew_arr[i+1];
                aew_arr[i+1] = aew_temp;
            }
        }
    }
    
    printf("ArunEworld array Assending order values [%d]: ",(int)(sizeof(aew_arr)/sizeof(int)));
    for(int i=0;i<5;i++)
        printf("%d ",i[aew_arr]);

    return 0;
}

/* Output
ArunEworld default array values [5]: 20 30 10 25 1 
ArunEworld array Assending order values [5]: 1 10 20 25 30 

...Program finished with exit code 0
Press ENTER to exit console.
*/

Descending Order of N Numbers in Array

Example:1

//Example-1
#include <stdio.h>

int main(void) {
  int n, a[]= {55,99,44,77,33,88,22,77,11,66};

  n = sizeof(a);
  n = n/4;
  int i,j,k,temp;
  printf("\n\t*****************\n");
   for (i=0; i<n; i++) printf("\t %d", a[i]); 
  printf("\n");
  printf("\n\t*****************\n");
   for(i=0;i<n;i++)
  {
  	for(j= i+1;j<n;j++)
  	{
  		if(a[i]>a[j])
  		{
  			temp = a[i];
  			a[i] = a[j];
  			a[j] = temp;
		  }
      for (k=0; k<n; k++) printf("\t %d", a[k]);
      printf("\n");
	}
  printf("\n");
}
printf("\n\t*****************\n");
   for (i=0; i<n; i++) printf("\t %d", a[i]); 
  return 0;
}


/* Output is
11	 22	 33	 44	 55	 66	 77	 77	 88	 99 
*/

Example:2

/**************************************************************************************************************/
//Example-2
#include <stdio.h>

int main()
{
    int aew_temp, aew_arr[5]={20,30,10,25,1};
    
    printf("ArunEworld default array values [%d]: ",(int)(sizeof(aew_arr)/sizeof(int)));
    
    for(int i=0;i<5;i++)
    {
        printf("%d ",i[aew_arr]);
    }
        
    printf("\n\r");
    
    for(int j=0;j<(sizeof(aew_arr)/sizeof(int))-1;j++)
    {
        for(int i=0;i<(sizeof(aew_arr)/sizeof(int))-1;i++)
        {
            if(aew_arr[i]<aew_arr[i+1])
            {
                aew_temp = aew_arr[i];
                aew_arr[i] = aew_arr[i+1];
                aew_arr[i+1] = aew_temp;
            }
        }
    }
    
    printf("ArunEworld array Desending order values [%d]: ",(int)(sizeof(aew_arr)/sizeof(int)));
    for(int i=0;i<5;i++)
        printf("%d ",i[aew_arr]);

    return 0;
}


/* Output
ArunEworld default array values [5]: 20 30 10 25 1 
ArunEworld array Desending order values [5]: 30 25 20 10 1 

...Program finished with exit code 0
Press ENTER to exit console.

*/

Example:3

/************************************************************************************************************/
//Example-3:
#include <stdio.h>
int main()
{
    int aew_temp, len=0,count=0, aew_arr[]={1,2,1,1,14,2,3,2,3,4};//,2,2,5,6,20,30,30,25,30};
    
    len = (int)(sizeof(aew_arr)/sizeof(int));
    printf("ArunEworld default array values [%d]: ",len);
    
    for(int i=0;i<len;i++)
    {
        printf("%d ",i[aew_arr]);
    }
        
    printf("\n\r");
    if(len !=1 && len != 0)
    {
        for(int i=0; i<len-1;i++)
        {
            for(int j=i; j<len-1;j++)
            {
                if(aew_arr[i]== aew_arr[j+1])
                {
                    for(int k=j+1;k<len-1;k++)
                        aew_arr[k]=aew_arr[k+1];
                        
                    len--;
                }
            }
        }
    }
    
    printf("ArunEworld array Desending order values [%d]: ",len);
    for(int i=0;i<len;i++)
        printf("%d ",i[aew_arr]);

    return 0;
}


Previous & Next


Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off