
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sort an Array in C++
Suppose we have an array of integers; we have to sort them in ascending order. So if the array is like [5,2,3,1], then the result will be [1,2,3,5]
To solve this, we will follow these steps −
Make one method called partition, this will take array, low and high
set pivot := low
for i in range low to high – 1
if nums[i] < nums[high], then swap(nums[i] and nums[pivot]), increase pivot by 1
swap nums[pivot] and nums[high]
Define a method called sortArr(), this will take array, low and high
if low >= high, then return
partitionIndex := partition(nums, low, high)
sortArr(nums, low, partitionIndex – 1)
sortArr(nums, partitionIndex + 1, high)
call the sortArr() from main method by passing low and high as 0 and size of arr – 1
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; void print_vector(vector<string> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: int partition(vector <int>& nums, int low, int high){ int pivot = low; for(int i = low; i < high; i++){ if(nums[i] < nums[high]){ swap(nums[i], nums[pivot]); pivot++; } } swap(nums[pivot], nums[high]); return pivot; } void sortArr(vector <int>& nums, int low, int high){ if(low >= high) return; int partitionIndex = partition(nums, low, high); sortArr(nums, low, partitionIndex - 1); sortArr(nums, partitionIndex + 1, high); } vector<int> sortArray(vector<int>& nums) { sortArr(nums, 0, nums.size() - 1); return nums; } }; main(){ vector<int> v1 = {5,2,3,1}; Solution ob; print_vector(ob.sortArray(v1)); }
Input
[5,2,3,1]
Output
[1,2,3,5]
- Related Articles
- Sort an Array of string using Selection sort in C++
- How to sort an array in C#?
- How to use std::sort to sort an array in C++
- Sort an array in descending order using C#
- C program to sort an array in an ascending order
- C program to sort an array by using merge sort
- How to sort an array of dates in C/C++?
- C# program to sort an array in descending order
- How to sort an Array using STL in C++?
- C program to sort an array in descending order
- Sort Transformed Array in C++
- Sort an array according to the order defined by another array in C++
- How do you sort an array in C# in ascending order?
- How do you sort an array in C# in descending order?
- C/C++ Program to Count Inversions in an array using Merge Sort?
