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++
#include <bits/stdc++.h>
using namespace std;
void sort( int num[], int len);
void swapNums( int nums[],
int first, int second);
int main()
{
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;
}
void sort( int num[], int len)
{
bool isSwapped;
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 ;
}
}
}
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++
#include <bits/stdc++.h>
using namespace std;
void quickSort( int nums[],
int low, int high);
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;
}
void quickSort( int nums[],
int low, int high)
{
if (low >= high)
return ;
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)
{
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)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!