Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
How to Sort a Vector in Descending Order Using STL in C++?
Next article icon

How to sort an Array using STL in C++?

Last Updated : 19 Mar, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[], sort this array using STL in C++.

Example:

  Input: arr[] = {1, 45, 54, 71, 76, 12}  Output: {1, 12, 45, 54, 71, 76}    Input: arr[] = {1, 7, 5, 4, 6, 12}  Output: {1, 4, 5, 6, 7, 12}  

Approach: Sorting can be done with the help of sort() function provided in STL.

Syntax:

  sort(arr, arr + n);  




// C++ program to sort Array
// using sort() in STL
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // Get the array
    int arr[] = { 1, 45, 54, 71, 76, 12 };
  
    // Find the size of the array
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Print the array
    cout << "Array: ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
  
    // Sort the array
    sort(arr, arr + n);
  
    // Print the sorted array
    cout << "Sorted Array: ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
  
    return 0;
}
 
 
Output:
  Array: 1 45 54 71 76 12   Sorted Array: 1 12 45 54 71 76  


Next Article
How to Sort a Vector in Descending Order Using STL in C++?

C

code_r
Improve
Article Tags :
  • C++
  • cpp-array
  • STL
Practice Tags :
  • CPP
  • STL

Similar Reads

  • How to sort an array of dates in C/C++?
    Given an array of dates, how to sort them. Example: Input: Date arr[] = {{20, 1, 2014}, {25, 3, 2010}, { 3, 12, 1676}, {18, 11, 1982}, {19, 4, 2015}, { 9, 7, 2015}} Output: Date arr[] = {{ 3, 12, 1676}, {18, 11, 1982}, {25, 3, 2010}, {20, 1, 2014}, {19, 4, 2015}, { 9, 7, 2015}} We strongly recommend
    3 min read
  • Sort on the basis of number of factors using STL
    Given an array of positive integers. Sort the given array in decreasing order of a number of factors of each element, i.e., an element having the highest number of factors should be the first to be displayed and the number having the least number of factors should be the last one. Two elements with
    7 min read
  • How to Sort a Vector in Descending Order Using STL in C++?
    Sorting vector in descending order means arranging the elements in such a way that the first element will be largest, and second element will be second largest and so on. In C++, the simplest way to sort the vector in descending order is to by using the sort() function with a custom comparator. [GFG
    3 min read
  • How to Take Input in Array in C++?
    Arrays in C++ are derived data types that can contain multiple elements of the same data type. They are generally used when we want to store multiple elements of a particular data type under the same name. We can access different array elements using their index as they are stored sequentially in th
    3 min read
  • Insertion sort using C++ STL
    Implementation of Insertion Sort using STL functions. Pre-requisites : Insertion Sort, std::rotate, std::upper_bound, C++ Iterators. The idea is to use std::upper_bound to find an element making the array unsorted. Then we can rotate the unsorted part so that it ends up sorted. We can traverse the a
    1 min read
  • Sorting an array according to another array using pair in STL
    We are given two arrays. We need to sort one array according to another. Examples: Input : 2 1 5 4 9 3 6 7 10 8 A B C D E F G H I JOutput : 1 2 3 4 5 6 7 8 9 10 B A F D C G H J E I Here we are sorting second array(a character array) according to the first array (an integer array).We have discussed d
    6 min read
  • Quickly merging two sorted arrays using std::merge() in C++ STL
    C++ program to merge two sorted arrays of length 'n' and 'm' respectively in sorted order. Examples: Input: A[] = {3, 6, 9} B[] = {2, 7, 11} Output: C[] = {2, 3, 6, 7, 9, 11} Input: A[] = {1, 1, 3, 6, 9} B[] = {1, 2, 7, 11, 11} Output: C[] = {1, 1, 1, 2, 3, 6, 7, 9, 11, 11} We have discussed other a
    2 min read
  • array::operator[ ] in C++ STL
    Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::operator[] This operator is used to reference the element present at position given inside the operator.
    2 min read
  • Create a Sorted Array Using Binary Search
    Given an array, the task is to create a new sorted array in ascending order from the elements of the given array.Examples: Input : arr[] = {2, 5, 4, 9, 8} Output : 2 4 5 8 9 Input : arr[] = {10, 45, 98, 35, 45} Output : 10 35 45 45 98 The above problem can be solved efficiently using Binary Search.
    9 min read
  • array::fill() and array::swap() in C++ STL
    Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::fill() This function is used to set a common value for all the elements of the array container. Syntax : a
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences