How to Sort an Array in C++?
Last Updated :
10 Oct, 2024
Sorting an array involves rearranging its elements in a specific order such as from smallest to largest element or from largest to smallest element, etc. In this article, we will learn how to sort an array in C++.
Example:
Input: arr ={5,4,1,2,3}
Output: 1 2 3 4 5
Explanation: arr is sorted in increasing order
Input: arr ={3,2,5,4,1}
Output: 5 4 3 2 1
Explanation: arr is sorted in decreasing order
Sort an Array using Library Function
C++ STL provides std::sort() method which is used to sort the array in any desired order. This function uses a combination of quicksort, insertion sort and heapsort algorithm. The default sorting order is ascending order, but we can also provide the custom comparator as the third parameter to sort in user defined order.
Syntax
std::sort(arr, arr + n);
where arr is the array to be sorted and n is the size of the array.
Code Implementation
C++
// C++ Program to how to sort an array using
// std::sort() function
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = {5, 4, 1, 2, 3};
// Calculate the size of the array
int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array using std::sort()
sort(arr, arr + n);
for (auto i : arr)
cout << i << " ";
return 0;
}
Time Complexity: O(n log n), where n is the size of the array.
Auxiliary Space: O(log n)
Other Method to sort an Array in C++
To use other sorting algorithm apart from std::sort(), you have to manually implement it.
Using Bubble Sort
In Bubble Sort Algorithm, we repeatedly swap adjacent elements if they are in the wrong order. This process is repeated until the array is sorted.
C++
// C++ Program to sort an array using bubble sort
#include <bits/stdc++.h>
using namespace std;
// Function to perform bubble sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
// Swap if the element found is
// greater than the next element
if (arr[j] > arr[j + 1])
swap(arr[j],arr[j+1]);
}
}
}
int main() {
int arr[] = {5, 4, 3, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
// Sort arr using bubble sort
bubbleSort(arr, n);
for (auto i : arr)
cout << i << " ";
return 0;
}
Time Complexity: O(n2), where n is the size of the array.
Auxiliary Space: O(1)
Using Selection Sort
Selection sort is a simple sorting algorithm that repeatedly finds the minimum element from the unsorted part of the array and places it at its position in the sorted part of the array until the complete array is sorted.
C++
// C++ Program to sort an array using selection sort
#include <bits/stdc++.h>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
// Assume current index as minimum element
int min = i;
// Find the minimum element in the remaining
// unsorted array
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min])
min = j;
}
// If the new minimum element found, swap
/// it with current index
if (min != i) {
swap(arr[i],arr[min]);
}
}
}
int main() {
int arr[] = {5, 4, 3, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
// Sort arr using selection sort
selectionSort(arr,n);
for (auto i: arr)
cout<<i<<" ";
return 0;
}
Time Complexity: O(n2), where n is the size of the array.
Auxiliary Space: O(1)
Similar Reads
How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements:
2 min read
How to Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen
2 min read
How to Sort a Deque in C++? In C++, the STL provides a container called a double-ended queue which is popularly known as deque. This container allows fast insertions and deletions at both ends of the container. In this article, we will learn how to sort a deque in C++. Example: Input: myDeque = {30, 10, 20,50,40} Output: 10 20
2 min read
How to Find the Mode in a Sorted Array in C++? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will discuss how to calculate the mode of the numbers in a sorted array in C++. Example: Input: myVector = {1, 2, 2, 3, 3, 3, 4, 4, 5} Outp
3 min read
How to Sort a List in C++ STL? In C++, a list is a sequence container provided by the STL library of C++ that provides the features of a doubly linked list and stores the data in non-contiguous memory locations efficiently. In this article, we will learn how to sort a list in C++. Example: Input: myList = {30, 10, 20, 40, 50};Out
2 min read