In this article, you’ll learn how to sort the elements of an array in ascending order in C/C++, along with that you’ll also see the algorithm to sort the elements of an array.
So, Let’s get started!
Prerequisite: You should have the knowledge of Traverse Operation in Array.
What is sort operation in array?
Sort operation in Array or sorting an array simply means, arranging the elements of an array in some specific (definite) logical order.
The ordering may be alphabetically according to some NAME key, or numerically according to some NUMBER key, such as social security number or account number, or it may also be alphanumerically.
How do you sort the elements of an array in C/C++?
Let’s see how you can sort the elements of an array step-by-step:
Variables we are using here:
- arr : Name of the array.
- n: Size of the array (i.e., Total number of elements in the array).
- i, j : Loop counter or counter variable for the
for
loop. - x : A temporary variable for swaping two values.
The following algorithm sort the elements of an array in ascending order.
Algorithm to Sort the elements of an Array:
- Step 01: Start
- Step 02: Repeat Step 03 to 07 for
i = 0
toi < n
- Step 03: Repeat Step 04 and 05 for
j = i + 1
toj < n
- Step 04: Check a statement using the
if
keyword, Ifarr[i] > arr[j]
, Swaparr[i]
andarr[j]
. - Step 05: [Increase counter. ] Set
j = j + 1
- Step 06: [End of step 03 loop. ]
- Step 07: [Increase counter. ] Set
i = i + 1
- Step 08: [End of step 02 loop. ]
- Step 09: Stop
Implementation in C:
// writing a program to perform sort operation on an array in C
#define MAX 50
void main()
{
int i, j, n, arr[MAX], x;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
x = arr[i];
arr[i] = arr[j];
arr[j] = x;
}
}
}
printf("\nThe numbers arranged in ascending order are given below: \n");
for (i = 0; i < n; i++)
printf("%d\n", arr[i]);
}
If you compile and run the above program, it will produce the following result:
Output:
Enter the size of the array: 6
Enter 6 elements:
963
147
52
10
5264
45
The numbers arranged in ascending order are given below:
10
45
52
147
963
5264
Implementation in C++:
// writing a program to perform sort operation on an array in C++
#include <iostream>
#define MAX 50
using namespace std;
int main()
{
int i, j, n, arr[MAX],x;
cout << "Enter the size of the array: " ;
cin >> n;
cout << "Enter " << n << " elements:\n" ;
for(i=0;i<n;i++)
cin >> arr[i];
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
x = arr[i];
arr[i] = arr[j];
arr[j] = x;
}
}
}
cout << "\nThe numbers arranged in ascending order are given below: \n" ;
for (i = 0; i < n; i++)
cout << arr[i] << "\n" ;
return 0;
}
If you compile and run the above program, it will produce the following result:
Output:
Enter the size of the array: 4
Enter 4 elements:
7
10
9
2
The numbers arranged in ascending order are given below:
2
7
9
10
Conclusion:
In this article, you learned how to sort the elements of an array in ascending order in C/C++, you also became familiar with the algorithm to sort the elements of an array.
So,
I really hope that you liked my article and got a lot of value from it.
Now, It’s your turn!
If you have any question, feel free to ask in the comment section below right now.
And, and, and…
Don’t forget to share this article to your lovely friends.
Coz, as you know, Sharing is Caring! 💗… Right?