
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to sort an array in an ascending order
Problem
Sort the given array in descending or ascending order based on the code that has been written.
Solution
An array is a group of related data items which share’s a common name. A particular value in an array is identified with the help of its "index number".
Declaring array
The syntax for declaring an array is as follows −
datatype array_name [size];
For example,
float marks [50]
It declares ‘marks’ to be an array containing 50 float elements.
int number[10]
It declares the ‘number’ as an array to contain a maximum of 10 integer constants.
Each element is identified by using an "array index".
Accessing array elements is easy by using the array index.
The logic we use to sort the array elements in ascending order is as follows −
for (i = 0; i < n; ++i){ for (j = i + 1; j < n; ++j){ if (num[i] > num[j]){ a = num[i]; num[i] = num[j]; num[j] = a; } } }
Program
Following is the C program to sort an array in an ascending order −
#include <stdio.h> void main (){ int num[20]; int i, j, a, n; printf("enter number of elements in an array
"); scanf("%d", &n); printf("Enter the elements
"); for (i = 0; i < n; ++i) scanf("%d", &num[i]); for (i = 0; i < n; ++i){ for (j = i + 1; j < n; ++j){ if (num[i] > num[j]){ a = num[i]; num[i] = num[j]; num[j] = a; } } } printf("The numbers in ascending order is:
"); for (i = 0; i < n; ++i){ printf("%d
", num[i]); } }
Output
When the above program is executed, it produces the following result −
enter number of elements in an array 5 Enter the elements 12 23 89 11 22 The numbers in ascending order is: 11 12 22 23 89
- Related Articles
- C program to sort an array of ten elements in an ascending order
- 8086 program to sort an integer array in ascending order
- Java Program to Sort Array list in an Ascending Order
- C++ Program to Sort the Elements of an Array in Ascending Order
- Golang Program To Sort An Array In Ascending Order Using Insertion Sort
- Python program to sort the elements of an array in ascending order
- Golang Program To Sort The Elements Of An Array In Ascending Order
- Swift Program to Sort the Elements of an Array in Ascending Order
- How do you sort an array in C# in ascending order?
- C# program to sort an array in descending order
- C program to sort an array in descending order
- How to sort an ArrayList in Ascending Order in Java
- How to sort an ArrayList in Java in ascending order?
- Java Program to sort an array in alphabetical order
- C++ Program to Sort the Elements of an Array in Descending Order
