C++ Program to Sort the Elements of an Array in Ascending Order
Last Updated :
10 Jul, 2022
Here, we will see how to sort the elements of an array in ascending order using a C++ program. Below are the examples:
Input: 3 4 5 8 1 10
Output: 1 3 4 5 8 10
Input: 11 34 6 20 40 3
Output: 3 6 11 20 34 40
There are 2 ways to sort an array in ascending order in C++:
- Brute-force Approach Using Bubble Sort.
- Optimized Approach Using Quicksort.
Let's start discussing these solutions.
1. Brute-force Approach Using Bubble Sort
Here, the brute force approach is used using the bubble sort method. Below is the C++ program to sort the array in ascending order using the brute-force method using bubble sort:
C++
// C++ program to sort array
// in ascending order using
// Brute-force approach
// using bubble sort
#include <bits/stdc++.h>
using namespace std;
void sort(int num[], int len);
void swapNums(int nums[],
int first, int second);
// Driver code
int main()
{
// Initializing arrya
int nums[] = {1, 12, 6, 8, 10};
int size_nums = (sizeof(nums) /
sizeof(nums[0]));
cout << "Before sorting the array is: \n";
for (int i = 0; i < size_nums; i++)
cout << nums[i] << " ";
cout << "\n\n";
sort(nums, size_nums);
cout << "After sorting the array is: \n";
for (int i = 0; i < size_nums; i++)
cout << nums[i] << " ";
cout << "\n";
return 0;
}
// Sort function
void sort(int num[], int len)
{
bool isSwapped;
/**
* Here we are running n-1 steps,
for each step, max item will
come at the last respective
index and swap element if the
element is smaller than the
previous one.
**/
for (int i = 0; i < len; i++)
{
isSwapped = false;
for (int j = 1; j < len - i; j++)
{
if (num[j] < num[j - 1])
{
swapNums(num, j, (j - 1));
isSwapped = true;
}
}
if (!isSwapped)
{
break;
}
}
}
// Swaps two numbers in array
void swapNums(int nums[],
int first, int second)
{
int curr = nums[first];
nums[first] = nums[second];
nums[second] = curr;
}
OutputBefore sorting the array is:
1 12 6 8 10
After sorting the array is:
1 6 8 10 12
- Time Complexity: O(n2)
- Space Complexity: O(1)
2. Optimized Approach Using QuickSort
Here, an optimized solution is presented using the quicksort sorting algorithm. Below is the C++ program to sort an array in ascending order using an optimized approach using quicksort:
C++
// C++ program to sort an array in
// ascending order using optimized
// approach using quick sort
#include <bits/stdc++.h>
using namespace std;
void quickSort(int nums[],
int low, int high);
// Driver code
int main()
{
int nums[] = {1, 6, 3, 10, 50};
int size_nums = (sizeof(nums) /
sizeof(nums[0]));
cout << "Before sorting array is: \n";
for (int i = 0; i < size_nums; i++)
cout << nums[i] << " ";
cout << "\n\n";
quickSort(nums, 0, size_nums - 1);
cout << "After sorting array is: \n";
for (int i = 0; i < size_nums; i++)
cout << nums[i] << " ";
cout << "\n";
return 0;
}
/**
* Sorts the specified array into ascending
numerical order.
*
* @param nums the array to be sorted.
* @param low for explaining the part of
array working on.
* @param high for explaining the part of
array working on.
*/
void quickSort(int nums[],
int low, int high)
{
// Base Condition
if (low >= high)
return;
// These are just for swapping
// the elements.
int start = low, end = high;
int mid = start + ((end - start) / 2);
int pivot = nums[mid];
while (start <= end) {
while (nums[start] < nums[end])
start++;
while (nums[end] > pivot)
end--;
if (start <= end)
{
// Swapping the start and end
// elements.
int x = nums[start];
nums[start] = nums[end];
nums[end] = x;
start++;
end--;
}
}
quickSort(nums, low, end);
quickSort(nums, start, high);
}
OutputBefore sorting array is:
1 6 3 10 50
After sorting array is:
1 3 6 10 50
Time Complexity:
- Best Case - O(n log n)
- Worst Case- O(n2)
Space Complexity: O(1)
Similar Reads
C++ Program to Find the Second Largest Element in an Array In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The
3 min read
Sort Vector of Pairs in Ascending Order in C++ Sorting a vector of pairs in ascending order means arranging the elements in such a way that first pair is lesser than second pair, second pair is lesser than third pair and so on.The easiest way to sort the vector of pairs in ascending order is to use std::sort() function. The below code example il
4 min read
How to Sort an Array in Descending Order using STL in C++? Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn how to sort an array in descending order using STL in C++. ExamplesInput: arr[] = {11, 9, 45, 21};Output: 78
4 min read
C++ Program for Sorting all array elements except one Given an array, a positive integer, sort the array in ascending order such that the element at index K in the unsorted array stays unmoved and all other elements are sorted. Examples: Input : arr[] = {10, 4, 11, 7, 6, 20} k = 2; Output : arr[] = {4, 6, 11, 7, 10, 20} Input : arr[] = {30, 20, 10} k =
3 min read
C++ Program for Sorting array except elements in a subarray Given an array A positive integers, sort the array in ascending order such that element in given subarray (start and end indexes are input) in unsorted array stay unmoved and all other elements are sorted.Examples : Input : arr[] = {10, 4, 11, 7, 6, 20} l = 1, u = 3 Output : arr[] = {6, 4, 11, 7, 10
2 min read