# How to sort array in c++

Welcome dear programmers! We are about to discuss the sorting of array in c++ in ascending as well as descending order. Sorting of data basically means arranging or organizing the data in a particular fashion. There are numerous algorithms available but we are using selection sort because its basic, simple and easy. A good understanding of sorting helps us a lot to master concepts of not only arrays but also nested loops. Let’s learn sorting.

## Stepwise Algorithm to sort array?

As we already mentioned in the intro, we will use selection sort to sort our array. Let’s discuss how it works.

•  In selection sort, we logically divide the array into sorted and unsorted portions.
• The sorted portion will have zero elements in the beginning.
•  We will find the smallest element in the array and swap it with the first element of the unsorted array.
•  After this process, the elements in the sorted array will gradually increase.
•  We will repeat this process until we get all the elements from the unsorted to sorted portion.
•  At the end of the selection sort, all the elements will be placed in sorted portion and the unsorted portion will be empty.
•  If we want to sort the array in descending order, we will find the maximum or largest element in the array instead of the smallest. It’s just a small difference.

See the diagram below to understand selection sort in more detail.

## Code to sort array in ascending order?

•  Declare an array called “ArrayofInt” to store the data to sort and two int type variables called size and temp.
• Take input from user in the array using simple for loop.
```int ArrayofInt[100];
int size;
int temp;
//input the unsorted array
cout << "enter the size of array" << endl;
cin >> size;
for (int i = 0; i < size; i++) {
cout << "Enter at array index " << i << endl;
cin >> ArrayofInt[i];
}```
• Display the unsorted data to the user again using for loop.
```// Printing the unsorted array to user
cout << "|Unsorted array|" << endl;
for (int i = 0; i < size; i++) {
cout << ArrayofInt[i] << " ";
}
cout << endl;```
• Sort the data using the selection sort algorithm.
•  avail nested for loop to find the smallest element in the unsorted array.
•  Swap the smallest element with the first element of the unsorted array.
•  One complete execution of the nested loop with counter “j” finds and swaps the smallest element one time.
•  Then the outer loop with counter “i” separates the sorted portion from the unsorted portion of elements in the array.
•  After the execution of outer loop “i”, all the elements will be in the sorted portion of the array and there will be no element in the unsorted portion.
``` //sort array in ascending order using selection sort
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (ArrayofInt[i] > ArrayofInt[j]) {
//swapping with smallest element of array.
temp = ArrayofInt[j];
ArrayofInt[j] = ArrayofInt[i];
ArrayofInt[i] = temp;
}
}
}```
• Now, finally, print the sorted array in ascending order to the user.
```// printing the sorted array in ascending order.
cout << "|sorted array|" << endl;
for (int i = 0; i < size; i++) {
cout << ArrayofInt[i] << " ";
}```

That’s how we sort an array in ascending order using selection sort the algorithm in c++.

## Complete program to sort array in ascending order

The complete code of the array sorting program is given below.

```#include<iostream>
using namespace std;
int main() {
int ArrayofInt[100];
int size;
int temp;
//input the unsorted array
cout << "enter the size of array" << endl;
cin >> size;
for (int i = 0; i < size; i++) {
cout << "Enter at array index " << i << endl;
cin >> ArrayofInt[i];
}
// Printing the unsorted array to user
cout << "|Unsorted array|" << endl;
for (int i = 0; i < size; i++) {
cout << ArrayofInt[i] << " ";
}
cout << endl;
//sort array in ascending order using selection sort
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (ArrayofInt[i] > ArrayofInt[j]) {
//swapping with smallest element of array.
temp = ArrayofInt[j];
ArrayofInt[j] = ArrayofInt[i];
ArrayofInt[i] = temp;
}
}
}
// printing the array in ascending order.
cout << "|sorted array|" << endl;
for (int i = 0; i < size; i++) {
cout << ArrayofInt[i] << " ";
}
cout << endl;

return 0;
}```

## Program to sort an array in descending order

• Both programs are almost the same. We will use greater than sign this time in the if statement.
• Find the largest element in the array and swap it with the first element of the unsorted array.

The complete code of the array sorting program is given below.

```#include<iostream>
using namespace std;
int main() {
int ArrayofInt[100];
int size;
int temp;
//input the unsorted array
cout << "enter the size of array" << endl;
cin >> size;
for (int i = 0; i < size; i++) {
cout << "Enter at array index " << i << endl;
cin >> ArrayofInt[i];
}
// Printing the unsorted array to user
cout << "|Unsorted array|" << endl;
for (int i = 0; i < size; i++) {
cout << ArrayofInt[i] << " ";
}
cout << endl;
//sort array in descending order using selection sort
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (ArrayofInt[i] > ArrayofInt[j]) {
//swapping with largest element of array.
temp = ArrayofInt[j];
ArrayofInt[j] = ArrayofInt[i];
ArrayofInt[i] = temp;
}
}
}
// printing array in descending order
cout << "|sorted in descending order|" << endl;
for (int i = 0; i < size; i++) {
cout << ArrayofInt[i] << " ";
}
cout << endl;
}```

I hope you enjoyed sorting arrays in ascending and descending order. We will meet again soon to discuss a new topic related to programming. Thanks for reading and supporting.

Scroll to Top