C++ Program to Sort the Elements of an Array in Ascending Order
Last Updated :
10 Jul, 2022
Improve
Try it on GfG Practice
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 10Input: 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++ 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;
}
Output
Before 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++ 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);
}
Output
Before 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)