In this tutorial you will learn about the C Program to Sort Array in Ascending Order and its application with practical example.
C Program to Sort Array in Ascending Order
In this tutorial, we will learn to create a C program that will Sort Array in Ascending Order using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Arithmetic operations in C Programming.
What is An array?
The array is a collection of similar data types. An array can store multiple values with different indexes in memory by using a single variable. The array can be both single as well as multidimensional.
Sort Array in Ascending Order
In this tutorial, we will sort the array in ascending order from the smallest to the biggest value of the array.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input size of the array. 3. Taking the elements in the array. 4. Sending that array to the for loop for sorting. 5. Print the Sorted element of array in ascending order. 6. End the program. |
Program description to Sort Array in Ascending Order.
In this program, we will first take the input array size and the elements from the user. Then we will sort all the elements of the array in ascending order. At last, we will print that sorted array elements.
With the help of this program, we can Sort Array in Ascending Order.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* * * C Program to Sort Array in Ascending Order * * */ #include <stdio.h> void main() { //declaring the required variables for the prorgam int i, j, a, n, arry[30]; //i = it will hold the integer value for the loop //j = it will hold the integere value for the loop //a = it will hold the integer value //n = it will hold the integer value //arry = it will hold the integer value //Taking the input number of elements of the array printf("Enter the value of N \n"); scanf("%d", &n); //Taking the array elements from the user printf("Enter the array's elements \n"); for (i = 0; i < n; ++i) scanf("%d", &arry[i]); //Sorting the array elements in the ascending order for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { if (arry[i] > arry[j]) { a = arry[i]; arry[i] = arry[j]; arry[j] = a; } } } //Printing the arranged array in ascending order. printf("The numbers arranged in ascending order are given below \n"); for (i = 0; i < n; ++i) printf("%d\n", arry[i]); } |
Output:-
In the above program, we have first initialized the required variable.
- j = it will hold the integer value.
- arry[] = it will hold the integer value of the input elements.
- i = it will hold the integer value.
- n = it will hold the integer value of the input.
Input number of elements from the user.
Program Logic Code.
Printing output in ascending order.