How to Sort an Array in C++
What is Sorting?
Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms, including bubble sort, insertion sort, selection sort, merge sort, quick sort, heap sort, etc. In this article, we will see to sort an array using Insertion sort and Selection sort.
1. Sorting an array using Selection sorting:
In a section sort, the smallest element in the array is repeatedly selected and substituted for the element at the beginning of the unsorted array to produce a sorted array.
Example for selection sorting:
#include<iostream>
using namespace std;
void selectionSort(int a[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++)
if (a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main() {
int a[] = { 1,44,55,79,12,25,60,39,100,49,69};
int n = sizeof(a)/ sizeof(a[0]);
int i;
cout<<"User entered array is:"<<endl;
for (i = 0; i < n; i++)
cout<< a[i] <<" ";
cout<<endl;
selectionSort(a, n);
printf("Sorted array is: \n");
for (i = 0; i < n; i++)
cout<< a[i] <<" ";
return 0;
}
Output:

Explanation:
The above code is written to sort an array using a selection algorithm. In the above code, a function with the name selection sort() is created that is used to sort the array. We have used two loops in the selection sort() function. In every loop, the smallest element found is placed at the beginning of the array. In the main function, we call the function selectionsort(), and the output gets printed.
- Example of Another method to sort the array:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
//taking input from the user using cout statement
cout<<"Enter the size of array: "; cin>>n;
int a[n];
cout<<"\nEnter the elements: ";
for(int i=0; i<n; i++) cin>>a[i];
//using for loop to sort the array and then print the new sorted array
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++) { if(a[i]>a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout<<"\nArray after swapping: ";
for(int i=0; i<n; i++)
cout<<a[i]<<" ";
return 0;
}
Output:

Explanation:
In the above example, we have taken the input from the user to sort the array. Here user need to provide the array size and the array values for sorting. Then using the cout statement, we have printed the sorted array. In the main function we have declared two for loops for sorting. In one for loop we have compared all the elements with each other and then properly arranged them.
- Sorting of an array using standard templet library
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1,44,55,79,12,25,60,39,100,49,69};
// Get size of array
size_t len = sizeof(arr) / sizeof(arr[0]);
// Calling sort() function from STL
sort(arr, arr + len);
// Printing Output
cout<<"The sorted array is: ";
for (int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout<<endl;
}
Output:

Explanation:
The above code is written to sort the array. Unlike the previous examples, we have used standard templet library’s pre-defined function sort(), which automatically sorts the given array. Then using the cout statement, we have printed the sorted array.
2. Sorting an array using Insertion sort
#include <iostream>
using namespace std;
void insertion_sort(int* arr, size_t len)
{
int temp;
// Assuming one element as sorted and inserting other elements into it
for (int i = 1; i < len; i++)
{
temp = arr[i];
// Finding the sorted position for the selected element in the sorted array and placing it.
for (int j = i - 1; j >= 0; j--)
{
if (temp > arr[j])
{
arr[j + 1] = temp;
break;
}
else if (temp <= arr[j])
{
arr[j + 1] = arr[j];
if (j == 0)
{
arr[j] = temp;
}
}
}
}
}
// Driver Code
int main()
{
int arr[] = { 1,44,55,79,12,25,60,39,100,49,69};
size_t len = sizeof(arr)/sizeof(arr[0]);
// Calling insertion_sort function with given array
insertion_sort(arr, len);
// Printing Output
cout << "The sorted Array is: ";
for (int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout<<endl;
}
Output:

Explanation:
A subarray is assumed to be sorted in the above code, and elements are added. In the beginning, only one element of the array is assumed to be sorted, and insetion_sort() implements the insertion sort.