# C++ Program to Sort the Elements of an Array in Ascending Order

To solve some problems effectively, it's important to arrange the data items in the correct sequence. One of the most popular arranging problems is the element sorting problem. This article will demonstrate how to arrange array members in C++ in ascending order (according to rising values).

To arrange either numerical or non-numerical elements in a specific order, a wide variety of sorting algorithms are available in this field. Only two straightforward sorting techniques will be covered in this article. the selection sort and the bubble sort. Let's examine each one individually using appropriate techniques and C++ implementation code.

## Sort array in ascending order using bubble sorting technique

One of the most popular and straightforward methods for sorting array components is the bubble sorting approach. In this method, two elements are checked one after the other to see if they are in the right order. If not, the method switches the elements around until they are in the right order. After that, move to the right and repeat the process for the other set of values. A single element is positioned in the proper intended position at the conclusion of each phase of the bubble sorting technique's few phases. Take a look at the bubble sorting algorithm.

### Algorithm

• read array A and its size n as input
• for i ranging from 0 to n-1, do
• for j ranging from 0 to n - 2, do
• if A[j] > A[j + 1], then
• swap A[j] and A[j + 1]
• end if
• end for
• end for

### Example

#include <iostream>
using namespace std;
void display( int arr[], int n ){
for ( int i = 0; i < n; i++ ) {
cout << arr[i] << ", ";
}
}
void swap ( int &a, int &b ){
int temp = a;
a = b;
b = temp;
}
void solve( int arr[], int n ){
int i, j;
for ( i = 0; i < n; i++ ) {
for ( j = 0; j < n-1; j++ ) {
if ( arr[j] > arr[ j+1 ] ) {
swap( arr[j], arr[ j + 1 ] );
}
}
}
}
int main(){
int arr[] = {8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84};
int n = sizeof( arr ) / sizeof( arr[0] );
cout << "Array before sorting: ";
display(arr, n);
solve( arr, n );
cout << "\nArray After sorting: ";
display(arr, n);
}


### Output

Array before sorting: 8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84,
Array After sorting: 2, 5, 8, 10, 12, 12, 25, 36, 44, 45, 58, 63, 74, 78, 84, 89, 95, 96,


## Sort array in ascending order using selection sorting technique

When using the selection sorting strategy, we start at index I and work our way to the end of the given array, finding either the smallest or maximum element. Assume we are uncovering every ingredient. It locates the minimum element from index I to the end of each phase, positions the element in the appropriate location, and then repeats the process to find the next maximum element from index I + 1 and so on. These stages will be finished, and then the array as a whole will be sorted appropriately.

### Algorithm

• read array A and its size n as input
• for i ranging from 0 to n-1, do
• ind := index of minimum element of A starting from i to n
• if A[ i ] > A[ ind ], then
• swap A[ i ] and A[ ind ]
• end if
• end for

### Example

#include <iostream>
using namespace std;
void display( int arr[], int n ){
for ( int i = 0; i < n; i++ ) {
cout << arr[i] << ", ";
}
}
void swap ( int &a, int &b ){
int temp = a;
a = b;
b = temp;
}
int min_index( int arr[], int n, int s, int e ){
int min = 99999, min_ind = -1;
for ( int i = s; i < e; i++ ) {
if ( arr[i] < min ) {
min = arr[i];
min_ind = i;
}
}
return min_ind;
}
void solve( int arr[], int n ){
int i, j, ind;
for ( i = 0; i < n; i++ ) {
ind = min_index( arr, n, i, n );
if ( arr[i] > arr[ ind ] ) {
swap( arr[i], arr[ ind ] );
}
}
}
int main(){
int arr[] = {8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84};
int n = sizeof( arr ) / sizeof( arr[0] );
cout << "Array before sorting: ";
display(arr, n);
solve( arr, n );
cout << "\nArray After sorting: ";
display(arr, n);
}


### Output

Array before sorting: 8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84,
Array After sorting: 2, 5, 8, 10, 12, 12, 25, 36, 44, 45, 58, 63, 74, 78, 84, 89, 95, 96,


## Conclusion

A basic problem is sorting, which involves arranging numbers or other items according to predetermined layout logics. There are many other sorting techniques available in this field, but in this post, we will focus on two that are simple to use and comprehend. These two techniques are the selection sorting technique and the bubble sort technique. We have arranged the data set in ascending (non-decreasing) order using these two techniques. Although not very time-efficient, these two sorting techniques are straightforward. Both of these two techniques need a time investment of O(n2), where n is the magnitude of the input. By just determining whether there will be no change in the subsequent phase if there is no swap in any phase, the bubble sort can be made faster.

Updated on: 14-Dec-2022

9K+ Views